MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / cuttingWood

Function cuttingWood

cpp/Binary Search/cutting_wood.cpp:4–18  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2#include <algorithm>
3
4int cuttingWood(std::vector<int>& heights, int k) {
5 int left = 0;
6 int right = *std::max_element(heights.begin(), heights.end());
7 while (left < right) {
8 // Bias the midpoint to the right during the upper-bound binary
9 // search.
10 int mid = (left + right) / 2 + 1;
11 if (cutsEnoughWood(mid, k, heights)) {
12 left = mid;
13 } else {
14 right = mid - 1;
15 }
16 }
17 return right;
18}
19
20// Determine if the current value of 'H' cuts at least 'k' meters of
21// wood.

Callers

nothing calls this directly

Calls 1

cutsEnoughWoodFunction · 0.70

Tested by

no test coverage detected