
덧칠하기코테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문이었는데 빼도 문제가 없네요. 생각해보니 넘어갔다면 이미 다 칠한게 되겠군요.
'코테' 카테고리의 다른 글
[백준-1926] 그림 (0) | 2024.07.18 |
---|---|
[javascript] class로 스택/큐 구현하기 (1) | 2023.06.12 |
모음사전 (1) | 2023.05.01 |
모의고사 (2) | 2023.04.25 |
추억 점수 (0) | 2023.04.06 |
@두루마기 :: 내가해냄
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!