Java

프로그래머스: 덧칠하기

Daeryuk Kim 2023. 11. 16. 13:13
class Solution {
    public int solution(int n, int m, int[] section) {
        int need_to_draw = section[0];
        int answer = 1;
        for (int i=1; i<section.length; i++) {
            if (need_to_draw + m - 1 < section[i]) {
                // 시작 위치(2) + 롤러길이(4) - 1 = 5까지 칠해짐
                // 5 < 6 보다 작으므로 칠해야 하는 부분 업데이트
                need_to_draw = section[i];
                answer++;
            }
        }
        return answer;
    }


    public static void main(String[] args) {
        int n = 8; // 1~8번까지 칸 들
        int m = 4; // 롤러가 한 번에 칠 할 수 있는 길이
        int[] section = {2, 3, 6}; // 2,3,6이 안 칠해져 있어요
        // Solution 클래스의 인스턴스 생성
        Solution sol = new Solution();

        // 샘플 데이터로 solution 메소드 호출하고 결과 저장
        int result = sol.solution(n, m, section);

        // 결과 출력
        System.out.println(result);
    }
}