728x90
https://school.programmers.co.kr/learn/courses/30/lessons/42883
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
메모리: 93.1 MB, 시간: 4951.01 ms
사용 알고리즘: 탐욕법
class Solution {
public String solution(String number, int k) {
StringBuilder answer = new StringBuilder();
// 현재 지울 수 있는 개수
int count = k;
int now = 0, next;
while(now < number.length()) {
// 현재 위치에서 가장 가까운 자신보다 큰 수 찾기
next = now + 1;
while(next < number.length() && number.charAt(now) >= number.charAt(next))
next++;
// 자신보다 큰 수까지 모두 지울 수 있으면 지우기
if(count >= next - now) {
count -= next - now;
now = next;
}
else {
answer.append(number.charAt(now));
now++;
}
}
return answer.toString();
}
}728x90
'Problem Solving > Programmers' 카테고리의 다른 글
| [프로그래머스, 12948] 핸드폰 번호 가리기 (java) (0) | 2025.05.16 |
|---|---|
| [프로그래머스, 12901] 2016년 (java) (0) | 2025.05.14 |
| [프로그래머스, 42860] 조이스틱 (java) (0) | 2025.05.14 |
| [프로그래머스, 181837] 커피 심부름 (java) (0) | 2025.05.13 |
| [프로그래머스, 181834] l로 만들기 (java) (0) | 2025.05.13 |