leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1802.cpp (565B)
0 class Solution { 1 public: 2 int maxValue(int n, int index, int maxSum) { 3 const int a = index, b = n - index - 1; 4 int low = 0, high = maxSum; 5 6 const auto arit = [](long n, int mid) { return (long)n * mid - n * (n + 1) / 2; }; 7 while (low < high) { 8 int mid = (low + high + 1) / 2; 9 long res = mid + arit(min(a, mid - 1), mid) + arit(min(b, mid - 1), mid); 10 if (res <= maxSum - n) 11 low = mid; 12 else 13 high = mid - 1; 14 } 15 return low + 1; 16 } 17 };