forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
49 lines (37 loc) · 1.03 KB
/
main.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/// Source : https://leetcode.com/problems/minimize-max-distance-to-gas-station/
/// Author : liuyubobobo
/// Time : 2020-04-24
#include <iostream>
#include <queue>
#include <cmath>
using namespace std;
/// Binary Search
/// Time Complexity: O(n * log(1e14))
/// Space Complexity: O(n)
class Solution {
public:
double minmaxGasDist(vector<int>& stations, int K) {
vector<double> seg;
for(int i = 1; i < stations.size(); i ++)
seg.push_back(stations[i] - stations[i - 1]);
double l = 0.0, r = 1e8;
while(r - l >= 1e-6){
double mid = (l + r) / 2;
if(ok(seg, K, mid)) r = mid;
else l = mid;
}
return l;
}
private:
bool ok(const vector<double>& seg, int K, double gap){
int x = 0;
for(double e: seg)
x += (ceil(e / gap) - 1);
return x <= K;
}
};
int main() {
vector<int> stations = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << Solution().minmaxGasDist(stations, 9) << endl;
return 0;
}