๐ ์๊ณ ๋ฆฌ์ฆ/Programmers
[C++/PGS] Lv.2 : ๊ธฐ๋ฅ๊ฐ๋ฐ
xxilliant
2025. 4. 24. 16: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
๋ฐ์ํ