-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeliveryBox.java
42 lines (31 loc) · 1018 Bytes
/
DeliveryBox.java
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
import java.util.Stack;
class Solution {
public int solution(int[] order) {
int answer = 0;
int orderNum = 1;
Stack<Integer> subContainer = new Stack<>();
subContainer.push(orderNum++);
int i = 0;
while(answer < order.length){
int cur = order[i];
if(subContainer.isEmpty()) subContainer.add(orderNum++);
if(subContainer.peek() == cur){
subContainer.pop();
answer ++;
}else{
while(orderNum <= order.length){
subContainer.push(orderNum++);
if(subContainer.peek() == cur){
answer ++;
subContainer.pop();
break;
}
else if(orderNum == order.length+1 && subContainer.peek() != cur)
return answer;
}
}
i+= 1;
}
return answer;
}
}