-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe_next_less_element.py
More file actions
43 lines (33 loc) · 1.41 KB
/
Copy pathe_next_less_element.py
File metadata and controls
43 lines (33 loc) · 1.41 KB
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
####################################################
# for each element in the list, return its distance
# from the 'next less element' (NLE). If an
# element does not have a NLE, return -1.
#
# uses a monotonous stack
###################################################
from typing import List
class NextLessElement:
def calculate(self, nums: List[int]) -> List[int]:
if not nums:
return []
right, monostack = [-1] * len(nums), []
for i in range(len(nums)):
""" check if the element pointed by the stack top
is greater than the current element.
if YES, then the current element is NLE of the
element pointed by stack. Remove element from
the stack and set its NLE
how it differs from PLE?
------------------------
PLE -> PLE of current element is set in the loop
NLE -> NLE of element pointed by the stack is set in the loop
"""
while monostack and nums[monostack[-1]] > nums[i]:
x = monostack.pop()
right[x] = i
""" append current index to the stack """
monostack.append(i)
""" calculate the distance of each element from its NLE """
for i in range(len(right)):
right[i] = right[i] - i if right[i] != -1 else right[i]
return right