• Feed
  • Explore
  • Ranking
/
/
    Problem Solving

    [Programmers] 큰 수 만들기

    코딩테스트 연습 > 탐욕법(Greedy) > 큰 수 만들기 [Lv. 2]
    GreedyMonotonicStack프로그래머스
    h
    hyeonZIP
    2026.03.10
    ·
    1 min read

    https://school.programmers.co.kr/learn/courses/30/lessons/42883

    import java.io.*;
    import java.util.*;
    
    class Solution {
        public String solution(String number, int k) {
            String answer = "";
            
            Deque<Integer> stack = new ArrayDeque<>();
            
            for(int num : Arrays.stream(number.split("")).mapToInt(Integer::parseInt).toArray()){
                if(stack.isEmpty()){
                    stack.push(num);
                    continue;
                }
                
                if(stack.peek() >= num || k==0){
                    stack.push(num);
                    continue;
                }
                
                while(!stack.isEmpty() && stack.peek() < num && k>0){
                    stack.pop();
                    k--;
                }
                stack.push(num);
            }
            
            for(int i=0; i<k; i++){
                stack.pop();
            }
            
            StringBuilder sb = new StringBuilder();
            
            for(int num : stack){
                sb.insert(0,num);
            }
            
            answer = String.valueOf(sb);
            
            return answer;
        }
    }

    주요 개념

    • 그리디

    • 모노토닉 스택

    풀이 과정

    • 가장 최근에 풀었던 백준의 탑 보기 문제와 동일한 모노토닉 스택 활용 문제다.







    - 컬렉션 아티클