All problems
1034MediumMathBinary SearchGreedy

The Tallest Post at a Chosen Place

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1802Maximum Value at a Given Index in a Bounded Array

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A fence has n posts in a row, numbered from 0. Every post's height is a whole number of at least 1, posts standing next to each other differ in height by at most 1, and all the heights added together come to at most budget.

Return the greatest height the post at spot can be given.

Examples

Example 1

Input
n = 4, spot = 2, budget = 6
Output
2

Four posts and a budget of six. Making the third post two tall needs heights of one, one, two and one, which adds to five and fits. Making it three tall would need one, two, three and two, adding to eight.

Example 2

Input
n = 6, spot = 1, budget = 10
Output
3

Six posts and a budget of ten. Heights of two, three, two, one, one and one add to exactly ten. Pushing the second post to four would need three either side and cost fourteen.

Example 3

Input
n = 2, spot = 0, budget = 2
Output
1

Two posts and a budget of two leaves nothing over: both posts stand at the minimum of one.

Constraints

  • 1 <= n <= 10^9
  • 1 <= budget <= 10^9
  • n <= budget
  • 0 <= spot <= n - 1

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def max_value(n: int, spot: int, budget: int) -> int:
Java
public int maxValue(int n, int spot, int budget)
September 7
Apply