Skip to content

Commit

Permalink
Update 16. Maximum Width Ramp.py
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb committed Aug 5, 2022
1 parent 4b350a3 commit f8c7bff
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion 13_Stack/16. Maximum Width Ramp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
# https://leetcode.com/problems/maximum-width-ramp/

class Solution:
def maxWidthRamp(self, nums: List[int]) -> int:
# nums = [9,8,1,0,1,9,4,0,4,1]
# arr = [0, 1, 2, 3]
arr = [] # array in indeices Sorted in decreasing order of elements
for i, ch in enumerate(nums):
if not arr or nums[arr[-1]] > nums[i]:
arr.append(i)

res = 0
i = len(nums)-1
while arr and i > 0:
if arr and nums[arr[-1]] <= nums[i]:
res = max(res, i - arr.pop())
else:
i -= 1

return res # 7


# Time: O(n)
# Space: O(n)




class Solution:
def maxWidthRamp(self, nums: List[int]) -> int:
'''
Expand Down Expand Up @@ -43,4 +69,4 @@ def maxWidthRamp(self, nums: List[int]) -> int:
while stack and nums[stack[-1]] <= nums[i]:
res = max(res, i - stack.pop())

return res
return res

0 comments on commit f8c7bff

Please sign in to comment.