-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableEditor.java
More file actions
68 lines (62 loc) · 1.74 KB
/
Copy pathTableEditor.java
File metadata and controls
68 lines (62 loc) · 1.74 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.*;
class Solution {
static int[] prev;
static int[] next;
static int current;
static Deque<Integer> stk = new ArrayDeque<>();
static int N;
static StringBuilder sb;
public String solution(int n, int k, String[] cmd) {
N = n;
prev = new int[n];
next = new int[n];
sb = new StringBuilder("O".repeat(n));
//초기화
for(int i = 0; i < n; i++) {
prev[i] = i-1;
next[i] = i+1;
}
next[n-1] = -1;
current = k;
for(String str: cmd) {
func(str);
}
return sb.toString();
}
static void func(String str) {
if(str.startsWith("U")) {
int x = Integer.parseInt(str.substring(2));
while(x-- > 0) {
current = prev[current];
}
} else if(str.startsWith("D")) {
int x = Integer.parseInt(str.substring(2));
while(x-- > 0) {
current = next[current];
}
} else if(str.startsWith("C")) {
stk.push(current);
if(prev[current] != -1) {
next[prev[current]] = next[current];
}
if(next[current] != -1) {
prev[next[current]] = prev[current];
}
sb.setCharAt(current, 'X');
if(next[current] == -1) {
current = prev[current];
} else {
current = next[current];
}
} else { //Z
int x = stk.pop();
if(prev[x] != -1) {
next[prev[x]] = x;
}
if(next[x] != -1) {
prev[next[x]] = x;
}
sb.setCharAt(x, 'O');
}
}
}