描述:数组 A,对于 i < j, 找到最大的 A[j] - A[i]

代码:

 class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
if len(prices) == 0 or len(prices) == 1:
return 0 cur_min = prices[0]
max_minus = 0 for i in range(1, len(prices)):
if prices[i] < cur_min:
cur_min = prices[i]
else:
tmp = prices[i] - cur_min
if tmp > max_minus:
max_minus = tmp return max_minus

动态规划:

设dp[i]是[0,1,2...i]区间的最大利润,则该问题的一维动态规划方程如下

dp[i+1] = max{dp[i], prices[i+1] - minprices}  ,minprices是区间[0,1,2...,i]内的最低价格

最大利润 = max{dp[0], dp[1], dp[2], ..., dp[n-1]}

其他思路:

按照股票差价构成新数组 prices[1]-prices[0], prices[2]-prices[1], prices[3]-prices[2], ..., prices[n-1]-prices[n-2],求新数组的最大子段和就是最大利润

参考地址:

http://www.cnblogs.com/TenosDoIt/p/3436457.html

#Leet Code# Best Time to Buy and Sell Stock的更多相关文章

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

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

  3. 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...

  4. [LeetCode] Best Time to Buy and Sell Stock III

    将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...

  5. 【LeetCode OJ】Best Time to Buy and Sell Stock III

    Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ Linear Time Solut ...

  6. 【leetcode】121-Best Time to Buy and Sell Stock

    problem 121. Best Time to Buy and Sell Stock code class Solution { public: int maxProfit(vector<i ...

  7. [Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机

    Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...

  8. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

  9. 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. C socket demo

    一.服务端-server.c #include <stdio.h> #include <sys/socket.h> #include <arpa/inet.h> # ...

  2. 大型网站都喜欢把js写在html中的真正原因

    相信经常观察大站的朋友都会发现,他们都把CSS写在HTML页面里,一个页面的或者多个页面的背景图片,都集成到一张图片里,他们有的JS文件,也写到页面里了……也许你会迷惑,现在到处讲页面的优化,不都是要 ...

  3. LINUX系统镜像下载总汇

    LINUX系统fedora.centos.debian.ubuntu ISO下载汇总 http://mirrors.sohu.com http://linux.ilvgo.cn/ ftp://gent ...

  4. BigInteger和BigDecimal大数相加问题

    package cn.hncu.big; import java.math.BigDecimal; public class BigDecimalDemo { public static void m ...

  5. Eclipse启动Tomcat访问不了首页

    Eclipse开发web项目与myEclipse不同: 启动服务器后访问 http:localhost:8080 找不到服务器 想要访问Tomcat首页只需修改Tomcat配置 进入Eclipse双击 ...

  6. (转)asp.net 高质量缩略图

    原文地址:http://www.cnblogs.com/Fooo/archive/2009/06/19/1506381.html using System.Drawing; ------------- ...

  7. 时空分割的画面--用xcode命令行回忆turbo c

    大学时期曾经玩过turbo c的同学,可以用xcode命令行写写c程序,回味一下吧:) 1. 首先在终端输入,touch main.c 新建文件 2. 编辑main.c内容,写一段简单代码 #incl ...

  8. jsonp调用webapi和mvc

    webapi代码如下: public string Get(int id) { var callback = HttpContext.Current.Request["callback&qu ...

  9. SDL实现限制帧速

    很多人都在SDL_PollEvent和SDL_WaitEvent之间纠结.前者会带来更好的帧数表现,但是CPU占用极大,可以直接吃掉一个核心.后者则基本不占用CPU,但是帧数会受到影响.有没有办法使两 ...

  10. myeclipse启动报JVM terminated. Exit code=1

    报错信息如图: 解决办法: 删除当前workspaces下文件夹,路径为:%Workspaces%/.metadata/.plugins/org.eclipse.core.runtime