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 ...
随机推荐
- DSP连接不上CCS3.3的问题讨论
环境 操作系统:Win7, 64bit IDE:CCS V3.3 仿真器:SEED XDS510PLUS DSP型号:TMS320C6713GDP(DSP6713) 检查步骤 试着按下复位按键后再点击 ...
- Java设计模式菜鸟系列(九)外观模式建模与实现
转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39805735 外观模式(Facade):是为了解决类与类之间的依赖关系的,像spring一 ...
- urlrewrite 匹配规则之优先选择
urlrewrite rule可以使用java的正则表达式匹配规则,但是这里存在一个问题点,假如有一个通配的规则和一个精确匹配的规则,urlrewrite 会选择那个去匹配呢? 如下两种规则: < ...
- EasyUI - tab动态加载datagrid
addTab: function() { $("#myTabs").tabs('add', { title: 'my title', closable: true, tools: ...
- 解决 RichTextBox 文件格式不对问题
RichTextBox文件格式不对: 原因:富文本框的LoadFile方法只支持RTF格式的文件或者标准的ASCII文本本档,,我们一般的文本文档是ANSI或者UTF-8的格式,所以,报这个错. 解决 ...
- 闲扯 Javascript 04 滚动条
物体运动基础 让Div移动起来 offsetLeft的作用 用定时器让物体连续移动 效果原理 让ul一直向左移动 复制li innerHTML和+= 修改ul的width 滚动过界后,重设位置 判断过 ...
- [转]在linux下如何判断是否已经安装某个软件?软件安装在哪个目录
<1>在linux下如何判断是否已经安装某个软件? ++++++++++++++++++++++++++++++++++++++++++ rpm -qa|grep 软件包 ++++++++ ...
- armeabi与armeabi-v7a
原文http://blog.csdn.net/liminled/article/details/17030747 1.armeabi armeabi是指的该so库用于Arm的通用CPU. 2.arme ...
- 转:c语言EOF是什么?(及getchar()和putchar用法)
我学习C语言的时候,遇到的一个问题就是EOF. 它是end of file的缩写,表示"文字流"(stream)的结尾.这里的"文字流",可以是文件(file) ...
- 解决linux下javac -version和java -version版本显示不一致
解决linux下javac -version和java -version版本显示不一致 [javascript] view plaincopy [root@localhost usr]# $JAVA_ ...