描述:数组 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. java和c#md5加密不同

    java的mad5加密后为32位字符串,c#直接加密后可能不是32位,位数也不确定. 普通的写法 public static string Md5(string sourcein) { var md5 ...

  2. [React Native] State and Touch Events -- TextInput, TouchableHighLight

    In React, components manage their own state. In this lesson, we'll walk through building a component ...

  3. [TypeScript] 1. Catching JavaScript Mistakes with TypeScript

    The TypeScript compiler is a powerful tool which catches mistakes even in vanilla JavaScript. Try it ...

  4. nginx-systemtap-toolkit

    https://github.com/openresty/nginx-systemtap-toolkit

  5. 标准I/O库之标准I/O的效率

    程序清单5-1 用getc和putc将标准输入复制到标准输出 #include "apue.h" int main( void ) { int c; while(( c = get ...

  6. 模拟接听电话的方法,兼容华为android5.0以上设备

    /** * 通过反射调用的方法,接听电话,该方法只在android 2.3之前的系统上有效. * * @param context */private static void answerRingin ...

  7. 玩转Android之在线视频播放控件Vitamio的使用

    其实Android中自带的MediaPlayer本身就能播放在线视频,MediaPlayer结合SurfaceView播放在线视频也是不错的选择(如果你没有性能或者用户体验上的要求),关于MediaP ...

  8. 深入理解计算机系统第二版习题解答CSAPP 2.9

    基于三元色R(红)G(绿)B(蓝)关闭(0)和打开(1),能够创建8种不同的颜色,如下: R G B 颜色 R G B 颜色 0 0 0 黑色 1 0 0 红色 0 0 1 蓝色 1 0 1 红紫色 ...

  9. 移动端rem布局的适配mixin【转藏】

    /*================================================================ 以下为基于ip5 宽度320做的适配,标准html{font-si ...

  10. 【转】ArrayList的toArray

    [转]ArrayList的toArray ArrayList提供了一个将List转为数组的一个非常方便的方法toArray.toArray有两个重载的方法: 1.list.toArray(); 2.l ...