[Programmers] 징검다리

Time Lapse :30min 0sec

solution.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <vector>
#include <algorithm>
using namespace std;
int SIZE;
//정답이 아니나 정답으로 인정되는 코드
/*
inline bool is_possible(vector<int>& rocks, int &stnd, int &n, int &dis){
register int removed = 0, prev = 0, i;
for(i=0;i<SIZE;i++)
rocks[i]-prev<stnd ? ++removed : prev = rocks[i];

return (dis-rocks[i])<dis ? (removed+1)<=n : removed<=n;
}
*/
//정답코드

inline bool is_possible(vector<int>& rocks, int &stnd, int &n, int &dis){
register int removed = 0, prev = 0, i;
for(i=0;i<SIZE;i++)
rocks[i]-prev<stnd ? ++removed : prev = rocks[i];

return dis-rocks[i-1]<stnd ? ++removed<=n : removed<=n;
}
int solution(int distance, vector<int> rocks, int n) {
sort(rocks.begin(),rocks.end());
SIZE = rocks.size();
register int lo = 1, hi = distance, mid;
while(lo+1<hi){
mid = (lo+hi)>>1;
is_possible(rocks,mid,n,distance) ? lo = mid : hi = mid ;
}
return is_possible(rocks,lo,n,distance) ? lo : hi;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/steppingStones/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.