leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2141.cpp (511B)


      1 class Solution {
      2   public:
      3     long long maxRunTime(int n, const vector<int> &batteries) {
      4         long long low = 1, high = reduce(batteries.begin(), batteries.end(), 0LL) / n;
      5         while (low < high) {
      6             long long mid = high - (high - low) / 2, sum = 0;
      7             for (long long bat : batteries)
      8                 sum += min(bat, mid);
      9             if (sum >= (long long)(n * mid))
     10                 low = mid;
     11             else
     12                 high = mid - 1;
     13         }
     14         return low;
     15     }
     16 };