-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1936.Add_Minimum_Number_of_Rungs.java
More file actions
44 lines (32 loc) · 1.47 KB
/
1936.Add_Minimum_Number_of_Rungs.java
File metadata and controls
44 lines (32 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// class Main {
// public static void main (String[] args) {
// int[] rungs = {3, 6, 8, 10};
// int dist = 1;
// Solution sol = new Solution();
// System.out.println(sol.addRungs(rungs, dist));
// }
// }
class Solution {
public int addRungs(int[] rungs, int dist) {
int extra_lad = 0; // Count the extra ladders needed
int current_height = 0; // Track the current height
// O(n)
for (int i=0; i<rungs.length; i++) {
int dist_diff = rungs[i] - current_height; // Find the height difference between the current height and the next ladder
// If the difference is greater than that of the maximums jumps that they can make
if (dist_diff > dist) {
current_height = rungs[i]; // Update the current height with the next height
// First we check how many ladders can we put with height 'dist' between current position and the next ladder
// if dist_diff is a multiple of dist, then we reduce one from their multiple.
// if dist_diff is not a multiple of dist, then we add extra_lad with their multiple.
extra_lad += (dist_diff%dist == 0 ? (dist_diff / dist) - 1 : (dist_diff / dist));
continue;
}
// Update the current height
current_height += dist_diff;
}
return extra_lad;
}
}
// Time Complexity - O(n)
// Beats 100% - 1 ms