-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnearest_smaller.py
executable file
·56 lines (47 loc) · 1.36 KB
/
nearest_smaller.py
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
#!/usr/bin/python
"""
Given an array of integers, find the nearest smaller number for every element such that
the smaller element is on left side.
http://www.geeksforgeeks.org/find-the-nearest-smaller-numbers-on-left-side-in-an-array/
"""
def solution1(arr):
"""
>>> solution1([1, 6, 4, 10, 2, 5])
[None, 1, 1, 4, 1, 2]
>>> solution1([1, 3, 0, 2, 5])
[None, 1, None, 0, 2]
"""
nearest_smaller = [None]
stack = [arr[0]]
for i in range(1, len(arr)):
while stack and stack[-1] > arr[i]:
stack.pop()
if stack:
nearest_smaller.append(stack[-1])
else:
nearest_smaller.append(None)
stack.append(arr[i])
return nearest_smaller
def solution2(arr):
"""
>>> solution2([1, 6, 4, 10, 2, 5])
[None, 1, 1, 4, 1, 2]
>>> solution2([1, 3, 0, 2, 5])
[None, 1, None, 0, 2]
"""
nearest_smaller = [None]
for i in range(1, len(arr)):
if arr[i] > arr[i-1]:
nearest_smaller.append(arr[i-1])
else:
j = len(nearest_smaller)-1
while j >= 0 and arr[j] and arr[j] > arr[i]:
j -= 1
if j < 0:
nearest_smaller.append(None)
else:
nearest_smaller.append(arr[j])
return nearest_smaller
if __name__ == "__main__":
import doctest
doctest.testmod()