티스토리 뷰
Algorithm/LeetCode Daily Challenge
188. Best Time to Buy and Sell Stock IV
Jaime.Lee 2022. 9. 13. 12:00반응형
Problem
You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k.
Find the maximum profit you can achieve. You may complete at most k transactions.
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
Example 1:
Input: k = 2, prices = [2,4,1]
Output: 2
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
Example 2:
Input: k = 2, prices = [3,2,6,5,0,3]
Output: 7
Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
Constraints:
- 0 <= k <= 100
- 0 <= prices.length <= 1000
- 0 <= prices[i] <= 1000
Solution
주식의 가격 배열과 최대 거래 횟수가 주어질 때 최대 이익을 구하는 문제입니다.
package io.lcalmsky.leetcode.best_time_to_buy_and_sell_stock_iv;
public class Solution {
public int maxProfit(int k, int[] prices) {
if (prices.length == 0) {
return 0;
}
int n = prices.length;
if (k >= n / 2) {
int maxProfit = 0;
for (int i = 1; i < n; i++) {
if (prices[i] > prices[i - 1]) {
maxProfit += prices[i] - prices[i - 1];
}
}
return maxProfit;
}
int[][] dp = new int[k + 1][prices.length];
for (int i = 1; i <= k; i++) {
int localMax = -prices[0];
for (int j = 1; j < prices.length; j++) {
dp[i][j] = Math.max(dp[i][j - 1], prices[j] + localMax);
localMax = Math.max(localMax, dp[i - 1][j] - prices[j]);
}
}
return dp[k][prices.length - 1];
}
}
k가 배열의 길이의 반 이상일 때는 이전 값보다 이후 값이 클 경우 무조건 거래를 했다고 가정하고 그 차이를 모두 더하면 간단히 풀 수 있습니다.
그렇지 않을 때는 dp를 이용해 풀이할 수 있습니다.
Test
package io.lcalmsky.leetcode.best_time_to_buy_and_sell_stock_iv;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class SolutionTest {
@Test
void testAll() {
assertAll(
() -> test(2, new int[]{2, 4, 1}, 2),
() -> test(2, new int[]{3, 2, 6, 5, 0, 3}, 7)
);
}
private void test(int k, int[] prices, int expected) {
// when
Solution solution = new Solution();
int actual = solution.maxProfit(k, prices);
// then
assertEquals(expected, actual);
}
}
반응형
'Algorithm > LeetCode Daily Challenge' 카테고리의 다른 글
1996. The Number of Weak Characters in the Game (0) | 2022.09.11 |
---|---|
94. Binary Tree Inorder Traversal (0) | 2022.09.10 |
144. Binary Tree Preorder Traversal (0) | 2022.09.09 |
814. Binary Tree Pruning (0) | 2022.09.08 |
429. N-ary Tree Level Order Traversal (0) | 2022.09.07 |
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- 226,133
- Today
- 34
- Yesterday
- 421
링크
TAG
- Spring Boot
- spring boot application
- r
- Spring Boot JPA
- 스프링 부트
- QueryDSL
- gRPC
- Jackson
- Spring Boot Tutorial
- Java
- 스프링 부트 튜토리얼
- spring boot app
- @ManyToOne
- 헥사고날 아키텍처
- 함께 자라기
- JSON
- 알고리즘
- 스프링 부트 회원 가입
- 클린 아키텍처
- 스프링 데이터 jpa
- spring boot jwt
- 함께 자라기 후기
- JPA
- Linux
- proto3
- 스프링부트
- intellij
- 스프링 부트 애플리케이션
- leetcode
- Spring Data JPA