https://school.programmers.co.kr/learn/courses/30/lessons/161989
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(int n, int m, vector<int> section) {
// n : 벽 길이 m : 덮어씌워 지는 길이 section : 벗겨진 인덱스 모음
int answer = 0, covered_idx = -1; // covered_idx == 칠 했을 때 요기 까지 칠해짐
sort(section.begin(), section.end()); // 오름차순 정렬
for (int i = 0; i < section.size(); i++)
{
if (section[i] < covered_idx) // 이미 칠해졌으면
continue ;
covered_idx = section[i] + m;
answer++;
}
return answer;
}
문제 이해가 안될 땐 예제를 보면 된다~~
'<algorithm> > 프로그래머스' 카테고리의 다른 글
프로그래머스 대충 만든 자판 c++ (2) | 2023.05.22 |
---|---|
프로그래머스 공원 산책 c++ (0) | 2023.05.22 |
프로그래머스 추억 점수 c++ (3) | 2023.05.21 |
달리기 경주 c++ (0) | 2023.04.28 |
프로그래머스 124 나라의 숫자(12899)(c++) (0) | 2023.04.12 |