Skip to content

Commit 45aca49

Browse files
authored
Create Decreasing Subsequent Values
1 parent b320db5 commit 45aca49

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Given an integer array arr, write a function decreasing_values to return an array of integers so that the subsequent integers in the array get filtered out if they are less than an integer in a later index of the array.
2+
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3+
4+
def decreasing_values(arr):
5+
n = len(arr)
6+
#monotonic stack
7+
res = []
8+
stack = []
9+
for i in range(n):
10+
11+
while stack and stack[-1]<arr[i]:
12+
stack.pop()
13+
stack.append(arr[i])
14+
15+
return stack
16+
17+
------------------------------------------------------------------------
18+
other solution
19+
20+
def decreasing_values(arr):
21+
arr = arr[::-1]
22+
m = arr[0]
23+
result = [m]
24+
for x in arr[1:]:
25+
if x >= m:
26+
result.append(x)
27+
m = x
28+
return result[::-1]

0 commit comments

Comments
 (0)