Skip to content

Instantly share code, notes, and snippets.

@mym0404
Created May 3, 2022 04:41
Show Gist options
  • Save mym0404/89b19d2525c99756f1cff8cfe640cb57 to your computer and use it in GitHub Desktop.
Save mym0404/89b19d2525c99756f1cff8cfe640cb57 to your computer and use it in GitHub Desktop.
n = int(input())
arr = list(map(int, input().split()))
stack = []
answer = [-1 for _ in range(n)]
for i in range(n):
while len(stack) and stack[-1][0] < arr[i]:
answer[stack[-1][1]] = arr[i]
stack.pop()
stack.append([arr[i], i])
for i in answer:
print(i, end=' ')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment