-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0401-1.cpp
More file actions
48 lines (44 loc) · 1.07 KB
/
0401-1.cpp
File metadata and controls
48 lines (44 loc) · 1.07 KB
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
41
42
43
44
45
46
47
48
#include <string>
#include <vector>
using namespace std;
vector<int> stack;
int solution(vector<int> order)
{
int answer = 0;
int conveyIdx = 1, orderIdx = 0;
while (conveyIdx <= order.size())
{
int target = order[orderIdx];
// 순서 맞아서 그냥 넣으면 되는 경우
if (conveyIdx == target)
{
answer++;
conveyIdx++;
orderIdx++;
continue;
}
// 일단 스택에서 꺼낼 수 있는지 체크
if (!stack.empty() && stack.back() == target)
{
stack.pop_back();
answer++;
orderIdx++;
continue;
}
// 순서도 안맞고, 스택에서도 못꺼내면 일단 스택에 넣고 다음거를 살펴본다
stack.push_back(conveyIdx);
conveyIdx++;
}
while (!stack.empty())
{
if (stack.back() == order[orderIdx])
{
answer++;
stack.pop_back();
orderIdx++;
continue;
}
break;
}
return answer;
}