#1936

Add Minimum Number of Rungs

specialist · 965 · lc medium +32 · verified · 43.8% accepted · 394 likes · top 25%

Description

You are climbing a ladder with rungs at heights given by the strictly increasing array rungs. Starting from the floor (height 0), you can reach any height within dist of your current height. You may insert new rungs at positive integer heights that are not already taken.

Return the minimum number of rungs you must add to climb to the last rung.

Example 1:

Input: rungs = [1,3,5,10], dist = 2
Output: 2
Explanation:
You currently cannot reach the last rung.
Add rungs at heights 7 and 8 to climb this ladder.
The ladder will now have rungs at [1,3,5,7,8,10].

Example 2:

Input: rungs = [3,6,8,10], dist = 3
Output: 0
Explanation:
This ladder can be climbed without adding additional rungs.

Example 3:

Input: rungs = [3,4,6,7], dist = 2
Output: 1
Explanation:
You currently cannot reach the first rung from the ground.
Add a rung at height 1 to climb this ladder.
The ladder will now have rungs at [1,3,4,6,7].

Code

1
2
3