[Programmers] 큰 수 만들기 - JAVA

ONE·2022년 1월 25일
0

Programmers

목록 보기
3/24

📚 Problem

큰 수 만들기

  • 어떤 숫자에서 k 개의 숫자를 제거 했을 때 얻을 수 있는 가장 큰 숫자

📝 Solution

  • 왼쪽부터 적은값을 제거해야 가장 큰 숫자를 얻을 수 있습니다
  • 왼쪽부터 탐색하며 다음값보다 작은 값을 문자열에서 빼줍니다
  • 위를 k 번 반복 합니다
for(int i = 0; i < k; i++){
	int idx = answer.length() - 1;
	for(int j = 0; j < idx; j++)
        if(answer.charAt(j) < answer.charAt(j + 1)){
            idx = j;
            break;
        }
        answer.deleteCharAt(idx);
}

💻 Code

Solution.java

public class Solution {
    public String solution(String number, int k) {
        StringBuilder answer = new StringBuilder(number);

        for(int i = 0; i < k; i++){
            int idx = answer.length() - 1;
            for(int j = 0; j < idx; j++)
                if(answer.charAt(j) < answer.charAt(j + 1)){
                    idx = j;
                    break;
                }
            answer.deleteCharAt(idx);
        }

        return answer.toString();
    }
}

SolutionTest.java

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class SolutionTest {
    Solution solution;

    @BeforeEach
    public void setSol(){
        solution = new Solution();
    }

    @Test
    public void solution_1(){
        String result = solution.solution("1924", 2);
        assertEquals("94", result);
    }

    @Test
    public void solution_2(){
        String result = solution.solution("1231234", 3);
        assertEquals("3234", result);
    }

    @Test
    public void solution_3(){
        String result = solution.solution("4177252841", 4);
        assertEquals("775841", result);
    }
}
profile
New, Strange, Want to try

0개의 댓글