题目:

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.

题目大意:

给定一个数组,里面的值表示一支股票每天的价格, 如第i天价格为prices[i],假设只能进行一次买进,一次卖出,请找出最大利润值。


解:

以prices[i]表示第i天股票价格,profit[i]表示到第i天为止所能获取的最大利润。

考虑在第n天卖出股票的最大利润 profit[n]=max(profit[n-1],  prices[n]-min), 其中min为前n-1天的股票最低价。

可见我们必须在遍历数组的过程中记录最小值。

而且profit只跟前一个状态有关,所以只需用一个值来记录,而不用数组。


Java代码:

public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0)
return 0;
int min = prices[0];
int profit = 0;
for(int i = 1; i < prices.length; i++){
min = prices[i] < min ? prices[i] : min;
profit = prices[i] - min > profit ? prices[i] - min : profit;
}
return profit;
}
}

Python代码:

class Solution:
# @param {integer[]} prices
# @return {integer}
def maxProfit(self, prices):
if not prices:
return 0
tmin = prices[0]
profit = 0
for each in prices[1:]:
profit = max(profit, each-tmin)
tmin = min(each, tmin)
return profit

(DP)Best Time to Buy and Sell Stock的更多相关文章

  1. [LeetCode] Best Time to Buy and Sell Stock 6道合集【DP】

    1. Best Time to Buy and Sell Stock 2. Best Time to Buy and Sell Stock II 3. Best Time to Buy and Sel ...

  2. Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock)

    Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock) 股票问题: 121. 买卖股票的最佳时机 122. 买卖股票的最 ...

  3. Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)

    Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...

  4. Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)

    Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...

  5. Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV)

    Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV) 股票问题: 121. 买卖股票的最佳时机 122. ...

  6. Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown)

    Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown) 股票问题: 121. 买卖股票 ...

  7. Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee)

    Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee) 股票问题: 1 ...

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

  9. LeetCode123: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 ...

随机推荐

  1. 有关JAVA基础学习中的集合讨论

        很高兴能在这里认识大家,我也是刚刚接触后端开发的学习者,相信很多朋友在学习中都会遇到很多头疼的问题,希望我们都能够把问题分享出来,把自己的学习思路整理出来,我们一起探讨一起成长.    今天我 ...

  2. Android 避免APP启动闪黑屏的解决办法(Theme和Style)

    前几天Boss就反应说,机器每次启动程序都会闪一下黑屏,这个客户不接受.没办法,只能想想怎么解决,最后找到了下面的方法.闪黑屏的原因主要是我们启动Activity的时候,需要跑完onCreate和on ...

  3. NVL函数(NVL,NVL2,NULLIF,COALESCE)

    NVL 语法:NVL( expr1, expr2) 功能:如果expr1为NULL,则NVL函数返回expr2的值,否则返回expr1的值,如果两个参数的都为NULL ,则返回NULL. 注意事项:e ...

  4. <.net>委托初探

    最近在学<.net深入体验与实战精要>. 今天就来初步讲解下委托. 一句话:委托定义了方法类型,可以将方法当做另一个方法的参数进行传递.委托包涵的只是方法的地址,而不是数据.类似于c指针. ...

  5. 注意:"AspNetPager”的控件“AspNetPager1”必须放在具有 runat=server 的窗体标记内

    应加: <form id="form1" runat="server"> </form> 否则一开始什么也不显示,页面控件看不见,加上a ...

  6. Django REST Framework学习——Android使用REST方法访问Diango

    本文更应该叫做Android如何模拟浏览器访问Django服务器后台. 环境为: Android通过HttpClient访问服务器,从Django中获取json数据,解析显示在UI界面上. 问题为: ...

  7. Arc engine - Geodatabase.

    Geodatabase以层次结构的数据对象来组织地理数据. 这些数据对象存储在要素类(Feature Classes).对象类(0bject classes)和数据集(Feature datasets ...

  8. Linux_X64安装Jboss

    一.系统环境 系统平台:RedHat 6.4 Java version:jdk-6u45-linux-x64 Jboss version:jboss-5.1.0.GA-jdk6   二.下载jdk1. ...

  9. Solr中schema.xml的解释

    接Solr-4.10.2与Tomcat整合.schema.xml位于D:\solr\data\solr\collection1\conf\中.1.fieldType节点    name: FieldT ...

  10. 0301——SearchController

    创建显示的页面 SearchViewController * searchVC = [[SearchViewController alloc]init]; 告诉搜索控制器将结果显示在创建的页面上 se ...