프로그래머스 기능개발

문제

1

문제 출처 : https://programmers.co.kr/learn/courses/30/lessons/42586

풀이

작업진도와 속도를 각각의 큐에 저장한다.

그리고 day 변수에 100이 되는 날짜 계산을 통해 나온값의 올림을 저장한다음 큐를 pop시키고 count를 증가시켰다.

부동소수점 주의점

이때 주의 할 점은 day 계산시 .0을 안붙이면 값이 다르게 나온다.

float a = (100 - 5) / 21.0; // 4.52...
float b = (100 - 5) / 21; // 4

그리고 큐가 비었는지 확인하고 front 변수에 현재 맨 앞에있는 큐에 계산된 day와 작업속도를 곱한 값을 더했을 때 100이 넘으면 마찬가지로 pop시키고 count를 증가시킨다.

큐가 비거나 100이 안넘는 값이 나올 때 까지 반복한다.

그리고 마지막으로 count를 배열에 저장하고 큐가 남아있으면 다시 day를 구하는 작업부터 시작한다.

#include <string>
#include <vector>
#include <queue>
#include <iostream>
#include <cmath>

using namespace std;

vector<int> solution(vector<int> progresses, vector<int> speeds)
{
	vector<int> answer;

	queue<int> q;
	queue<int> speedQue;
	int day = 0;

	int size = progresses.size();

	for (int i = 0; i < size; i++)  // 큐에 정보를 담음
	{
		q.push(progresses[i]);
		speedQue.push(speeds[i]);
	}



	while (!q.empty())
	{
		int count = 0;
		day =(int)ceil((100.0 - q.front()) / speedQue.front());
		q.pop();
		speedQue.pop();
		count++;

		if (!q.empty())
		{
			int front = q.front() + day * speedQue.front();
			while (front >= 100)
			{
				q.pop();
				speedQue.pop();
				count++;
				if (!q.empty())
					front = q.front() + day * speedQue.front();
				else
					break;
			}

		}
		answer.push_back(count);

	}



	return answer;
}

댓글남기기