121. Best Time to Buy and Sell Stock(股票最大收益)
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.
Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5 max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1]
Output: 0 In this case, no transaction is done, i.e. max profit = 0.
暴力超时:
class Solution:
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
n = len(prices)
if n==0:
return 0
max_diff = 0
for i in range(n):
for j in range(i,n):
diff = prices[j]-prices[i]
if diff>max_diff:
max_diff = diff return max_diff
只需要找出最大的差值即可,即 max(prices[j] – prices[i]) ,i < j。一次遍历即可,在遍历的时间用遍历low记录 prices[o….i] 中的最小值,就是当前为止的最低售价,时间复杂度为 O(n)。
class Solution:
def maxProfit(self, a):
"""
:type prices: List[int]
:rtype: int
"""
n = len(a)
if n==0:
return 0
mins = a[0]
max_diff = 0
for i in range(1,n):
#买入价也可以看成是卖出价 #找到到截止到第i天的最低买入价
if(a[i]<mins):
mins = a[i]
# 更新最大收益
elif max_diff < a[i] - mins:
max_diff = a[i] - mins
return max_diff
121. Best Time to Buy and Sell Stock(股票最大收益)的更多相关文章
- 121. Best Time to Buy and Sell Stock (一) leetcode解题笔记
		121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ... 
- 30. leetcode 121. Best Time to Buy and Sell Stock
		121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ... 
- leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
		121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ... 
- 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 ... 
- 121. 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 ... 
- [LeetCode] 121. 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】121 Best Time to Buy and Sell Stock
		Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ... 
- 121. 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 ... 
- (Array)121. 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 121. Best Time to Buy and Sell Stock ----- java
		Say you have an array for which the ith element is the price of a given stock on day i. If you were ... 
随机推荐
- 通过ArcGIS Desktop数据发布ArcGIS Server
			1.双击GIS Servers--->Add ArcGIS Server 2.选择Publish GIS Services 3.输入Server URL:http://localhost:608 ... 
- Redis(八)-- Redis分布式锁实现
			一.使用分布式锁要满足的几个条件 系统是一个分布式系统(关键是分布式,单机的可以使用ReentrantLock或者synchronized代码块来实现) 共享资源(各个系统访问同一个资源,资源的载体可 ... 
- OSG绘制金字塔geode+动态纹理坐标
			osg::Node* createPyramidModel() { // create the root node which will hold the model. osg::Group* roo ... 
- 关于ARM的内核架构
			很多时候我们都会对M0,M0+,M3,M4,M7,arm7,arm9,CORTEX-A系列,或者说AVR,51,PIC等,一头雾水,只知道是架构,不知道具体是什么,有哪些不同?今天查了些资料,来解解惑 ... 
- background-clip和background-origin
			background-clip 修剪:背景颜色从哪些区域开始显示,默认从border开始该属性指定了背景在哪些区域可以显示,但与背景开始绘制的位置无关,背景的绘制的位置可以出现在不显示背景的区域,这时 ... 
- Spring学习笔记--注入Bean属性
			这里通过一个MoonlightPoet类来演示了注入Bean属性property的效果. package com.moonlit.myspring; import java.util.List; im ... 
- block基本使用和底层
			block基础使用语法 一.block与函数的对比 定义函数指针 int (*myFn)(); 定义Blocks int (^MyBlocks)(int,int); 调用函数指针 (*myFn)( ... 
- 【Java nio】Channel
			package com.slp.nio; import org.junit.Test; import java.io.*; import java.nio.ByteBuffer; import jav ... 
- javascript:;禁用a标签默认功能的缺点。
			在使用a标签做切换tab或者其他功能时,经常使用javascript:;来作为a标签的href来使用. 缺点: 1.在js尚未加载的情况下,点击该a标签会弹出新窗口. 2.会使gif动画失效(没经历过 ... 
- 搭建FastDFS
			---恢复内容开始--- FastDFS是用c语言编写的一款开源的分布式文件系统.FastDFS为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标,使用Fast ... 
