본문 바로가기
알고리즘/프로그래머스

[프로그래머스] 체육복 c++

by 오오오니 2024. 1. 24.

풀이

1. 앞뒤로 모두 체육복을 빌릴 수 있다면 앞에 있는 체육복을 빌린다.
(뒤에 있는 체육복은 2번 앞에 있는 사람이 빌릴수도 있기 때문에)
2. 여벌을 가지고 왔지만 도난당한 사람은 본인 것을 입어야하기 때문에
    체육복을 입을 수는 있지만 앞뒤로 빌려줄 수 없다. 

#include <string>
#include <vector>
#include <algorithm>

using namespace std;
bool students[32];
int solution(int n, vector<int> lost, vector<int> reserve) {
    int answer = 0;
    
    for(int i=0;i<reserve.size();i++){
        students[reserve[i]]=true;
    }
    
    sort(lost.begin(),lost.end());
    
    int cnt=0;
// (1) 여벌을 가지고 왔지만 도난당한 사람
    for(int i=0;i<lost.size();i++){
        if(students[lost[i]]){
            students[lost[i]]=false;
            cnt++;
            lost[i]=-1;
        }
    }
    for(int i=0;i<lost.size();i++){
      //(1)은 빌릴 수 없음
        if(lost[i]==-1){
            continue;     
        }
            
        if(students[lost[i]-1] )
        {   students[lost[i]-1]=false;
            cnt++;
        }
        else if(students[lost[i]+1]){
            students[lost[i]+1]=false;
            cnt++;
        }
    }
    
    return answer=  n - (lost.size()-cnt);
}