GitHubSeob

C++ / 프로그래머스 / 덧칠하기 본문

Programmers/Level 1

C++ / 프로그래머스 / 덧칠하기

GitHubSeob 2023. 6. 24.

문제

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제풀이

롤러가 닿을 수 있는 범위를 알려주는 변수인 roller을 선언한다.

section을 탐색하면서 roller값이 더 작다면 롤러를 옮겨야 하므로 roller값을 section + m - 1로 바꾸고 answer에 1을 더한다.

 

코드

#include <string>
#include <vector>

using namespace std;

int solution(int n, int m, vector<int> section) {
    int answer(0), idx(0), roller(0);

    for (idx = 0; idx < section.size(); ++idx) {
        if (roller < section[idx]) {
            roller = section[idx] + m - 1;
            answer++;
        }
    }

    return answer;
}