๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][Programmers][์Šคํƒ/ํ] ๊ธฐ๋Šฅ๊ฐœ๋ฐœ

ibelieveinme 2023. 5. 18. 22:43
728x90

* ๋ฌธ์ œ

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

 

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

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

programmers.co.kr

 

* ํ•ด๊ฒฐ๋ฒ•

progresse(ํ˜„์žฌ ์ง„ํ–‰ ์ƒํƒœ)์™€ speed(ํ•˜๋ฃจ๋‹น ์ฒ˜๋ฆฌ๊ฐ€๋Šฅ ๊ฐ’)์„ ์ฃผ๋ฏ€๋กœ progress[i] += speed[i] ํ•˜๋ฉฐ ๋ฐ˜๋ณต๋ฌธ์„ ์ง„ํ–‰ํ•œ๋‹ค.

progress์˜ ๋งจ ์•ž๊ฐ’์ด 100์ด๋ฉด ๋ฐฐํฌ๋ฅผ ์ง„ํ–‰ํ•ด์•ผํ•˜๋ฏ€๋กœ progresses, speeds ๋ฐฐ์—ด์—์„œ ๊บผ๋‚ด๊ณ  ๊บผ๋‚ธ ๊ฐœ์ˆ˜๋ฅผ ์ƒˆ์„œ answer ๋ฒกํ„ฐ์— ๋„ฃ๋Š”๋‹ค. progresses ๋ฒกํ„ฐ๋ฅผ ๋ชจ๋‘ ๋ฐฐํฌํ•˜๋ฉด(empty์ƒํƒœ) answer ๋ฒกํ„ฐ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

* ์•Œ์•„๋‘˜ ๊ฒƒ

- ๋ฒกํ„ฐ์˜ ๋งจ ์•ž ์š”์†Œ ๋นผ๋Š” ๋ฒ•.

v.erase(v.begin());

๋ฒกํ„ฐ๋Š” ์›๋ž˜ stack์ฒ˜๋Ÿผ ๋งจ ๋ ์š”์†Œ๋ฅผ pop()์œผ๋กœ ๋นผ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ธ๋ฐ, queue์ฒ˜๋Ÿผ ๋งจ ์•ž ์š”์†Œ๋ฅผ ๋บ„ ์ˆ˜ ์žˆ๋‹ค.

์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ eraseํ•จ์ˆ˜์— ๋ฒกํ„ฐ์˜ ๋งจ ์‹œ์ž‘ ์š”์†Œ๊ฐ’์„ ์ฃผ๋ฉด ๋œ๋‹ค.

 

- queue ์ฝ”๋“œ

[์„ ์–ธ ๋ฐฉ๋ฒ•]

#include<queue>

queue<int> q;

 

[ํ•จ์ˆ˜]

q.push(element); // ๊ฐ’ ์ถ”๊ฐ€
q.pop(); // front()๊ฐ’ ์ œ๊ฑฐ
q.front(); // ๋งจ ์•ž ๊ฐ’ ๋ฐ˜ํ™˜
q.back(); // ๋งจ ๋งˆ์ง€๋ง‰ ๊ฐ’ ๋ฐ˜ํ™˜
q.size(); // queue ์‚ฌ์ด์ฆˆ ๋ฐ˜ํ™˜
q.empty() // ๋น„์—ˆ๋Š”์ง€ ํ™•์ธ
swap(q1 , q2);  // q1๊ณผ q2 ๋‚ด์šฉ ๋ฐ”๊พธ๊ธฐ

 

* ์ฝ”๋“œ

#include <string>
#include <vector>
using namespace std;

vector<int> solution(vector<int> progresses, vector<int> speeds) {
    vector<int> answer;
    
    //๋ชจ๋“  ์ž‘์—…์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
    while(!progresses.empty()){
        
        //์™„๋ฃŒ๋œ ์–‘์— ์ž‘์—…๋Ÿ‰ ๋”ํ•˜๊ธฐ
        for(int i = 0; i < progresses.size(); i++){
            progresses[i] += speeds[i];
        }
        
        //๋งจ ์•ž์— progress๊ฐ€ 100๋œ๊ฑฐ ์‹น ๋นผ๊ธฐ
        int count = 0;
        while(!progresses.empty() && progresses.front() >= 100){
            count++;
            progresses.erase(progresses.begin());
            speeds.erase(speeds.begin());    
        }
        if(count != 0) answer.push_back(count);
    }
    return answer;
}
728x90