123. Best Time to Buy and Sell Stock III(dp)
来源:互联网 发布:织梦小说源码 编辑:程序博客网 时间:2024/06/05 14:54
题目:
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 two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
设状态f(i)表示区间[0,i-1]上的最大利润,设置状态g(i),表示区间[i,n-1]上最大利润。则最大利润为max{f(i)+g(i)};
public class Solution { public int maxProfit(int[] prices) { if(prices == null || prices.length<2)return 0; int max = 0; int n = prices.length; int[] l = new int[n]; int[] r = new int[n]; int min = prices[0]; for(int i=1;i<n;i++) { min = Math.min(prices[i], min); l[i]=Math.max(prices[i]-min, l[i-1]); } int maxx = prices[n-1]; for(int i=n-2;i>=0;i--) { maxx= Math.max(prices[i], maxx); r[i]=Math.max(maxx-prices[i], r[i+1]); } for(int i=0;i<n;i++) { max = Math.max(max, r[i]+l[i]); } return max; }}
0 0
- 【数组&DP】Best Time to Buy and Sell Stock III
- 【Leetcode】Best Time to Buy and Sell Stock III (DP)
- [leetcode][DP] Best Time to Buy and Sell Stock III
- [leetcode][DP] Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock III(dp)
- leetcode123. [DP]Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III(dp)
- LeetCode 123. Best Time to Buy and Sell Stock III
- 123.Best Time to Buy and Sell Stock III
- [Leetcode]123.Best Time to Buy and Sell Stock III
- [Leetcode] 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- [leetcode] 123.Best Time to Buy and Sell Stock III
- 【leetcode】123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- [leetcode] 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- 123. Best Time to Buy and Sell Stock III
- Unity3D follow.
- iOS APP图标一键生成
- lua定时器与定时任务的接口设计
- 使用随机算法产生一个数,要求把1-1000W之间这些数全部生成。
- 15章 上机1
- 123. Best Time to Buy and Sell Stock III(dp)
- sha1签名
- 15章 上机4商品批发显示总金额
- [深度学习论文笔记][Weight Initialization] Delving deep into rectifiers: Surpassing human-level performance
- 如何判断用户是拒绝还是允许通讯录权限?
- java练习--判断字符出现次数
- 遍历map的四种方式
- android 图片 色彩 Bitmap.Config RGB_565 ARGB_8888
- php中使用redis HyperLogLogs