๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๐Ÿ‘จ‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

 

ํ(Queue)๋ฅผ ์ด์šฉํ•˜์—ฌ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ํ’€์ด๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

  1. for ๋ฌธ์„ ํ†ตํ•ด, ๊ฐ ์ž‘์—…์„ ์ˆœํšŒ
    • ํ˜„์žฌ ์ž‘์—…์˜ ๋ฐฐํฌ ๊ฐ€๋Šฅํ•œ ๋‚จ์€ ์ผ์ˆ˜๋ฅผ ๊ณ„์‚ฐ
    • ํ์— ์•„๋ฌด๊ฒƒ๋„ ์—†๋‹ค๋ฉด, push()ํ•˜๊ณ  ๋‹ค์Œ ๋ฃจํ”„๋กœ ์ด๋™
  2. ํ์˜ front๋ณด๋‹ค ํ˜„์žฌ ์ž‘์—…์˜ ๋ฐฐํฌ ๊ฐ€๋Šฅํ•œ ๋‚จ์€ ์ผ์ˆ˜๊ฐ€ ์ž‘๋‹ค๋ฉด, front๊ฐ€ ๋ฐฐํฌ ๋  ๋•Œ ๊ฐ™์ด ๋ฐฐํฌ๋˜๋ฏ€๋กœ push()
  3. ์•„๋‹ˆ๋ผ๋ฉด, ํ˜„์žฌ ํ์— ์žˆ๋Š” ์›์†Œ ๊ฐœ์ˆ˜๋ฅผ answer์— ์ถ”๊ฐ€ํ•˜๊ณ , ํ ์‹น ๋น„์šฐ๊ธฐ
    • ๋‹ค ๋น„์šด ํ›„, ํ˜„์žฌ ์ž‘์—…์˜ ๋ฐฐํฌ ๊ฐ€๋Šฅํ•œ ๋‚จ์€ ์ผ์ˆ˜๋ฅผ push()
  4. for ๋ฌธ์„ ๋‹ค ๋Œ๊ณ  ๋‚˜๋ฉด, ๋งˆ์ง€๋ง‰ ์ž‘์—…์„ answer์— ์ถ”๊ฐ€

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <vector>
#include <queue>
using namespace std;
const int MAX_RATE = 100;

vector<int> solution(vector<int> progresses, vector<int> speeds) {
    vector<int> answer;
    queue<int> days;
    int length = progresses.size();

    for (int i = 0; i < length; i++) {
        int remainigRate = MAX_RATE - progresses[i];
        int requiredDays = remainigRate / speeds[i];
        requiredDays += (remainigRate % speeds[i] != 0) ? 1 : 0;

        if (days.empty()) {
            days.push(requiredDays);
            continue;
        }

        // ์ด์ „ ์ž‘์—… ๋‚จ์€ ์ผ์ˆ˜๋ณด๋‹ค ์ ์œผ๋ฉด ๊ฐ™์ด ๋ฐฐํฌํ•ด์•ผ ํ•จ
        if (requiredDays <= days.front()) {
            days.push(requiredDays);
            continue;
        }

        int releaseNum = days.size();
        answer.push_back(releaseNum);
        
        while (!days.empty()) days.pop();
        days.push(requiredDays);
    }

    answer.push_back(days.size());
    return answer;
}

 

 

728x90
๋ฐ˜์‘ํ˜•