-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
75 lines (70 loc) · 1.64 KB
/
Solution.java
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Stack - O(n)
class Solution {
public int maxWidthRamp(int[] nums) {
Stack<Integer> stack = new Stack<>();
int maxWidth = 0;
for (int i = 0; i < nums.length; i++) {
if (stack.isEmpty() || nums[stack.peek()] > nums[i]) {
stack.push(i);
}
}
for (int j = nums.length - 1; j >= 0; j--) {
while (!stack.isEmpty() && nums[stack.peek()] <= nums[j]) {
maxWidth = Math.max(maxWidth, j - stack.pop());
}
}
return maxWidth;
}
}
/*
* Brute force (Nested Loop)- time limit exceeded
* Requires checking all pairs of indices, leading to Big O of n squared
* class Solution {
* public int maxWidthRamp(int[] nums) {
* int maxWidth = 0;
* int n = nums.length;
*
*
* for (int i = 0; i < n; i++) {
* for (int j = i + 1; j < n; j++) {
* if (nums[i] <= nums[j]) {
*
* maxWidth = Math.max(maxWidth, j - i);
* }
* }
* }
*
* return maxWidth;
*
* }
* }
*/
/*
* Optimized Nested Loop (Inner Loop Decrementing j):
* Iterates through j backwards from the end, breaking early once a valid ramp
* is found.This can lead to fewer comparisons, though it still has Big O of n
* squared time complexity in the worst case.
*
* public class Solution{
* public static int maxWidthRamp(int[] nums) {
* int maxWidth = 0;
* int n = nums.length;
*
*
* for (int i = 0; i < n; i++) {
*
* for (int j = n - 1; j > i; j--) {
* if (nums[i] <= nums[j]) {
*
* maxWidth = Math.max(maxWidth, j - i);
* break; // Found a valid j, no need to check further for this i
* }
* }
* }
*
* return maxWidth;
* }
* }
*
*
*/