๋ฐ์ํ
๋ฌธ์ ๋งํฌ
https://programmers.co.kr/learn/courses/30/lessons/42584
๋์ํ์ด
deque๋ฅผ์จ์ผ ํจ์จ์ฑํ ์คํธ๋ฅผ ํต๊ณผํ ๊ฒ์ด๋ผ๊ณ ์๊ฐํ๋๋ฐ, ์์ธ๋ก ๊ฐ๋จํ๊ฒ ๋ฆฌ์คํธํํ๋ก ํ์๋๋ ๊ทธ๋ฅ ํด๊ฒฐ๋์๋ค.
def solution(prices):
answer = []
for i in range(len(prices)-1):
price = prices[i]
for j in range(i+1, len(prices)):
if price > prices[j]:
answer.append(j-i)
break
elif j == len(prices)-1:
answer.append(j-i)
answer.append(0)
return answer
๋ค๋ฅธ์ฌ๋ํ์ด
https://programmers.co.kr/learn/courses/30/lessons/42584/solution_groups?language=python3
๋์ฒ๋ผ ๋ฆฌ์คํธ๋ฅผ appendํด์ฃผ๋ ๋ฐฉ์์ ์๋๊ณ 0์ผ๋ก answer๋ฆฌ์คํธ๋ฅผ ์ฑ์ดํ ํ๋์ฉ +1 ํด๋๊ฐ๋๊ฒ ์ ๊ธฐํ๋ค. ๋ฌธ์ ํ์ด์ ๊ฐ์ฅ ์ค์ํ ํต์ฌ์ ์๊ฐํ ๋ฐฉ์์ ๋์ผํ๋ค.
def solution(prices):
answer = [0] * len(prices)
for i in range(len(prices)):
for j in range(i+1, len(prices)):
if prices[i] <= prices[j]:
answer[i] += 1
else:
answer[i] += 1
break
return answer
๊ทธ๋๋ ์ด๋ ๊ฒ ํ๋๊ฒ ํจ์ฌ ๊ฐ๊ฒฐํ ์ฝ๋ ๊ฐ์๋ค.
๋ฐ์ํ
'Algorithm > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ์ด๋ถํ์ - ์ ๊ตญ์ฌ์ฌ (level.3) (0) | 2021.10.07 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ ํ์ - ์นดํซ (level.2) (0) | 2021.09.23 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์คํ์ฑํ ๋ฐฉ (์นด์นด์ค, level.2) (0) | 2021.09.16 |
[ํ๋ก๊ทธ๋๋จธ์ค] 124๋๋ผ์ ์ซ์ (level.2) (0) | 2021.09.16 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์คํ/ํ - ๋ค๋ฆฌ๋ฅผ ์ง๋๋ ํธ๋ญ (level.2) (0) | 2021.09.15 |