当前位置:网站首页>74. maximum profit on shares
74. maximum profit on shares
2022-06-29 18:57:00 【It's Joe Joe】
subject
Suppose you store the price of a stock in an array in chronological order , What's the maximum profit you can get from buying and selling this stock at one time ?
Input : [7,1,5,3,6,4]
Output : 5
explain : In the 2 God ( Stock price = 1) Buy when , In the 5 God ( Stock price = 6) Sell when , Maximum profit = 6-1 = 5 .
Note that profit cannot be 7-1 = 6, Because the selling price needs to be higher than the buying price .
Ideas
Greedy Algorithm : Maintain a buying minimum , And a maximum profit at the time of sale .
Code
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if not prices:
return 0
Min=prices[0]
Max=0
for i in range(0, len(prices)):
Min=min(Min, prices[i])
Max=max(Max, prices[i]-Min)
return Max
边栏推荐
- 第三方工具與框架集成
- Amazing pandaverse:meta "borderless, to activate fashion attributes in the new journey of 2.0
- Sd6.25 summary of intensive training
- 什么是多范式编程语言,其中的“多范式”是什么意思?
- postgis 生成 图形切割
- C Primer Plus 第12章_存储类别、链接和内存管理_代码和练习题
- Weibo comments on high-performance and high availability architecture design (module 5 of architecture practice camp)
- SD6.22集训总结
- 面霸篇:MySQL六十六问,两万字+五十图详解!
- Adobe Premiere foundation - material nesting (animation of Tiktok ending avatar) (IX)
猜你喜欢

Amazing pandaverse:meta "borderless, to activate fashion attributes in the new journey of 2.0

Machine learning 7-Support vector machine

第八届“互联网+”大赛 | 云原生赛道邀你来挑战

防汛救援便携式应急通信系统解决方案

2. add customized related files to the keil5 project established by stm32cubemx

Svg circle drawing path animation

Adobe Premiere foundation - opacity (mixed mode) (XII)

Notes on spintronics - zhangshufeng

对强缓存和协商缓存的理解

Deep learning --- the weight of the three good students' scores (2)
随机推荐
Weibo comments on high-performance and high availability architecture design (module 5 of architecture practice camp)
如何在树莓派上使用OAK相机?
Usage of BeanUtils property replication
Adobe Premiere foundation - material nesting (animation of Tiktok ending avatar) (IX)
第八届“互联网+”大赛 | 云原生赛道邀你来挑战
Data warehouse model layered ODS, DWD, DWM practice
Shandong University project training (VI) Click event display line chart
移动端测试
第02章_MySQL的数据目录
优雅书写Controller(参数验证+统一异常处理)
2. 在STM32CubeMX建立的Keil5工程中添加自定义的相关文件
6.29 simulation summary
保持jupyter notebook在终端关闭时的连接方法
selenium的跨浏览器测试
面试题 10.10. 数字流的秩
防汛救援便携式应急通信系统解决方案
对强缓存和协商缓存的理解
MySQL enterprise development specification
Adobe Premiere foundation - opacity (matte) (11)
These advantages of the Institute are really fragrant! The landing rate is still very high!