diff --git a/workbook_8708/11501-s2.py b/workbook_8708/11501-s2.py new file mode 100644 index 0000000..ac907b3 --- /dev/null +++ b/workbook_8708/11501-s2.py @@ -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() + +""" +걸린 시간: + +시간 복잡도: + +해설: +""" \ No newline at end of file