mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 01:00:15 +00:00
[mypy] Fix type annotations in data_structures/stacks/next_greater_element.py
(#5763)
* Fix type annotations in next_greater_element.py * Refactor next_greater_element.py
This commit is contained in:
parent
1a43c92c77
commit
e7381b513b
|
@ -1,8 +1,10 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
arr = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0]
|
arr = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0]
|
||||||
expect = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1]
|
expect = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1]
|
||||||
|
|
||||||
|
|
||||||
def next_greatest_element_slow(arr: list) -> list:
|
def next_greatest_element_slow(arr: list[float]) -> list[float]:
|
||||||
"""
|
"""
|
||||||
Get the Next Greatest Element (NGE) for all elements in a list.
|
Get the Next Greatest Element (NGE) for all elements in a list.
|
||||||
Maximum element present after the current one which is also greater than the
|
Maximum element present after the current one which is also greater than the
|
||||||
|
@ -10,10 +12,13 @@ def next_greatest_element_slow(arr: list) -> list:
|
||||||
>>> next_greatest_element_slow(arr) == expect
|
>>> next_greatest_element_slow(arr) == expect
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
for i in range(0, len(arr), 1):
|
arr_size = len(arr)
|
||||||
next = -1
|
|
||||||
for j in range(i + 1, len(arr), 1):
|
for i in range(arr_size):
|
||||||
|
next: float = -1
|
||||||
|
for j in range(i + 1, arr_size):
|
||||||
if arr[i] < arr[j]:
|
if arr[i] < arr[j]:
|
||||||
next = arr[j]
|
next = arr[j]
|
||||||
break
|
break
|
||||||
|
@ -21,7 +26,7 @@ def next_greatest_element_slow(arr: list) -> list:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def next_greatest_element_fast(arr: list) -> list:
|
def next_greatest_element_fast(arr: list[float]) -> list[float]:
|
||||||
"""
|
"""
|
||||||
Like next_greatest_element_slow() but changes the loops to use
|
Like next_greatest_element_slow() but changes the loops to use
|
||||||
enumerate() instead of range(len()) for the outer loop and
|
enumerate() instead of range(len()) for the outer loop and
|
||||||
|
@ -31,7 +36,7 @@ def next_greatest_element_fast(arr: list) -> list:
|
||||||
"""
|
"""
|
||||||
result = []
|
result = []
|
||||||
for i, outer in enumerate(arr):
|
for i, outer in enumerate(arr):
|
||||||
next = -1
|
next: float = -1
|
||||||
for inner in arr[i + 1 :]:
|
for inner in arr[i + 1 :]:
|
||||||
if outer < inner:
|
if outer < inner:
|
||||||
next = inner
|
next = inner
|
||||||
|
@ -40,7 +45,7 @@ def next_greatest_element_fast(arr: list) -> list:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def next_greatest_element(arr: list) -> list:
|
def next_greatest_element(arr: list[float]) -> list[float]:
|
||||||
"""
|
"""
|
||||||
Get the Next Greatest Element (NGE) for all elements in a list.
|
Get the Next Greatest Element (NGE) for all elements in a list.
|
||||||
Maximum element present after the current one which is also greater than the
|
Maximum element present after the current one which is also greater than the
|
||||||
|
@ -53,21 +58,19 @@ def next_greatest_element(arr: list) -> list:
|
||||||
>>> next_greatest_element(arr) == expect
|
>>> next_greatest_element(arr) == expect
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
stack = []
|
arr_size = len(arr)
|
||||||
result = [-1] * len(arr)
|
stack: list[float] = []
|
||||||
|
result: list[float] = [-1] * arr_size
|
||||||
|
|
||||||
for index in reversed(range(len(arr))):
|
for index in reversed(range(arr_size)):
|
||||||
if len(stack):
|
if stack:
|
||||||
while stack[-1] <= arr[index]:
|
while stack[-1] <= arr[index]:
|
||||||
stack.pop()
|
stack.pop()
|
||||||
if len(stack) == 0:
|
if not stack:
|
||||||
break
|
break
|
||||||
|
if stack:
|
||||||
if len(stack) != 0:
|
|
||||||
result[index] = stack[-1]
|
result[index] = stack[-1]
|
||||||
|
|
||||||
stack.append(arr[index])
|
stack.append(arr[index])
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user