[프로그래머스 Level 2] 큰 수 만들기
1분 읽기
풀이 1:
function solution(number, k) {
const stack = [];
const idx = [...number].findIndex(current => {
if(k === 0) return true; // 얼리 리턴
while(stack.length && stack.at(-1) < current && 0 < k){
stack.pop();
k--;
}
stack.push(current);
});
// 얼리 리턴 되었다면
if(idx > -1) return stack.join('') + number.slice(idx);
return stack.slice(0, number.length - k).join('');
}Review
제한 조건
- number는 2자리 이상, 1,000,000자리 이하인 숫자입니다.
- k는 1 이상 number의 자릿수 미만인 자연수입니다.
Greedy10편 중 4번째
관련 글
4분 읽기
소수 구하기
소수 판별법과 소수를 구하는 알고리즘을 정리합니다.
1분 읽기
[프로그래머스 Level 2] 미로 탈출
프로그래머스 미로 탈출을 BFS로 풀이합니다.
1분 읽기
[프로그래머스 Level 2] 마법의 엘리베이터
프로그래머스 마법의 엘리베이터를 자리수 그리디로 풀이합니다.