Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mypy] Fix type annotations in data_structures/stacks/next_greater_element.py #5763

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactor next_greater_element.py
  • Loading branch information
dylanbuchi committed Nov 4, 2021
commit 0c15d449f65ed6a116ead601a47604dad6e34c2c
19 changes: 9 additions & 10 deletions data_structures/stacks/next_greater_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ def next_greatest_element_slow(arr: list[float]) -> list[float]:
"""

result = []
arr_size = len(arr)

for i in range(0, len(arr), 1):
for i in range(arr_size):
next: float = -1
for j in range(i + 1, len(arr), 1):
for j in range(i + 1, arr_size):
if arr[i] < arr[j]:
next = arr[j]
break
Expand Down Expand Up @@ -57,21 +58,19 @@ def next_greatest_element(arr: list[float]) -> list[float]:
>>> next_greatest_element(arr) == expect
True
"""
arr_size = len(arr)
stack: list[float] = []
result: list[float] = [-1] * len(arr)
result: list[float] = [-1] * arr_size

for index in reversed(range(len(arr))):
if len(stack):
for index in reversed(range(arr_size)):
if stack:
while stack[-1] <= arr[index]:
stack.pop()
if len(stack) == 0:
if not stack:
break

if len(stack) != 0:
if stack:
result[index] = stack[-1]

stack.append(arr[index])

return result


Expand Down