EKOSPOJ
Pattern: Binary search on the answer
Raise the saw blade as high as possible while still cutting at least m metres of wood. A higher blade cuts less, so binary search the highest blade height whose cut is still enough.
The problem
A woodcutter has a saw blade that can be set at any whole-number height H. It cuts every tree taller than H down to
H, and he keeps the parts above. Given the tree heights and the wood he needs, m, return the highest H that still
gives at least m metres. For example, heights [20, 15, 10, 17] with m = 7 give 15, and [4, 42, 40, 26, 46] with
m = 20 give 36.
The link above is the SPOJ problem EKO, which reads its input from a file. This guide's function maxSawHeight solves
one case, and its name is this guide's own. Enough wood always exists.
Think first
- For a blade height
H, how much wood is cut? - When
Hgoes up, does the wood go up or down? - Which blade height surely gives enough wood?
Hints
Brute force, and why it fails
Try every height from the tallest tree down: O(n × maxHeight), and heights can reach 10⁹. Binary search: O(n log maxHeight).
The key insight
The amount of wood falls as the blade rises, so "is this height high enough to stop at" is monotonic. The best height is the last guess that passes.
Dry run
heights = [20, 15, 10, 17] and m = 7. The blade can go from 0 up to 20.
The plan
lo = 0, hi = max(heights)
while lo < hi:
mid = lo + (hi - lo + 1) / 2
wood = sum of (h - mid) over h > mid
if wood >= m: lo = mid
else: hi = mid - 1
return lo
Complexity
- Time
O(n log maxHeight)- Space
O(1)
About 30 guesses, each one pass over the trees.
Edge cases
mequal to the total of all heights: the answer is 0.- One tree: the answer is
h - m. - Many equal trees: the wood changes in big steps, which the search handles the same way.
I've built my logic, show the code
Java
class Solution {
public int maxSawHeight(int[] heights, long m) {
int lo = 0, hi = 0;
for (int h : heights) hi = Math.max(hi, h); // a blade at the tallest tree cuts nothing
while (lo < hi) {
int mid = lo + (hi - lo + 1) / 2; // round up, because lo = mid below
if (wood(heights, mid) >= m) lo = mid; // still enough wood: raise the blade
else hi = mid - 1;
}
return lo;
}
private long wood(int[] heights, int blade) {
long total = 0;
for (int h : heights) if (h > blade) total += h - blade; // only the part above the blade
return total;
}
}C++
class Solution {
public:
int maxSawHeight(vector<int>& heights, long long m) {
int lo = 0, hi = *max_element(heights.begin(), heights.end()); // a blade at the top cuts nothing
while (lo < hi) {
int mid = lo + (hi - lo + 1) / 2; // round up, because lo = mid below
if (wood(heights, mid) >= m) lo = mid; // still enough wood: raise the blade
else hi = mid - 1;
}
return lo;
}
long long wood(vector<int>& heights, int blade) {
long long total = 0;
for (int h : heights) if (h > blade) total += h - blade; // only the part above the blade
return total;
}
};Python
class Solution:
def maxSawHeight(self, heights: list[int], m: int) -> int:
lo, hi = 0, max(heights) # a blade at the tallest tree cuts nothing
while lo < hi:
mid = (lo + hi + 1) // 2 # round up, because lo = mid below
if sum(h - mid for h in heights if h > mid) >= m:
lo = mid # still enough wood: raise the blade
else:
hi = mid - 1
return loCommon mistakes
- Adding the wood in 32 bits; a million trees of 10⁹ overflow.
- Counting trees shorter than the blade with a negative cut.
- Rounding
middown withlo = mid.