프로그래머스/C
프로그래머스 Level 1 : 덧칠하기 C언어
starfish22
2023. 5. 1. 10:54
728x90
▶문제 : 코딩테스트 연습 - 덧칠하기 | 프로그래머스 스쿨 (programmers.co.kr)
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
▶코드 작성
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int n, int m, int section[], size_t section_len) {
int answer = 0;
int end = 0;//마지막에 칠한 벽 위치
for (int i = 0; i < section_len; i++) {
if (end < section[i]) {//칠하지 않은 벽 일때
end = section[i] + m - 1;//section[i]을 기준으로 칠함
answer++;
}
}
return answer;
}
▶해석
section 배열이 이미 정렬되어 있으므로 앞에서부터 차례대로 칠하고,
어디까지 칠했는지 end 변수로 나타내어 단순 반복하였다.
728x90