덧칠하기
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문이었는데 빼도 문제가 없네요. 생각해보니 넘어갔다면 이미 다 칠한게 되겠군요.