LeetCode--Best Time to Buy and Sell Stock (贪心策略 or 动态规划)
Best Time to Buy and Sell Stock
Submissions: 45572My Submissions
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),
design an algorithm to find the maximum profit.
Have you been asked this question in an interview?
解题思路:
贪心法:
分别找到价格最低和最高的一天,低进高出,注意最低的一天要在最高的一天之前。
把原始价格序列变成差分序列,也就是对于元素j,在该点卖出的价格等于prices[j] 减去它前面元素中最小的值。这样遍历一遍数组就OK了。
本题也能够做是最大m 子段和,m = 1 。
动态规划法:
首先用prices[i] - prices[i-1] 将数组转化为 差分序列,那么问题就转化为在这个差分序列中找出一个最大( maximum )的连续的子序列和的问题,能够用LeetCode--Maximum Subarray 最大连续子序列和 (动态规划)类似方法求解。
备注:若採用暴利破解法,时间复杂度是O(n^2),会超出时间限制。
程序源码:
贪心算法Java代码
public int maxProfit(int[] prices) {
if(prices.length ==0)
return 0;
int i=0;
int profit=0;
int begMin= prices[0];
for(i=1; i<prices.length; ++i){
profit=Math.max(profit, prices[i]-begMin);
begMin=Math.min(begMin, prices[i]);
}
return profit;
}
动态规划:java代码
public int maxProfit(int[] prices) {
int profit=0;
int temp=0;
for(int i=1; i<prices.length; ++i){
int diff=prices[i] - prices[i-1];
temp=Math.max(temp+diff, diff);
profit=Math.max(profit, temp);
}
return profit;
}
暴利破解法:
/**
* 暴利枚举法,超出时间限制
* @param prices
* @return
*/
public static int maxProfit2(int[] prices) {
int profit= 0;
for( int i=0; i<prices.length-1; ++i){
for (int j=i+1; j<prices.length; ++j){
profit = Math.max(prices[j]-prices[i], profit);
}
}
return profit;
}
附录:
相关题目:
LeetCode--Best Time to Buy and Sell Stock (贪心策略 or 动态规划)的更多相关文章
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock 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] 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] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- LeetCode Best Time to Buy and Sell Stock IV
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...
- [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费
Your are given an array of integers prices, for which the i-th element is the price of a given stock ...
随机推荐
- Qt Chart 5.7.0 傻瓜安装教程
前提 qtchart 里的README文件(注意红色标记处)(本人翻译不行.多多见谅,也可以在评论里纠正( ⊙ o ⊙ )) --------------- Qt Charts 5.7.0 ----- ...
- cmake 学习笔记(一)
最大的Qt4程序群(KDE4)采用cmake作为构建系统 Qt4的python绑定(pyside)采用了cmake作为构建系统 开源的图像处理库 opencv 采用cmake 作为构建系统 ... 看 ...
- UVa 121 - Pipe Fitters
称号:放置在一个圆中的矩形,它要求每个圆的每行或列是切线,问:多少能竖起来. 分析:计算几何.数论.首先计算矩形显示屏,然后计算互显示器(每一行与相邻行相同差1个月)求最大,你可以. 说明:╮(╯▽╰ ...
- Wakelock API详解
官方资料 http://developer.android.com/intl/zh-CN/reference/android/os/PowerManager.WakeLock.html http:/ ...
- C#验证码的另一种操作方法
sb = new StringBuilder(); char c = '0'; string s = ""; for (int i = 0; i < 4; i++) { Ra ...
- SUSAN检测算子
USAN区域(核同值区):和核像素的灰度相同会相信的模板像素的区域. 利用这个区域的尺寸.重心.二阶矩等可以帮助检测图像的边缘和角点.利用USAN的面积作为特征可以起到增强边缘和角点的效果. 该方法不 ...
- SecureCRT辅助解决方案
SecureCRT辅助解决方案 1. 下载SecureCRT 7.3版本并激活: 2. SecureCRT linux配色方案: 3. SecureCRT设置log保存方案: 1. secureCRT ...
- iphone开发,模拟器生成文件路径
原文链接:http://blog.csdn.net/gf771115/article/details/7722023 Finder---位置----apple(用户名)---application s ...
- Qt 状态机框架学习(没学会)
Qt状态机框架是基于状态图XML(SCXML) 实现的.从Qt4.6开始,它已经是QtCore模块的一部分.尽管它本身是蛮复杂的一套东西,但经过和Qt的事件系统(event system).信号槽(s ...
- java操作Excel处理数字类型的精度损失问题验证
java操作Excel处理数字类型的精度损失问题验证: 场景: CELL_TYPE_NUMERIC-->CELL_TYPE_STRING--->CELL_TYPE_NUMERIC POI版 ...