FastPrepMaximum Saw Height for At Least M Cut Length
Problems/Maximum Saw Height for At Least M Cut Length

Maximum Saw Height for At Least M Cut Length

Amazon logoAmazon● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source gives the monotone wood-cutting objective and the concrete values N=4, required length 7, and heights [20,15,10,17]. The judged task makes the integer-height convention and bounds explicit while preserving that objective, for an estimated 96% source match.

You have vertical wooden poles with integer heights heights. Set a saw to a non-negative integer height h; every pole taller than h contributes height - h units of wood, and shorter poles contribute nothing.

Given requiredWood, return the largest saw height that collects at least that much wood.

Function

maxSawHeight(heights: int[], requiredWood: int) → int

Examples

Example 1

heights = [20,15,10,17]requiredWood = 7return = 15

At height 15 the cuts yield 5 + 0 + 0 + 2 = 7. Raising the saw would yield too little.

Example 2

heights = [4,42,40,26,46]requiredWood = 20return = 36

Height 36 yields 6 + 4 + 10 = 20 units from the three taller poles.

Example 3

heights = [5]requiredWood = 5return = 0

The only way to collect all five units is to place the saw at ground level.

Constraints

  • 1 <= heights.length <= 100000.
  • 1 <= heights[i] <= 10^9.
  • 1 <= requiredWood <= min(2 * 10^9, sum(heights)).

More Amazon problems

  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
See Amazon hiring insights
Saved
class Solution {
    public int maxSawHeight(int[] heights, int requiredWood) {
      // write your code here
    }
}
Checking account…