Algorithm/Programmers
[Algorithm] Programmers :: 택배상자
MaxLevel
2023. 6. 8. 12:25
https://school.programmers.co.kr/learn/courses/30/lessons/131704
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
int solution(vector<int> order)
{
int answer = 0;
stack<int> sub;
int curBoxNum = 1; // 컨베이어벨트에서 꺼낼 수 있는 상자.
int orderIndex = 0;
while (1)
{
if (orderIndex >= order.size() - 1) orderIndex = order.size() - 1;
if (order[orderIndex] > curBoxNum)
{
sub.push(curBoxNum);
++curBoxNum;
continue;
}
if (curBoxNum == order[orderIndex])
{
++curBoxNum;
++orderIndex;
++answer;
continue;
}
if (order[orderIndex] < curBoxNum)
{
if (sub.size() > 0 && sub.top() == order[orderIndex])
{
sub.pop();
++answer;
++orderIndex;
}
else break;
}
}
return answer;
}
|
cs |
음... 의외로 좀 걸렸던 문제. 실제 코딩테스트였다면 진짜 큰일날 뻔 했다.