Skip to content

Instantly share code, notes, and snippets.

@inspirit941
Created January 26, 2020 09:02
Show Gist options
  • Save inspirit941/d76d6b8c95eb72beed7c549f4286878b to your computer and use it in GitHub Desktop.
Save inspirit941/d76d6b8c95eb72beed7c549f4286878b to your computer and use it in GitHub Desktop.
import sys
sys.stdin = open('input.txt')
n = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
answer = [0 for _ in range(len(arr))]
stack = []
for i in range(len(arr)-1, -1, -1):
while stack and arr[stack[-1]] < arr[i]:
answer[stack.pop()] = i+1
stack.append(i)
print(*answer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment