-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package lv2; | ||
|
||
import java.util.Stack; | ||
|
||
public class 택배상자 { | ||
public int solution(int[] order) { | ||
|
||
Stack<Integer> stack = new Stack<>(); | ||
int idx = 0; | ||
int answer = 0; | ||
|
||
for (int i = 1; i <= order.length; i++) { // 기존 컨테이너의 순서 | ||
|
||
// 보조 컨테이너 확인 | ||
while (!stack.empty() && idx < order.length && stack.peek() == order[idx]) { | ||
stack.pop(); | ||
idx++; | ||
answer++; | ||
} | ||
|
||
// 기존 컨테이너 확인 | ||
if (idx == order.length) { | ||
return answer; | ||
} | ||
|
||
if (order[idx] != i) { // 다름 | ||
stack.push(i); | ||
} else { // 찾음 | ||
answer++; | ||
idx++; | ||
} | ||
} | ||
|
||
// 보조 컨테이너 확인 | ||
while (!stack.empty() && idx < order.length && stack.peek() == order[idx]) { | ||
stack.pop(); | ||
idx++; | ||
answer++; | ||
} | ||
|
||
return answer; | ||
} | ||
} |