அல்கோரிதம்index stack

Index stack — இடங்கள்

TT
Testlaa Team
May 14, 20261 min read

Span/rectangle — indices stack; width information எளிது.

Try this in Python

def daily_temperatures(t: list[int]) -> list[int]:
    n = len(t)
    ans = [0] * n
    st: list[int] = []
    for i in range(n):
        while st and t[st[-1]] < t[i]:
            j = st.pop()
            ans[j] = i - j
        st.append(i)
    return ans


print(daily_temperatures([73, 74, 75, 71, 69, 72, 76, 73]))

Key takeaways

  • i warmer index; i-j wait.
  • Stock spans / histogram variants.

Tags:

Stacks & queuesPythonமாணவர்கள்