Skip to content

Commit

Permalink
프로그래머스/Greedy: 큰 수 만들기(#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
sieunnnn committed Jan 10, 2024
1 parent 90b85ae commit 037d5db
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/programmers/algorithmKit/greedy/PROB02.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package programmers.algorithmKit.greedy;

import java.util.*;

public class PROB02 {
public String solution(String number, int k) {
StringBuilder answer = new StringBuilder();
Stack<Character> stack = new Stack<>();

for (int i = 0; i < number.length(); i++) {
char c = number.charAt(i);
while (!stack.isEmpty() && stack.peek() < c && k > 0) {
stack.pop();
k--;
}
stack.push(c);
}

while (!stack.isEmpty()) {
if (k == 0) {
answer.append(stack.pop());

} else {
stack.pop();
k--;
}
}

return answer.reverse().toString();
}
}

0 comments on commit 037d5db

Please sign in to comment.