Leetcode 188. Best Time to Buy and Sell Stock IV
来源:互联网 发布:matlab定义数组 编辑:程序博客网 时间:2024/06/04 18:29
Leetcode 188. Best Time to Buy and Sell Stock IV
很久很久没有刷到这么好的题目了,很经典的DP题目,动态规划简而言之,就是要学会分情况讨论。
public class Solution {public static void main(String[] args){Solution s = new Solution();int[] k = {6,1,3,2,4,7};int r = s.maxProfit(2, k);System.out.println(r);} public int maxProfit(int k, int[] prices) { int len = prices.length; if(k>=len){ int res = 0; for(int i=1;i<len;i++){ if(prices[i]>prices[i-1]){ res += prices[i] - prices[i-1]; } } return res; } int[][] local = new int[len][k+1]; int[][] global = new int[len][k+1]; for(int i=1;i<len;i++){ int diff = prices[i] - prices[i-1]; int res = prices[i] > prices[i-1] ? diff:0; for(int j=1;j<=k;j++){ local[i][j] = global[i-1][j-1]+res>local[i-1][j]+diff?global[i-1][j-1]+res:local[i-1][j]+diff; global[i][j] = global[i-1][j]>local[i][j]?global[i-1][j]:local[i][j]; } } return global[len-1][k]; }}
阅读全文
0 0
- [leetcode] 188.Best Time to Buy and Sell Stock IV
- [leetcode] 188.Best Time to Buy and Sell Stock IV
- [leetcode] 188. Best Time to Buy and Sell Stock IV
- [LeetCode] 188. Best Time to Buy and Sell Stock IV
- Leetcode 188. Best Time to Buy and Sell Stock IV
- 【leetcode】188. Best Time to Buy and Sell Stock IV
- LeetCode 188. Best Time to Buy and Sell Stock IV
- Leetcode 188. Best Time to Buy and Sell Stock IV
- Leetcode 188. Best Time to Buy and Sell Stock IV
- Leetcode 188. Best Time to Buy and Sell Stock IV
- LeetCode 188. Best Time to Buy and Sell Stock IV
- [LeetCode] 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 -- leetcode
- 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
- spark--transform算子--mapPartitionsWithIndex
- Linux fork函数详解
- css3的简单动画效果(animation和transition)
- ngrok微信公众号本地开发
- UESTC 1703 一道更简单的字符串题 哈希+枚举
- Leetcode 188. Best Time to Buy and Sell Stock IV
- 一个错误
- java并发编程实战-避免活跃性危险
- 3532:最大上升子序列和
- JAVA 文档注释
- 异常处理
- 7-18 常用类下
- linux 下 mysql命令行
- 所有的惠普机器!区别对待