#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 ...
随机推荐
- ios8 关于UIAlertController 代替UIActionsheet
self.alertController=[UIAlertController alertControllerWithTitle:@"请选择\n\n\n\n\n\n\n\n\n\n\n\n\ ...
- 有关UIImageView+AFNetworking 下载图片的线程问题
今天写了一个demo,从服务器获取图片,然后显示在cell上,大家都知道cell的重用机制,当往下拉的时候,上面的cell遮住了,下面的cell就会重用被遮住的cell, 贴代码: NSString ...
- [转]非常好的vsftpd安装于配置
环境:CentOS 5.0 操作系统一.安装:1.安装Vsftpd服务相关部件:[root@KcentOS5 ~]# yum install vsftpd*Dependencies Resolved= ...
- 【17】以独立语句将newed对象置入智能指针
1.为什么? 考虑下面的情况:方法声明为void processWidget(shared_ptr<Widget> pw,int priority). 调用方法 processWidget ...
- SpringMVC请求分发的简单实现
简介 以前用了下SpringMVC感觉挺不错了,前段事件也简单了写了一些代码来实现了SpringMVC简单的请求分发功能,实现的主要思想如下: 将处理请求的类在系统启动的时候加载起来,相当于S ...
- [欧拉] poj 2230 Watchcow
主题链接: http://poj.org/problem? id=2230 Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submi ...
- [linux]date命令时间戳和时间之间的转换
非常多时候我们查看数据库的数据,或者是一些别人系统中的数据须要用时间戳来查询.或者查询出来的结果是个时间戳. 还有时候,查询条件须要输入时间戳. 我之前的办法就是用在线工具来完毕,后来用mac了.我觉 ...
- Linux 释放cached内存
使用free -m 查看系统使用的内存情况: # free -m total used free shared buffers ...
- 【邮件】imap与pop3的区别
文:铁乐猫 2015 10月14日 今天替一位在外出差的用户安装和设置完foxmail用于收发邮件,到下午被告知对方用foxmail发完邮件后,在网页上登录邮箱后并没有看到在foxmail中" ...
- MySQL数据库分表的3种方法
原文地址:MySQL数据库分表的3种方法作者:dreamboycx 一,先说一下为什么要分表 当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了.分表的目 ...