기능개발(15분)

myeongrangcoding·2023년 11월 13일

프로그래머스

목록 보기
8/65

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

그러면 안되는데 중간에 연락이 와서 잠깐 연락을 했다... 할 땐 문제에만 집중하기.

구현 아이디어 3분 구현 12분

풀이

  1. 큐를 2개 활용하여 q1에서 하나씩 꺼내서 작업량을 높여준다.
  2. 작업량을 높인 프로세스를 q2에 넣는다.
  3. q1이 비면 q2의 front를 검사하여 100이 넘는다면 pop 한다.
  4. 100이 넘지 않는다면 break 한 뒤 q1에 q2의 값들을 넣어준다.
  5. 다시 1번부터 반복한다.
  6. 반복은 q1과 q2가 둘 다 비었을 때 종료한다.
#include <string>
#include <vector>
#include <queue>

using namespace std;

struct Data
{
    int prog;
    int speed;
    
    Data(int p, int s)
    {
        prog = p;
        speed = s;
    }
};

vector<int> solution(vector<int> progresses, vector<int> speeds) {
    vector<int> answer;
    
    int N = progresses.size();
    queue<Data> q1, q2;
    
    for(int i = 0; i < N; ++i)
    {
        q1.push(Data(progresses[i], speeds[i]));
    }
    
    while(!q1.empty() || !q2.empty())
    {
        if(!q1.empty())
        {
            Data b = q1.front();
            b.prog += b.speed;
            q1.pop();
            q2.push(b);
        }
        if(q1.empty())
        {
            int res = 0;
            // q2에서 100인지 확인.
            while(!q2.empty())
            {
                Data b = q2.front();
                if(b.prog >= 100)
                {
                    q2.pop();
                    ++res;
                }
                else break;
            }
            
            if(res != 0) answer.push_back(res);
            while(!q2.empty())
            {
                q1.push(q2.front());
                q2.pop();
            }
        }
    }
    
    return answer;
}

풀이

이런 생각은 어떻게 하는 걸까? 진짜 대단하시다.

#include <string>
#include <vector>

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)
    {
        // 6 / 1 + 1 = 7;
        // 69 / 30 + 1 = 3;
        // 44 / 4 + 1 = 12;
        
        // day == 작업을 마치는데 걸리는 날짜.
        day = (99 - progresses[i]) / speeds[i] + 1;
        
        // max_day보다 더 오래 걸리면 1을 push_back.
        if(answer.empty() || max_day < day)
            answer.push_back(1);
        
        // 더 적게 걸리면 뒤를 ++.
        else ++answer.back();
        
        // 7
        if(max_day < day)
            max_day = day;
    }
    
    return answer;
}

풀이

내 풀이는 상당히 구질구질해서 다시 생각해 보았다.

작업을 완료하기 위해 남은 날을 계산하여 큐에 넣고 front를 꺼내 지금 꺼낸 front가 이전에 꺼낸 front 보다 큰 값이라면 answer에 1을 넣고 그렇지 않다면 answer를 더해주는 방법이 있겠다.

다시 풀어야지.

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

using namespace std;

vector<int> solution(vector<int> progresses, vector<int> speeds) {
    vector<int> answer;
    
    queue<int> days;
    for(int i = 0; i < progresses.size(); ++i)
    {
        int p = progresses[i];
        int d = (99 - p) / speeds[i] + 1;
        days.push(d);
    }
    
    int maxi = 0;
    while(!days.empty())
    {
        int d = days.front();
        if(maxi < d)
        {
            answer.push_back(1);
            maxi = d;
        }
        else ++answer.back();
        
        days.pop();
    }
    
    return answer;
}
profile
명랑코딩!

0개의 댓글