Link | 프로그래머스 161918번 문제 : 덧칠하기
이 문제는 최소한의 횟수로 전체 필요한 부분을 덧칠하면 되는 문제이다.
가장 최소한의 횟수로 덧칠하기 위해서는 앞쪽 부분부터 차례대로 덧칠하면 된다.
Stream
public int solution(int n, int m, int[] section) {
Queue<Integer> nums = Arrays.stream(section).boxed()
.collect(Collectors.toCollection(LinkedList::new));
return (int) IntStream.range(0, nums.size()).takeWhile(i -> !nums.isEmpty()).peek(i -> {
int block = nums.poll();
while (!nums.isEmpty() && block + m - 1 >= nums.peek()) nums.poll();
}).count();
}
Non-Stream
public int solution(int n, int m, int[] section) {
int block = section[0], count = 1;
for (int i = 1; i < section.length; i++) {
if (block + m - 1 < section[i]) {
count++;
block = section[i];
}
}
return count;
}