Time Lapse :1hour 10min 2sec
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
| #include <string> #include <vector> #include <algorithm> using namespace std; long long solution(int n, vector<int> times) { long long answer = 0; sort(times.begin(), times.end()); long long left = 1; long long right = answer = (long long)times[times.size() - 1] * n; while (left <= right) { long long mid = (left + right) / 2; long long sum = 0; for (int i = 0; i < times.size(); ++i) { sum += mid / (long long)times[i]; } if (sum == n) answer = min(answer,mid); if (sum >= n) right = mid - 1; else left = mid + 1; } return answer; }
|