๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/Programmers

[C++/PGS] Lv.2 : ๊ธฐ๋Šฅ๊ฐœ๋ฐœ

by xxilliant 2025. 4. 24.
728x90
๋ฐ˜์‘ํ˜•

 

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

 

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

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr


ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ ˆ๋ฒจ 2.

์Šคํƒ์„ ํ™œ์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•ด์•ผ ํ•œ๋‹ค

 

์ฒ˜์Œ์— ์ œ์ถœํ–ˆ์„ ๋•Œ ๋ช‡๋ช‡ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์—์„œ ์‹คํŒจ๊ฐ€ ๋–ด๋Š”๋ฐ,

์•„๋ž˜ ํ…Œ์ผ€๋ฅผ ํ™œ์šฉํ•ด์„œ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค ใ…Ž.ใ…Ž

์Šคํƒ ๋‚ด๋ถ€์˜ max๊ฐ’์„ ๊ธฐ์–ตํ•ด๋‘์–ด์•ผ ํ•œ๋‹ค!

 

 

 

๋‚˜์˜ ํ’€์ด

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

vector<int> solution(vector<int> progresses, vector<int> speeds) {
    vector<int> answer;
    int n = progresses.size();
    int nowMax = 0;
    stack<int> st;
    
    for(int i=0; i<n; ++i){
        int left = 100 - progresses[i];
        int date = left / speeds[i];
        int before = false;
        if(left % speeds[i] != 0) date += 1;
        // cout << date <<"\n";
        if(st.empty()) {
            st.push(date);
            nowMax = date;
        }
        else{
            int size = st.size();
            // top์ด ๋” ํฌ๋ฉด ์Šคํƒ์— push
            if(nowMax >= date){
                st.push(date);
                continue;
            }
            // ๋งจ ์œ„์˜ ์ˆซ์ž๋ณด๋‹ค ํฌ๋ฉด ๊ธฐ์กด ๋‚ด์šฉ์„ ๋ชจ๋‘ pop, count
            while(!st.empty()){
                st.pop();
            }
            answer.push_back(size);
            st.push(date);
            nowMax = date;
        }
    }
    if(!st.empty()){
        answer.push_back(st.size());
    }
    
    return answer;
}

 

728x90
๋ฐ˜์‘ํ˜•