๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš“ Self Study/๐Ÿ”“ Programmers

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (๊ธฐ๋Šฅ ๊ฐœ๋ฐœ, ์Šคํƒ/ํ) C++

by UKHYUN22 2022. 1. 3.
728x90

 

Queue๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋™์ผํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋Š”๋ฐ Time ์ดˆ๊ณผ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธด๋‹ค. ๋‹ค๋ฅธ ๊ฒฝ์šฐ๋Š” ๋‹ค ํ†ต๊ณผํ–ˆ๋Š”๋ฐ Queue๋ž‘ Vector์˜ ์ฐจ์ด๊ฐ€ ์ด๋ ‡๊ฒŒ ์‹ฌํ•œ๊ฐ€... ์ƒ๊ฐ์ด ๋“ ๋‹ค. 

 

#include <string>
#include <vector>
#include <iostream>

using namespace std;

vector<int> solution(vector<int> progresses, vector<int> speeds) {
    vector<int> answer;
    int front_index = 0;
    int count = 0;
    
    while(front_index != progresses.size()) {
        // ๊ฐ ์ž‘์—…์„ ํ•œ ๋ฒˆ์”ฉ ์ง„ํ–‰์‹œํ‚ค๋Š” for๋ฌธ
        for(int i =0 ; i < progresses.size() ; i++) {
            progresses[i] += speeds[i];
        }

        // ๋งŒ์•ฝ ์ œ์ผ ์•ž์˜ ๊ฒƒ์ด 100์ด ๋„˜์€ ๊ฒฝ์šฐ 
        // ์•ž์—์„œ๋ถ€ํ„ฐ 100์ด ๋„˜์€ ๊ฒƒ์ด ์—ฐ์†๋œ ๊ฒฝ์šฐ๋ฅผ count ํ•œ๋‹ค.
        while(true) {
            if(progresses[front_index] >= 100) {
                front_index++;
                count++;
            } 
            // ์•„๋‹Œ ๊ฒฝ์šฐ ํƒˆ์ถœ์„ ํ•œ๋‹ค.
            else break;
        }

        if(count != 0)
            answer.push_back(count);
        
        // ์„ธ์–ด์ฃผ๋Š” ๋ณ€์ˆ˜๋Š” ๋‹ค์‹œ ์ดˆ๊ธฐํ™”๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.
        count = 0;
    }
    

    return answer;
}

 

๋‚˜์ค‘์— ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋Ÿฌ ๋‹ค์‹œ ์˜ค์ž...