1936. Add Minimum Number of Rungs LeetCode Solution

In this guide, you will get 1936. Add Minimum Number of Rungs LeetCode Solution with the best time and space complexity. The solution to Add Minimum Number of Rungs problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Add Minimum Number of Rungs solution in C++
  4. Add Minimum Number of Rungs solution in Java
  5. Add Minimum Number of Rungs solution in Python
  6. Additional Resources
1936. Add Minimum Number of Rungs LeetCode Solution image

Problem Statement of Add Minimum Number of Rungs

You are given a strictly increasing integer array rungs that represents the height of rungs on a ladder. You are currently on the floor at height 0, and you want to reach the last rung.
You are also given an integer dist. You can only climb to the next highest rung if the distance between where you are currently at (the floor or on a rung) and the next rung is at most dist. You are able to insert rungs at any positive integer height if a rung is not already there.
Return the minimum number of rungs that must be added to the ladder in order for you 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].

See also  1620. Coordinate With Maximum Network Quality LeetCode Solution

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].

Constraints:

1 <= rungs.length <= 105
1 <= rungs[i] <= 109
1 <= dist <= 109
rungs is strictly increasing.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1936. Add Minimum Number of Rungs LeetCode Solution in C++

class Solution {
 public:
  int addRungs(vector<int>& rungs, int dist) {
    int ans = 0;
    int prev = 0;

    for (const int rung : rungs) {
      ans += (rung - prev - 1) / dist;
      prev = rung;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1936. Add Minimum Number of Rungs LeetCode Solution in Java

class Solution {
  public int addRungs(int[] rungs, int dist) {
    int ans = 0;
    int prev = 0;

    for (final int rung : rungs) {
      ans += (rung - prev - 1) / dist;
      prev = rung;
    }

    return ans;
  }
}
// code provided by PROGIEZ

1936. Add Minimum Number of Rungs LeetCode Solution in Python

class Solution:
  def addRungs(self, rungs: list[int], dist: int) -> int:
    ans = 0
    prev = 0

    for rung in rungs:
      ans += (rung - prev - 1) // dist
      prev = rung

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.