덧칠하기

2023. 9. 18. 18:05

인트로

레벨 1 javascript 문제 다풀려고 노력 중..


문제

덧칠하기

https://school.programmers.co.kr/learn/courses/30/lessons/161989


나의 풀이

function solution(n, m, section) {
    let lastPaintIndex = section[0]+m-1;
    let cnt = 1;
    section.map((el) => {
        if(el > lastPaintIndex) {
            cnt+=1;
            lastPaintIndex=el+m-1;
            while(lastPaintIndex > n) {
                lastPaintIndex-=1;
            }
        }
    })
    return cnt;
}
//다른풀이 참고 후 
function solution(n, m, section) {
    let lastPaintIndex = section[0]+m-1;
    let cnt = 1;
    section.map((el) => {
        if(el > lastPaintIndex) {
            cnt+=1;
            lastPaintIndex=el+m-1;
        }
    })
    return cnt;
}

다른 풀이 참고

function solution(n, m, sections) {
    var answer = 0;
    var painted = 0;
    for(var section of sections) {
        if(painted < section) {
            answer++;
            painted = section+m-1;
        }
    }
    return answer;
}

롤러가 벽을 넘어가면 안된다는 규칙이 있어서 넣은 while문이었는데 빼도 문제가 없네요. 생각해보니 넘어갔다면 이미 다 칠한게 되겠군요.


 

 

 

 

 

 

'programmers' 카테고리의 다른 글

모음사전  (1) 2023.05.01
모의고사  (2) 2023.04.25
추억 점수  (0) 2023.04.06
가장 가까운 글자  (0) 2023.04.03
시저 암호  (0) 2023.03.29

BELATED ARTICLES

more