문제
https://school.programmers.co.kr/learn/courses/30/lessons/161989
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
< 덧칠하기 >
문제 풀이 (Java)
class Solution {
public int solution(int n, int m, int[] section) {
int answer = 0;
int end = 0;
for (int i = 0; i < section.length; i++) {
if (end > section[i]) {
continue;
}
end = section[i] + m;
answer += 1;
if (end > n) {
break;
}
}
return answer;
}
}
section을 살펴보며 이미 색칠한 구역이라면 넘어간다. 아직 색칠하지 않은 구역이라면 그 구역에서 m만큼 칠하며 answer도 +1 해준다. 이때, 색칠을 한 후 전체 길이 n을 넘어선다면 더 이상 색칠할 구역이 없으므로 종료한다.
최종 answer을 반환한다.

출처: 프로그래머스 코딩 테스트 연습,
https://school.programmers.co.kr/learn/challenges
'🌞Algorithm > 🔥programmers' 카테고리의 다른 글
| [programmers] 과제 진행하기 (0) | 2026.06.02 |
|---|---|
| [programmers] 땅따먹기 (0) | 2026.06.01 |
| [programmers] 공원 산책 (0) | 2026.05.28 |
| [programmers] 무인도 여행 (0) | 2026.05.27 |
| [programmers] 바탕화면 정리 (0) | 2026.05.26 |