반응형
https://school.programmers.co.kr/learn/courses/30/lessons/161989
#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 |