leetcode 【 Best Time to Buy and Sell Stock 】python 实现
思路:
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.
代码:oj测试代码 Runtime: 111 ms
class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
# none case or one element case
if prices is None or len(prices)<2 :
return 0
# dp
min_buy = 0
max_profit = 0
for i in range(1,len(prices)):
if prices[i]<prices[min_buy]:
min_buy = i
max_profit = max(prices[i]-prices[min_buy],max_profit)
return max_profit
思路:
典型的动态规划题目。
跟求最小子集的和这道题(http://www.cnblogs.com/xbf9xbf/p/4240510.html)类似。
个人认为这类题目的精髓在于“在每一步,我们维护两个变量,一个是全局最优,就是到当前元素为止最优的解是,一个是局部最优,就是必须包含当前元素的最优的解”。
对于这道题来说,有一个小梗需要想明白:如果prices[i]就是到i为止最小的值,那么prices[i]-prices[min_buy]不是等于0了么?
请注意,回顾动态规划的规则:一定要包含当前元素的最优解。既然当前元素已经是历史最小值了,那么还非要包含当前元素的最优解,不正是他自身减去自身么?
维护一个max_profit(最大利润),一个min_buy(历史最低点)。
1. 如果当前的prices[i]小于prices[min_buy],则更新min_buy。
2. 取prices[i]-prices[min_buy]和max_profit中较大的一个,作为max_profit的值。
最后返回max_profit即可。
leetcode 【 Best Time to Buy and Sell Stock 】python 实现的更多相关文章
- [leetcode]Best Time to Buy and Sell Stock @ Python
原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/ 题意: Say you have an array for ...
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
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 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 ...
- [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
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 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- LeetCode Best Time to Buy and Sell Stock IV
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...
- [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费
Your are given an array of integers prices, for which the i-th element is the price of a given stock ...
随机推荐
- Invoke 和 BeginInvoke 的区别(转发)
在Invoke或者BeginInvoke的使用中无一例外地使用了委托Delegate. 一.为什么Control类提供了Invoke和BeginInvoke机制? 关于这个问题的最主要的原因已经是do ...
- 红象云腾CRH 一键部署大数据平台
平台: arm 类型: ARM 模板 软件包: azkaban hadoop 2.6 hbase hive kafka spark zeppelin azkaban basic software bi ...
- 网络编程——基于UDP的网络化CPU性能检测
网络化计算机性能检测软件的开发,可对指定目标主机的CPU利用率进行远程检测,并自动对远程主机执行性能指标进行周期性检测,最终实现图形化显示检测结果. 网络通信模块:(客户端类似,因为udp是对等通信) ...
- pat乙级1034
1.vs2013不能用scanf,改为scanf_s,但是提交时不能用scanf_s,用scanf... scanf_s(], &a[], &b[], &b[]); 2.c++ ...
- POJ-3436 ACM Computer Factory---最大流+拆点
题目链接: https://vjudge.net/problem/POJ-3436 题目大意: 每台电脑有p个组成部分,有n个工厂加工电脑.每个工厂对于进入工厂的半成品的每个组成部分都有要求,由p个数 ...
- 【BZOJ3940】[USACO2015 Feb] Censoring (AC自动机的小应用)
点此看题面 大致题意: 给你一个文本串和\(N\)个模式串,要你将每一个模式串从文本串中删去.(此题是[BZOJ3942][Usaco2015 Feb]Censoring的升级版) \(AC\)自动机 ...
- CDQ分治入门
前言 \(CDQ\)分治是一个神奇的算法. 它有着广泛的用途,甚至在某些题目中还能取代\(KD-Tree\).树套树等恶心的数据结构成为正解,而且常数还小得多. 不过它也有一定的缺点,如必须离线操作, ...
- 第七章 动态创建HTML内容
javascript也可以改变网页的结构和内容 document.write()方法 可以方便快捷地把字符串插入到文档里 document.write("<strong>hell ...
- getchar输入多行字符,原格式输出(包含换行符)
#include<stdio.h> int main() { FILE fp; ]; ; char ch; while((ch=getchar())!=EOF){ str[k++]=ch; ...
- python 实现无序列表
# -*- coding:utf-8 -*- class Node: def __init__(self, initdata): self.data = initdata self.next = No ...