programmers_큰 수 만들기_java
2023. 4. 15. 16:21ㆍAlgorithm/Programmers
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/42883
import java.util.*;
class Solution {
public String solution(String number, int k) {
String answer = "";
int numberLength = number.length();
Stack<Integer> stack = new Stack<>();
for(int i = 0; i< numberLength; i++){
while(k>0 && !stack.isEmpty()){
if(stack.peek() < number.charAt(i)-'0'){
stack.pop();
k--;
}else{
break;
}
}
stack.push(number.charAt(i)-'0');
}
StringBuilder sb = new StringBuilder();
while(!stack.isEmpty()){
if(stack.size() > numberLength - k){
stack.pop();
}else{
sb.insert(0, stack.pop());
}
}
answer = sb.toString();
return answer;
}
}
'Algorithm > Programmers' 카테고리의 다른 글
programmers_124 나라의 숫자_java (0) | 2023.04.16 |
---|---|
programmers_롤케이크 자르기_java (0) | 2023.04.16 |
programmers_쿼드압축 후 개수 세기_java (0) | 2023.04.14 |
programmers_숫자 변환하기_java (0) | 2023.04.14 |
programmers_뒤에 있는 큰 수 찾기_java (0) | 2023.04.14 |