codekofi
← All problems

Problem 13

Medium45 XP

1 · Worked examples

0 / 5

Give it an input and say what you think comes back. It compiles and runs for real, so a wrong guess still shows you the answer — probe as much as you like. Only correct predictions count.

solution()
returns

2 · Which problem is it?

Locked until you have predicted 5 outputs correctly.

The accepted solution

1int solution(const vector<int>& nums, int h) {
2 int lo = 1;
3 int hi = *max_element(nums.begin(), nums.end());
4 while (lo < hi) {
5 int mid = lo + (hi - lo) / 2;
6 long long hours = 0;
7 for (int x : nums) {
8 hours += (x + mid - 1) / mid;
9 }
10 if (hours <= h) hi = mid;
11 else lo = mid + 1;
12 }
13 return lo;
14}

Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.