Maximum Element After Decreasing and Rearranging
Real-world version: Shaved Course WallThe statement here is our own: the same inputs, outputs and technique.
A mason has one block per entry of blocks, and blocks[i] is how many courses of brick block i currently stands. Before building, the mason may apply either operation as often as wanted, in any order:
- shave a block down to any smaller whole number of courses, never below
1and never taller than it already is; - swap two blocks, so the blocks may end up in any order along the wall.
The finished wall, read left to right, must satisfy both rules:
- the leftmost block stands exactly
1course; - neighbouring blocks differ by at most
1course.
Every block must be used. Return the largest number of courses any block can stand in a finished wall.
Examples
Example 1
- Input
- blocks = [2, 2, 1, 1, 3]
- Output
- 3
Shaving the two blocks of three courses down and ordering the wall as 1, 1, 2, 2, 3 obeys both rules, and its tallest block stands 3 courses.
Example 2
- Input
- blocks = [1, 1, 5]
- Output
- 2
Ordering the wall as 1, 1, 2 after shaving the tall block from 5 courses to 2 obeys both rules, and its tallest block stands 2 courses.
Example 3
- Input
- blocks = [1000000000, 1, 1000000000]
- Output
- 3
Shaving the two tall blocks to 2 and 3 courses and ordering the wall as 1, 2, 3 obeys both rules, and its tallest block stands 3 courses.
Constraints
- 1 <= blocks.length <= 10^5
- 1 <= blocks[i] <= 10^9
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def tallest_after_shaving(blocks: list[int]) -> int:public int tallestAfterShaving(int[] blocks)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 13 test cases: 13 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's