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 ...
 
随机推荐
- Python函数式编程:Lambda表达式
			
首先我们要明白在编程语言中,表达式和语句的区别. 表达式是一个由变量.常量.有返回值的函数加运算符组成的一个式子,该式子是有返回值的 ,如 a + 1 就是个表达式, 单独的一个常量.变量 或函数调 ...
 - php抓取ajax页面返回图片。
			
要抓取的页面:http://pic.hao123.com/ 当我们往下滚动的时候,图片是用ajax来动态获取的.这就需要我们仔细分析页面了. 可以看到,异步加载的ajax文件为: http://pic ...
 - 转:javascript面向对象编程
			
作者: 阮一峰 日期: 2010年5月17日 学习Javascript,最难的地方是什么? 我觉得,Object(对象)最难.因为Javascript的Object模型很独特,和其他语言都不一样,初学 ...
 - 2、Zookeeper集群搭建、命令行Client操作
			
zookeeper 集群最好是奇数台: 5台允许挂掉2台 4台只能允许挂掉1台 zjtest7-redis:/opt/zookeeper/bin# ./zkServer.sh status ZooKe ...
 - Android灭亡论之Firefox OS操作系统出现
			
今天是2014年7月1日,过几天就要到深圳实训去了,实训核心内容是Android开发.尽管Android现在很火,但作为程序猿的我们必须时刻保持清醒的头脑.我虽不是什么预言家,但近期接触的Androi ...
 - 二路单调自增子序列模型【acdream 1216】
			
题目:acdream 1216 Beautiful People 题意:每一个人有两个值,能力值和潜力值,然后要求一个人的这两个值都严格大于第二个人的时候,这两个人才干呆在一块儿,给出很多人的值,求最 ...
 - CodeForces 191C 树链剖分 第4遍
			
非常无奈,模板重新无奈的打错了.. 只是,非常快便找到了.. 题意:给一些边,有一些操作,每次操作,都要在这些边上加上1,求每一个边的边权.. #include<cstdio> #incl ...
 - VS2013配置opencv3.0.0 (win8.1)
			
今天下载了最新版本的opencv3.0.0,之前一直是opencv2.4.8 点击.exe文件,我将解压后的文件夹放在D:\盘,取名opencv30,D:\opencv30 添加环境变量:D:\ope ...
 - 功能间(两个form)数据交互的编程方法
			
功能间数据交互的编程方法 现在框架具有在两个打开的功能之间进行通讯的机制.通讯是指,一个功能调用另外一个功能的方法,或者传递一些数据,并得到返回结果.比如处置单打开结算单,结算单保存后,将结算单号反填 ...
 - HDOJ 3047 带权并查集
			
解题思路转自: http://blog.csdn.net/azheng51714/article/details/8500459 http://blog.csdn.net/acresume/artic ...