Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

[2023-10-02] wooyeol #296 #314

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Programmers/주식가격/wooyeol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
주식가격
https://school.programmers.co.kr/learn/courses/30/lessons/42584?language=python3

풀이시간
11:30 ~ 11:50 (20분)

문제 조건
1 <= prices의 각 가격 <= 10,000
2 <= prices의 길이 <= 100,000

시간 복잡도 :
O(N(N+1)/2)

접근법
무슨 알고리즘으로 풀이 할 수 있을까? -> 큐 와 스택

문제를 잘못 읽어 10,000개의 데이터 인줄 알고 시간 복잡도를 생각하지않고 풀이했는데
다시보니 100,000개인 것을 확인하였습니다.

그럼 이 풀이로는 풀이가 되지 않아야하는 의아함이 들어 다른 풀이를 참조하였습니다.

풀이 참고 : https://velog.io/@soo5717/프로그래머스-주식가격-Python

해당 풀이를 참고했을 때 2배 정도 빠른 효율성 테스트 결과를 확인 할 수 있었습니다.

"""
from collections import deque

# deque을 사용한 방법
def solution1(prices):
# 전체 데이터 덱으로 변환
dq = deque(prices)
answer = []

while dq:
# 덱의 첫 요소 popleft
target = dq.popleft()

sec = 0
# 현재 타겟보다 감소한 값이 존재한다면 break 후 시간 반환
for price in dq:
sec += 1
if price < target:
break
answer.append(sec)

return answer

# stack을 사용한 방법
def solution2(prices):
# answer를 max 값으로 초기화
# ex) [1,2,3,2,3] -> [4,3,2,1,0]
answer = [v for v in range(len(prices)-1, -1, -1)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 먼저 역으로 한 인덱스에서 가질 수 있는 최대값을 정의해주는 방법이 훨씬 와닿았던 것 같습니다!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 이 부분을 스택에 남은 원소들에 대해 처리해줬었는데 이렇게 구현하는게 훨씬 간결하고 가독성이 좋은 것 같아요!! 고생하셨습니다 우열님 👍👍


stack = [0]
for idx in range(1, len(prices)):
# prices의 값이 감소했다면
while stack and prices[stack[-1]] > prices[idx]:
# 가격이 떨어지지 않은 기간 업데이트
p_idx = stack.pop()
answer[p_idx] = idx - p_idx
stack.append(idx)
return answer

case1 = [1, 2, 3, 2, 3]
print(solution1(case1)) # [4, 3, 1, 1, 0]
print(solution2(case1)) # [4, 3, 1, 1, 0]