188. Best Time to Buy and Sell Stock IV——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most k transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Example 1:
Input: [2,4,1], k = 2
Output: 2
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
Example 2:
Input: [3,2,6,5,0,3], k = 2
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.
题目大意:最多k次交易,问可以达到的最大获利。
思路:dp, 递推式:
dp[i, j] represents the max profit up until prices[j] using at most i transactions.
dp[i, j] = max(dp[i, j-1], prices[j] - prices[jj] + dp[i-1, jj]) { jj in range of [0, j-1] }
= max(dp[i, j-1], prices[j] + max(dp[i-1, jj] - prices[jj]))
dp[i, j] 表示最多i次交易时,价格为prices[j]时的最大获利。
核心代码如下:
int[][] dp = new int[k+1][n];
for (int i = 1; i <= k; i++) {
int localMax = dp[i-1][0] - prices[0];
for (int j = 1; j < n; j++) {
dp[i][j] = Math.max(dp[i][j-1], prices[j] + localMax);
localMax = Math.max(localMax, dp[i-1][j] - prices[j]);
}
}
188. Best Time to Buy and Sell Stock IV——LeetCode的更多相关文章
- 188. Best Time to Buy and Sell Stock IV leetcode解题笔记
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【刷题-LeetCode】188 Best Time to Buy and Sell Stock IV
Best Time to Buy and Sell Stock IV Say you have an array for which the i-th element is the price of ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 188. Best Time to Buy and Sell Stock IV (Array; DP)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 188. Best Time to Buy and Sell Stock IV
题目: 链接: 题解: 测试: Reference:
- 188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV
假设你有一个数组,其中第 i 个元素是第 i 天给定股票的价格.设计一个算法来找到最大的利润.您最多可以完成 k 笔交易.注意:你不可以同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 详见: ...
随机推荐
- python词频统计
1.jieba 库 -中文分词库 words = jieba.lcut(str) --->列表,词语 count = {} for word in words: if len(word)==1 ...
- Jquery-ajax()方法提交json数据
1.ajax()提交json数据代码 var strJson = getStrPayJson(); $.ajax({ type: "POST", url: "/userc ...
- 在 Azure Web 应用中创建 Java 应用程序
本分步指南将通过 Azure Web 应用帮助您启动并运行示例 Java 应用程序.除 Java 外,Azure Web 应用还支持其他语言,如 PHP..NET.Node.JS.Python.Rub ...
- TensorFlow入门:mac 安装 TensorFlow
开发环境: mac os 10.12.5 Python 2.7.10 GCC 4.2.1 mac默认是不带pip的,安装pip. sudo easy_install pip 1.安装virtualen ...
- Csharp:操作存儲過程輸出參數,和返回值
/// <summary> ///塗聚文 存儲過程輸出參數,和返回值 /// 20131030 /// Geovin Du /// </summary> /// <par ...
- 【Android】9.0活动的生命周期(二)——实际代码演示
1.0 新建一个项目ActivityLifeCycleTest. 新建两个活动NormalActivity和DialogActivity,勾选“Generate Layout File”. 文件目录 ...
- 隐藏 google 地图 Logo 隐藏 百度 地图 Logo
Google 地图 var isFirstLoad=true; //地图瓦片加载完成之后的回调 google.maps.event.addListener(map, 'tilesloaded', fu ...
- java实现12306的45分钟内支付,45分钟后取消订单功能?
java实现12306的45分钟内支付,45分钟后取消订单功能? - 回答作者: 匿名用户 https://zhihu.com/question/27254071/answer/35948645
- C#实现文件异步上传
//前台方法,包含弹出框确认以及文件选择<input type="button" id="importxlsx" name="importxls ...
- day5-基础 函数
函数 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,具体区别,我们后面会讲,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做subroutine(子过程或子程序 ...