அல்கோரிதம்next greater logic
Next greater — core logic
TT
Testlaa Team
May 14, 2026•1 min read
இடமிருந்து வலம்: stack — வலது next greater காத்திருப்பு.
Try this in Python
def next_greater(nums: list[int]) -> list[int | None]:
n = len(nums)
ans: list[int | None] = [None] * n
st: list[int] = []
for i in range(n - 1, -1, -1):
while st and st[-1] <= nums[i]:
st.pop()
ans[i] = st[-1] if st else None
st.append(nums[i])
return ans
print(next_greater([2, 4, 0, 9, 6]))
Key takeaways
- Reverse scan — வலது next.
- Indices forward-ம் common.
<=vs<tie-break.
Tags:
Stacks & queuesPythonமாணவர்கள்
