We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 814d4e8 commit 92b952fCopy full SHA for 92b952f
leetcode/maximum_width_ramp.rb
@@ -29,4 +29,26 @@ def max_width_ramp(nums)
29
end
30
31
max_width
32
+end
33
+
34
+#Solution 2 (using two pointer with preprocessing)
35
+# @param {Integer[]} nums
36
+# @return {Integer}
37
+def max_width_ramp(nums)
38
+ len = nums.length
39
+ max_to_the_right = [0]*len
40
+ max_to_the_right[-1] = nums[-1]
41
+ (len-2).downto(0) do |i|
42
+ max = max_to_the_right[i+1]<nums[i] ? nums[i] : max_to_the_right[i+1]
43
+ max_to_the_right[i] = max
44
+ end
45
+ max_width = 0
46
+ left=0
47
+ for right in 0...len
48
+ while max_to_the_right[right]<nums[left]
49
+ left+=1
50
51
+ max_width = [max_width, right-left].max
52
53
+ max_width
54
0 commit comments