-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExplodeBalloon.java
More file actions
46 lines (35 loc) · 1.16 KB
/
Copy pathExplodeBalloon.java
File metadata and controls
46 lines (35 loc) · 1.16 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
package org.example.Algorithm.test;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
public class ExplodeBalloon {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Deque<int[]> balloon = new ArrayDeque<>();
StringBuilder sb = new StringBuilder();
int N = sc.nextInt();
for (int i = 0; i < N; i++) {
balloon.add(new int[]{i + 1, sc.nextInt()});
}
while (!balloon.isEmpty()) {
int[] pop = balloon.remove();
sb.append(pop[0]).append(" ");
if (balloon.size() == 1) {
int[] lastPop = balloon.remove();
sb.append(lastPop[0]).append(" ");
break;
}
int writeNum = pop[1];
if (writeNum < 0) {
for (int i = 0; i < -writeNum; i++) {
balloon.addFirst(balloon.removeLast());
}
} else {
for (int i = 0; i < writeNum - 1; i++) {
balloon.add(balloon.remove());
}
}
}
System.out.println(sb);
}
}