일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 109. Convert Sorted List to Binary Search Tree
- 컴퓨터의 구조
- 밴픽
- t1
- Class
- Regular Expression
- Protocol
- Python Code
- kaggle
- Substring with Concatenation of All Words
- Convert Sorted List to Binary Search Tree
- iterator
- Generator
- LeetCode
- shiba
- data science
- attribute
- 30. Substring with Concatenation of All Words
- 파이썬
- 315. Count of Smaller Numbers After Self
- 715. Range Module
- 프로그래머스
- 시바견
- concurrency
- Python Implementation
- DWG
- Decorator
- 운영체제
- 43. Multiply Strings
- Python
Archives
- Today
- Total
Scribbling
LeetCode: 188. Best Time to Buy and Sell Stock IV 본문
Computer Science/Coding Test
LeetCode: 188. Best Time to Buy and Sell Stock IV
focalpoint 2021. 11. 4. 18:11LeetCode에서 푼 문제 중 가장 어려웠다...
당연히 빡대가리 내 머리로는 못 풀었고, 다른 사람 풀이 보고 이해하는 데에도 한참 걸렸다.
DP로 풀어야하는데, 점화식은 아래와 같다. O(NK)
위 점화식에 대한 예제이다.
P = [3, 3, 5, 0 0, 3, 1, 4], K = 3
class Solution:
def maxProfit(self, k: int, prices: List[int]) -> int:
if not prices:
return 0
n = len(prices)
dp = [[0] * (n) for _ in range(k+1)]
ret = 0
for i in range(1, k+1):
prevprofit = -prices[0]
for j in range(1, n):
prevprofit = max(prevprofit, dp[i-1][j] - prices[j])
dp[i][j] = max(dp[i][j-1], prevprofit + prices[j])
ret = max(ret, dp[i][j])
return ret
'Computer Science > Coding Test' 카테고리의 다른 글
프로그래머스: 가장 먼 노드 (0) | 2021.11.05 |
---|---|
LeetCode: 124. Binary Tree Maximum Path Sum (0) | 2021.11.04 |
LeetCode: 123. Best Time to Buy and Sell Stock III (0) | 2021.11.03 |
LeetCode: 122. Best Time to Buy and Sell Stock II (0) | 2021.11.03 |
LeetCode: 120. Triangle (0) | 2021.11.03 |