문제
풀이 1
const solution = (number, k) => {
const stack = [];
let count = 0;
for (let i = 0; i < number.length; i++) {
const item = number[i]
if (stack.length === 0) {
stack.push(item)
continue;
}
while (stack[stack.length - 1] < item) {
stack.pop()
count++
if (count === k) return stack.join("") + number.slice(i, number.length)
if (stack.length === 0) break;
}
stack.push(item)
}
return stack.join("").slice(0, number.length - k + count)
}
풀이 2
function solution(number, k) {
let stack = [];
let arr = number.split('').reverse();
while(arr.length && k>0){
stack.push(arr.pop())
while(stack[stack.length-1] < arr[arr.length-1] && k>0) {
stack.pop();
k = k-1;
}
}
if(k !== 0) stack = stack.slice(0, -k)
return stack.join('') + arr.reverse().join('')
}