앞쪽에 있는 기능이 전부 개발되었다면 앞에서부터 기능이 개발되지 않은 것이 나올 때까지 pop 해주고

그 cnt를 answer에 저장해주면 된다.

 

우선 progresses와 speeds를 전부 큐에 넣어주고 반복문을 통해 0번 위치의 기능을 수행하는 데에

얼마나 걸리는지 세준다. 이후 현재 날 * speed의 front + q의 front가 100보다 크다면 그 기능도

이미 완료되었기 때문에 pop 해주면 된다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string>
#include <vector>
#include <queue>
using namespace std;
 
vector<int> solution(vector<int> progresses, vector<int> speeds) {
    vector<int> answer;
    queue<int> q, sp;
 
    for (int i : progresses) q.push(i);
    for (int i : speeds) sp.push(i);
    
    while (!q.empty()) {
        int cnt = 1;
        int day = 0;
        int cur = q.front(); q.pop();
        int spd = sp.front(); sp.pop();
        
        while(cur < 100) cur += spd, ++day;
 
        while(!q.empty() && sp.front() * day + q.front() >= 100) {
            sp.pop(), q.pop(), ++cnt;
        }
        
        answer.push_back(cnt);
    }
    
    return answer;
}
cs

+ Recent posts