Skip to content

Commit

Permalink
2024-05-30
Browse files Browse the repository at this point in the history
  • Loading branch information
mjj111 committed May 30, 2024
1 parent 03dcb75 commit 5886852
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions mjj111/implement/ํ‘œํŽธ์ง‘.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.util.*;

class ํ‘œํŽธ์ง‘ {
public String solution(int n, int k, String[] cmd) {
Stack<Integer> cancels = new Stack<>();
int now = k;
int totalRows = n;

for (String c : cmd) {
if (c.charAt(0) == 'U') {
int value = Integer.parseInt(c.split(" ")[1]);
now -= value;
continue;
}
if (c.charAt(0) == 'D') {
int value = Integer.parseInt(c.split(" ")[1]);
now += value;
continue;
}
if (c.charAt(0) == 'C') {
cancels.push(now);
totalRows--;
if (now == totalRows) now--;
continue;
}
if (c.charAt(0) == 'Z') {
int cancelledRow = cancels.pop();
if (cancelledRow <= now) now++;
totalRows++;
}
}

StringBuilder sb = new StringBuilder();
for (int i = 0; i < totalRows; i++) {
sb.append("O");
}

while (!cancels.isEmpty()) {
sb.insert(cancels.pop().intValue(), "X");
}

return sb.toString();
}
}

0 comments on commit 5886852

Please sign in to comment.