덧칠하기

문제


문제풀이

덧칠해야하는 section부터 4를 더한 값은 칠하고 계속 앞으로 나가면서 안칠한 section 값이 나오면 다시 +4를 해주는 문제


나의 답안

public static int solution(int n, int m, int[] section) {
    int answer = 0;
    int[] wall = new int[n + 1];
    Arrays.fill(wall, 1);
    for (int i : section) {
        wall[i]--;
    }
    for (int i = 1; i <= n; i++) {
        if (wall[i] >= 1) continue;
        if (wall[i] == 0) {
            for (int j = 0; j < m; j++) {
                if (i + j > wall.length - 1) {
                    break;
                } else {
                    wall[i + j]++;
                }
            }
            answer++;
        }
    }

    return answer;
}

다른 답안

class Solution {
    public int solution(int n, int m, int[] section) {
        int answer = 0;
        int current = 1;

        for(int i = 0; i < section.length; i++) {
            if(current <= section[i]) {
                current = section[i] + m;
                answer++;
            }
        }

        return answer;
    }
}

'Programmers 문제풀이 > Lv.1' 카테고리의 다른 글

카드 뭉치  (0) 2023.03.23
완주하지 못한 선수  (0) 2023.03.23
체육복  (0) 2023.03.23
기사단원의 무기  (0) 2023.03.22
로또의 최고 순위와 최저 순위  (0) 2023.03.22