일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 밴픽
- 109. Convert Sorted List to Binary Search Tree
- 프로그래머스
- Class
- Python Implementation
- 30. Substring with Concatenation of All Words
- Regular Expression
- iterator
- 43. Multiply Strings
- 컴퓨터의 구조
- Substring with Concatenation of All Words
- 시바견
- shiba
- kaggle
- 715. Range Module
- LeetCode
- 315. Count of Smaller Numbers After Self
- Decorator
- Python Code
- 파이썬
- Python
- concurrency
- 운영체제
- attribute
- Convert Sorted List to Binary Search Tree
- data science
- Protocol
- t1
- Generator
- DWG
Archives
- Today
- Total
Scribbling
LeetCode: 123. Best Time to Buy and Sell Stock III 본문
Computer Science/Coding Test
LeetCode: 123. Best Time to Buy and Sell Stock III
focalpoint 2021. 11. 3. 21:40class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
n = len(prices)
leftprofit, rightprofit = [0] * n, [0] * n
leftmin = prices[0]
for i in range(1, n):
price = prices[i]
leftmin = min(leftmin, price)
leftprofit[i] = max(leftprofit[i-1], price - leftmin)
rightmax = prices[-1]
for i in range(n-2, -1, -1):
price = prices[i]
rightmax = max(rightmax, price)
rightprofit[i] = max(rightprofit[i+1], rightmax - price)
maxprofit = 0
for i in range(n):
maxprofit = max(maxprofit, leftprofit[i]+rightprofit[i])
return maxprofit
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 124. Binary Tree Maximum Path Sum (0) | 2021.11.04 |
---|---|
LeetCode: 188. Best Time to Buy and Sell Stock IV (0) | 2021.11.04 |
LeetCode: 122. Best Time to Buy and Sell Stock II (0) | 2021.11.03 |
LeetCode: 120. Triangle (0) | 2021.11.03 |
LeetCode: 117. Populating Next Right Pointers in Each Node II (0) | 2021.11.03 |