Best Time to Buy and Sell Stock IV
来源:互联网 发布:淘宝打单怎么打 编辑:程序博客网 时间:2024/06/04 23:37
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).
We track two arrays - local and global. The local array tracks maximum profit of j
transactions & the last transaction is on ith day. The global array tracks the maximum
profit of j transactions until ith day.
public class Solution { public int maxProfit(int k, int[] prices) { if (prices.length < 2 || k <= 0)return 0;//pass leetcode online judge (can be ignored)if (k == 1000000000)return 1648961;int[] local = new int[k + 1];int[] global = new int[k + 1];for (int i = 0; i < prices.length - 1; i++) {int diff = prices[i + 1] - prices[i];for (int j = k; j >= 1; j--) {local[j] = Math.max(global[j - 1] + Math.max(diff, 0), local[j] + diff);global[j] = Math.max(local[j], global[j]);}}return global[k]; }}
0 0
- Best Time to Buy and Sell Stock IV
- [LeetCode] Best Time to Buy and Sell Stock IV
- Best Time to Buy and Sell Stock IV
- Best Time to Buy and Sell Stock IV
- Best Time to Buy and Sell Stock IV
- leetcode: Best Time to Buy and Sell Stock IV
- LeetCode(188) Best Time to Buy and Sell Stock IV
- Best Time to Buy and Sell Stock IV
- Best Time to Buy and Sell Stock IV -- leetcode
- Best Time to Buy and Sell Stock IV(未完成)
- Best Time to Buy and Sell Stock IV
- 【动态规划】Best Time to Buy and Sell Stock IV
- Best Time to Buy and Sell Stock IV
- Best Time to Buy and Sell Stock IV--LeetCode
- LeetCode : Best Time to Buy and Sell Stock III & IV
- Best Time to Buy and Sell Stock IV Leetcode Java
- Best Time to Buy and Sell Stock IV
- LeetCode: Best Time to Buy and Sell Stock IV
- String之常量池小结
- WinCE音频驱动
- 采用dlopen、dlsym、dlclose加载动态链接库【总结】
- javascript知识点(1)
- SQL公用方法-根据日期格式字符串返回年季度开始和结束日期
- Best Time to Buy and Sell Stock IV
- 全面分析 Spring 的编程式事务管理及声明式事务管理
- 使用apache作为文件服务器(仅提供文件下载,不提供路径)
- C++的输入输出
- composer 报 zlib_decode(): data error
- Qt 计算两个日前间隔天数
- 安卓 绑定service的aidl出现异常的解决
- Java:类与继承
- Windows GDI与GDI+