Programmers algorithm 문제 : 기능개발 본문

프로그래머스에서 푼 첫번째 알고리즘 문제!
예시를 읽어보면 더욱 쉽게 문제를 이해 할 수 있다.
각 기능이 앞으로 몇일이 더 소요될지를 먼저 큐에 저장한다.

다음과 같이 주어질 경우, 큐에
7,3,9 순서대로 집어 넣는다.
-------
9 3 7
-------
맨 앞의 값을 history에 저장한 후, 뒤의 값과 비교하면서 기능의 갯수를 카운트 하면 된다.
7을 pop 한후 history에 저장한다.
이후 q.front()는 3인데, history와 비교했을때, 값이 작으므로 7일 후에 두번째 기능이 발매 될 수 있다.
이런 경우 count++을 하고, history는 7로 유지!
pop()을 한 후, 다음 q.front()는 9인데, 이 기능은 7일 이후에 완료 된다는 의미 이므로 일단 지금까지 count 된 갯수만큼 먼저 answer에 넣어주면 된다.
이후 history = q.front(), count = 1로 초기화 한 후 다시 진행하면 끝!
<전체 소스코드>
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
30
31
32
33
34
35
36
37
38
39
40
41
|
#include <string>
#include <vector>
#include <queue>
using namespace std;
vector<int> solution(vector<int> progresses, vector<int> speeds) {
vector<int> answer;
queue<int> q;
for(int i = 0; i<progresses.size(); i++){
int day = 0;
if((100-progresses[i])%speeds[i] != 0){
day = (100-progresses[i]) / speeds[i]+1;
}
else{
day = (100-progresses[i]) / speeds[i];
}
q.push(day);
}
int his = 0;
int cnt = 1;
his = q.front();
while(!q.empty()){
q.pop();
if(q.front()<=his){
if(q.empty()){
answer.push_back(cnt);
}
cnt++;
}
else{
answer.push_back(cnt);
cnt = 1;
his = q.front();
}
}
return answer;
}
|
cs |
'Programmers_algorithm' 카테고리의 다른 글
Programmers algorithm 문제 : 프린터 (0) | 2022.03.08 |
---|
Comments