#Leet Code# Best Time to Buy and Sell Stock
描述:数组 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的更多相关文章
- [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 ...
- 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- ...
- [LeetCode] Best Time to Buy and Sell Stock III
将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...
- 【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 ...
- 【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 ...
- [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 ...
- 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 ...
- 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Linux内存管理学习笔记--物理内存分配
http://blog.chinaunix.net/uid-20321537-id-3466022.html
- 【转】GitHub删除一个仓库——2013-08-27 21
http://xiacijian.blog.163.com/blog/static/849931902012111195041170/ 1.进入要删除的仓库 2.找到 导航栏 Code NetWor ...
- [转]JavaScript作用域安全构造函数
构造函数其实就是一个使用new操作符调用的函数.当使用new调用时,构造函数内用到的this对象会对指向新创建的对象实例,如下的例子所示: function Person(name, ag ...
- JSTL-core核心代码标签库中的forEach,remove, forTokens,choose,when,otherwise,redirect 标签
<%@ page language="java" import="java.util.*, cn.hncu.domain.*" pageEncoding= ...
- VB学习笔记
stack segment stack 'stack' dw dup() ;此处输入堆栈段代码 stack ends data segment ;IBUF OBUF 看成是内存的地址,IBUF+1和I ...
- APUE(1)——UNIX基本概念
1.OS——操作系统是管理硬件资源的软件,也称作内核.与此同时,操作系统还为其他程序提供一系列的服务,比如执行程序.打开文件.读文件等等. 2.Kernel——内核对外提供一系列的系统调用,而一些库又 ...
- Java并发——线程池Executor框架
线程池 无限制的创建线程 若采用"为每个任务分配一个线程"的方式会存在一些缺陷,尤其是当需要创建大量线程时: 线程生命周期的开销非常高 资源消耗 稳定性 引入线程池 任务是一组逻辑 ...
- SharePoint 文档库实现文件夹拖放到文档库
打开文档库-> 选择文件夹-> 在Ribbon中选择“库(list)”-> 在右边可以看到打开方式-> 选择用资源管理器打开-> 在新打开的资源管理器中可能实现对文夹的拖 ...
- zzzzw_在线考试系统①准备篇
在弄完购物系统之后,小博也了解了解怎么用struts这个框架捣鼓一个在线考试系统 购物系统用的是MVC模式,现在这个struts2原理上也是基于MVC模式的.那么要做这个东西之前先了解一下难点在哪里 ...
- web开发学习之旅---html第二天
一.转义符 一些字符在html中拥有特殊的含义,比如小于号(<)用于定义 HTML 标签的开始.如果我们希望浏览器正确地显示这些字符,我们必须在 HTML 源码中插入转义符. 分类 二.html ...