[InterviewBit] WoodCutting Made Easy!

WoodCutting Made Easy!

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int Solution::solve(vector<int> &A, int B) {
sort(begin(A), end(A));
vector<long long> psum{0};
for(int i = 0; i < A.size(); i++) psum.push_back(psum.back() + A[i]);
int l = 0, r = A.back(), res = -1;
while(l <= r) {
long long m = l + (r - l) / 2;
long long p = lower_bound(begin(A), end(A), m) - begin(A);
long long now = psum.back() - psum[p] - (A.size() - p) * m;
if(now < B) r = m - 1;
else {
res = m;
l = m + 1;
}
}

return res;
}n
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/04/PS/interviewbit/woodcutting-made-easy/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.