Best Time to Buy and Sell Stock

Total Accepted: 14044 Total
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 II (贪心策略,差分序列)

LeetCode--Best Time to Buy and Sell Stock III (动态规划)

LeetCode--Best Time to Buy and Sell Stock (贪心策略 or 动态规划)的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. mysqli_set_charset和SET NAMES优劣分析

    bool mysqli_set_charset ( mysqli $link , string $charset ) 这应该是首选的用于改变字符编码的方法,不建议使用 mysqli_query()执行 ...

  2. Android 改变窗口标题栏的布局

    Android改变窗口标题栏的布局  第一种方式 --在XML文件里面引入配置文件作为标题. 第二种方式  --动态的代码加入进去. 第三种方式(网上的): 一. 重点 一般应用的Title都是建立应 ...

  3. 一起学习CMake – 03

    这一节我们就一起来看看如何用CMake来链接自己写的lib库,如何进行这些库文件的管理. 一个团队共同开发软件时,一般都是分模块进行作业的,每个人负责整个软件中的一部分,然后再整合成一个完整的软件系统 ...

  4. ADB logcat 过滤方法(抓取日志)

    1. Log信息级别 Log.v- VERBOSE  : 黑色 Log.d- DEBUG  : 蓝色 Log.i- INFO  : 绿色 Log.w- WARN  : 橙色 Log.e- ERROR ...

  5. BZOJ 1196: [HNOI2006]公路修建问题( MST )

    水题... 容易发现花费最大最小即是求 MST 将每条边拆成一级 , 二级两条 , 然后跑 MST . 跑 MST 时 , 要先加 k 条一级road , 保证满足题意 , 然后再跑普通的 MST . ...

  6. uva 10313 Pay the Price(完全背包)

    题目连接:10313 - Pay the Price 题目大意:有0~300这300种价值的金额. 现在可能给出参数: 1个:n, 输出可以组成价值n的方式的个数. 2个:n, a输出用个数小于a的价 ...

  7. 每天4个linux命令--步骤一

     1 :Linux的诞生 Linux由芬兰赫尔辛基大学的Linus Torvalds创建 1991年10月,Linux第一个公开版 0.02版发布 1994年3月,Linux 1.0版发布 Linus ...

  8. PHP脚本监控程序

    #!/bin/sh # Find ip IP=`/sbin/ifconfig eth1 | grep 'inet addr' | awk '{ print substr($2, index($2, & ...

  9. Unity3d 帧率设置 及在游戏执行时显示帧率

    在Unity3d 中能够通过代码设置 来限定游戏帧率. Application.targetFrameRate=-1; 设置为 -1 表示不限定帧率. 转自http://blog.csdn.net/h ...

  10. Google - Pagerank

    词条权值的局限. 上一篇blog以信息和概率的角度探讨了词条对于文档的权值. 见blog:http://blog.csdn.net/ice110956/article/details/17243071 ...