算法才是灵魂:索引按值排序,维护索引递增栈。时间复杂度O(nlogn)代码如下,顺便说一下,楼主给的答案少了一个.. def nextProgram(l:list):
l = sorted(list(enumerate(l)), key=lambda x:x[1])
res = [-1] * len(l)
stack = []
for i,e in l:
while len(stack) > 0 and stack[-1] < i:
index = stack.pop()
res[index] = e
stack.append(i)
return res
print(nextProgram([11,13,10,5,12,21,3]))
#[12, 21, 12, 12, 21, -1, -1]