11501-s2 하는중

This commit is contained in:
2026-02-09 20:01:03 +09:00
parent 5d4a3bd574
commit 3ca18fe457

62
workbook_8708/11501-s2.py Normal file
View File

@@ -0,0 +1,62 @@
# 주식
import sys
from collections import deque
input = sys.stdin.readline
def calculate_revenue(n, prices):
total = 0
q = deque([prices[0]])
up = 0
if prices[0] <= prices[1]:
up = 1
else:
up = 0
for i in range(1, n):
if len(q) != 0 and i == n-1 and prices[i] >= q[-1]:
high = q.pop() if i != n-1 else prices[i]
while len(q) >= 1 and q[0] > high:
q.popleft()
while len(q) >= 1:
total += (high-q.pop())
if not up:
if len(q) != 0 and prices[i] > q[-1]:
up = 1
q.append(prices[i])
continue
if len(q) != 0 and (prices[i] < q[-1] or i == n-1):
high = q.pop() if i != n-1 else prices[i]
while len(q) >= 1 and q[0] > high:
q.popleft()
while len(q) >= 1:
total += (high-q.pop())
up = 0
q.append(prices[i])
return total
def solution():
t = int(input().rstrip())
for _ in range(t):
n = int(input().rstrip())
prices = list(map(int, input().rstrip().split()))
print(calculate_revenue(n, prices))
return
solution()
"""
걸린 시간:
시간 복잡도:
해설:
"""