输入样例
5
3 4 2 7 5
输出样例
-1 3 -1 2 2
解答思路与笔记:
代码
n = int(input())
nums = list(map(int, input().split()))
stack = []
res = []
for num in nums:
while stack and stack[-1] >= num:
stack.pop()
if stack:
res.append(stack[-1])
else:
res.append(-1)
stack.append(num)
print(' '.join(map(str, res)))