https://school.programmers.co.kr/learn/courses/30/lessons/42586
먼저 progresses의 각 원소마다 speeds를 계산해서 며칠이 걸리는지 계산한 뒤 큐에 바로 넣어준다.
일단 가장 맨 앞의 원소를 first로 넣어주고 pop한번한다.
그 후 큐가 빌 때까지 while을 돌리면서
q의 front가 first보다 작으면 count++, pop한다.
first보다 크면 count를 answer에 넣고 first를 갱신 한 뒤 pop하고 count = 1로 초기화 한다.
마지막에 큐를 비운 다음 남은 count를 push_back한다.
#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;
for(day = 0; progresses[i] < 100; day++)
progresses[i] += speeds[i];
q.push(day);
}
int first = q.front();
int count = 1;
q.pop();
while(!q.empty())
{
if(first >= q.front())
{
count++;
q.pop();
}
else
{
answer.push_back(count);
first = q.front();
q.pop();
count = 1;
}
}
answer.push_back(count);
return answer;
}
#include <string>
#include <vector>
#include <iostream>
using namespace std;
vector<int> solution(vector<int> progresses, vector<int> speeds) {
vector<int> answer;
int day;
int max_day = 0;
for (int i = 0; i < progresses.size(); ++i)
{
day = (99 - progresses[i]) / speeds[i] + 1;
if (answer.empty() || max_day < day)
answer.push_back(1);
else
++answer.back();
if (max_day < day)
max_day = day;
}
return answer;
}
for문을 한번만 사용하고 큐도 쓰지 않았다.
똑같이 day를 계산하는데 100이아닌 99에서 뺀 다음 스피드로 나누고 +1을 하였다.
이 경우 모든 스피드 값에서 정확한 day를 계산할 수 있다.
그 후 answer가 비어있거나 max_day(가장 앞의 과정이 걸린 day)가 day보다 작으면
answer에 1을 넣는다.
else의 경우 answer에 원소가 있고, 검사하는 원소의 앞의 원소의 day가 지금 원소의 day보다 크면 가장 최근에 answer값을 ++한다.
그리고 max_day가 day보다 작다면 max_day값을 갱신한다.