Next Greater Element · Nhóm: Ngăn xếp & Hàng đợi
Phần tử lớn hơn tiếp theo (Next Greater Element), nhóm Ngăn xếp & Hàng đợi.
def next_greater(a):
res = [-1] * len(a)
stack = []
for i in range(len(a)):
while stack and a[stack[-1]] < a[i]:
res[stack.pop()] = a[i]
stack.append(i)
return res
Mở trang để xem mô phỏng từng bước và xuất slide bài giảng.