일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Generator
- Python Code
- 43. Multiply Strings
- 30. Substring with Concatenation of All Words
- 315. Count of Smaller Numbers After Self
- attribute
- 운영체제
- Convert Sorted List to Binary Search Tree
- 시바견
- 컴퓨터의 구조
- Protocol
- t1
- Class
- 109. Convert Sorted List to Binary Search Tree
- iterator
- Python
- kaggle
- Regular Expression
- LeetCode
- Decorator
- 715. Range Module
- 밴픽
- Python Implementation
- shiba
- data science
- concurrency
- DWG
- 파이썬
- Substring with Concatenation of All Words
- 프로그래머스
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 |