Leetcode_123_Best Time to Buy and Sell Stock III
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43740415
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).
思路:
(1)题意为给定一个数组,数组中第i个元素的值对应着第i天的股票,最多只能进行两次交易,每次交易只能买入一次并卖出,求能得到的最大利润。该题为Best Time to Buy and Sell Stock和Best Time to Buy and SellStockⅡ的加强版。
/*** @author liqq*/
public class Solution {public int maxProfit(int[] x) {if (x == null || x.length <= 1)return 0;int[] right = new int[x.length];int[] left = new int[x.length];int rmin = x[0];for (int i = 1; i < x.length; i++) {rmin = Math.min(rmin, x[i]);right[i] = Math.max(right[i - 1], x[i] - rmin);}int lmax = x[x.length - 1];left[x.length - 1] = 0;for (int i = x.length - 2; i >= 0; i--) {lmax = Math.max(lmax, x[i]);left[i] = Math.max(left[i + 1], lmax - x[i]);}int sum = 0;for (int i = 0; i < x.length; i++) {sum = Math.max(sum, right[i] + left[i]);}return sum;}
}
Leetcode_123_Best Time to Buy and Sell Stock III相关推荐
- 【DP + 卖股票】LeetCode 123. Best Time to Buy and Sell Stock III
LeetCode 123. Best Time to Buy and Sell Stock III Solution1: 不得不让人感叹算法之精妙啊!!! 参考网址:[1]http://www.cnb ...
- Leetcode Best Time to Buy and Sell Stock III
Leetcode Best Time to Buy and Sell Stock III,本算法的关键为找出其动态子结构.可以发现,序列中的最小值可以做为其的一个分割,令左边序列为left,右边的序列 ...
- [LeetCOde][Java] Best Time to Buy and Sell Stock III
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- Best Time to Buy and Sell Stock III
解题思路来自:https://blog.csdn.net/u012501459/article/details/46514309 Say you have an array for which the ...
- 最多两次股票交易-Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode OJ - Best Time to Buy and Sell Stock III
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- Best Time to Buy and Sell Stock III O(n) 求解方法
leetcode的题目:http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ leetcode的题目都很简练,但是很 ...
- 【Best Time to Buy and Sell Stock III 】cpp
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- leetcode 123. Best Time to Buy and Sell Stock III | 123. 买卖股票的最佳时机 III(总结DP 模型套路)
题目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/ DP 模型套路 DP 套路之:暴力递 ...
最新文章
- hung-yi lee_p11_逻辑回归
- OKR 怎么突然火起来了?
- C++实现树的建立,查找,遍历输出
- springboot的jsp应该放在哪_详解SpringBoot 添加对JSP的支持(附常见坑点)
- 01.Python基础_菜单_快捷键_基本语法_变量_输入输出
- 操作系统之计算机系统概述:5、中断和异常
- Windows10远程桌面连接提示:出现身份验证错误,要求的函数不受支持
- C语言课后习题(11)
- java的scanner使用步骤
- mvc core2.1 Identity.EntityFramework Core 导航状态栏(六)
- pbrt源码中用全主元消去法求矩阵逆的实现
- FreeCAD 扫掠空间曲线
- IT运营新世界大会:广通软件开启双态运维大时代
- 深层和浅层卷积_浅层vs深层javascript
- [转载]1986年吴图南 马岳梁 吴英华 孙剑云等名家大師
- 请不要在“微服务”的狂热中迷失自我!
- php配置设置时区,php如何设置时区
- 网络编程:Reactor与Proactor的概念
- 怎样把内网IP映射外网
- 解决光影精灵锁win键的问题