[2023년 11월 23일]기능개발(23분)

myeongrangcoding·2023년 11월 23일

프로그래머스

목록 보기
47/65

https://school.programmers.co.kr/learn/courses/30/lessons/42586#

구현 아이디어 6분 구현 17분

풀이

#include <string>
#include <vector>
#include <queue>

using namespace std;

// 남은 날짜 = 남은 진도율 / 진행 속도 (나머지가 있다면 + 1)
// 큐에 넣고 꺼내면서 현 max보다 큰 값이 front면 다음 날짜로 넘어간다.

vector<int> solution(vector<int> progresses, vector<int> speeds) {
    vector<int> answer;
    int N = progresses.size();
    
    queue<int> Q;
    for(int i = 0; i < N; ++i)
    {
        int day = (100 - progresses[i]) / speeds[i];
        if((100 - progresses[i]) % speeds[i]) ++day;
        
        Q.push(day);
        //printf("%d ", day);
    }
    
    int max_day = Q.front();
    Q.pop();
    int cnt = 1;
    
    while(!Q.empty())
    {
        int front = Q.front();
        Q.pop();
        
        if(front <= max_day)
            ++cnt;
        else
        {
            answer.push_back(cnt);
            
            // 처음에 front를 Q.front()로 적어서 틀렸다.
            // Q의 front를 이미 pop했기 때문에 의도와는 다른 front가 들어간다.
            max_day = front;
            cnt = 1;
        }
    }
    
    answer.push_back(cnt);
    
    return answer;
}
profile
명랑코딩!

0개의 댓글