์ฃผ์๊ฐ๊ฒฉ ๋ฌธ์
์คํ
Last In First Out(LIFO), ๋์ค์ ๋ค์ด๊ฐ ์์๊ฐ ๋จผ์ ๋์ด push: ์คํ์ ์์ pop: ๊ฐ์ฅ ๋ง์ง๋ง์ ๋ค์ด๊ฐ ์์๋ฅผ ์ ๊ฑฐํ๋ฉฐ ๋ฐํ
ํ์ด์ฌ ํ์ด
def solution(prices): st=[] answer = [0]*len(prices) for i in range(len(prices)): if st == []: st.append(i) continue else: if prices[i] >= prices[i-1]: st.append(i) else: while prices[i] < prices[i-1] : top = st.pop() answer[top] = i-top if st == []: st.append(i) break top = st.pop() st.append(top) if prices[i] >= prices[top] : st.append(i) break top = st.pop() st.append(top) while st != []: n=st.pop() answer[n] = top - n return answer
๊ทธ๋ฐ๋ฐ ๋ณ๋ก.. ์ข์ ์ฝ๋๋ผ๊ณ ์๊ฐ์ ์ ๋ค์ง๋ง ์ด์จ๋ ํด๊ฒฐ
์ฐธ๊ณ ํ ํ ์คํธ ์ผ์ด์ค
prices = [3, 1, 2, 4, 2]
answer = [1, 3, 2, 1, 0]
ํ์ด ๋ฐฉ๋ฒ (https://gurumee92.tistory.com/m/170 ์ฐธ๊ณ )

๋ฐ์ํ