当前位置:网站首页>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
边栏推荐
- Selenium WebDriver的高级特性
- Know that Chuangyu has helped the energy industry in asset management and was selected into the 2021 IOT demonstration project of the Ministry of industry and information technology
- Adobe Premiere foundation - time remapping (10)
- 第三方工具與框架集成
- 山东大学项目实训(八)设计轮播图进入页面
- 关于微服务
- Svg circle drawing path animation
- Anaconda installs and configures jupyter notebook remote
- 熊猫跑酷js小游戏代码
- 金鱼哥RHCA回忆录:DO447构建高级作业工作流--创建工作流作业模板并启动工作流作业
猜你喜欢

2. 在STM32CubeMX建立的Keil5工程中添加自定义的相关文件

第02章_MySQL的数据目录

产品-Axure9(英文版),中继器(Repeater)实现表格内容的增删查改(CRUD)

Shandong University project training (VI) Click event display line chart

JDBC knowledge

《安富莱嵌入式周报》第271期:2022.06.20--2022.06.26

Anaconda安装并配置jupyter notebook远程

These advantages of the Institute are really fragrant! The landing rate is still very high!

Request header field XXXX is not allowed by access control allow headers in preflight response

Panda Parkour JS games code
随机推荐
山东大学项目实训(八)设计轮播图进入页面
Svg circle drawing path animation
Sword finger offer 34 Path DFS method for binary tree neutralization
Mac: MySQL 66 questions, 20000 words + 50 pictures!
Shandong University project training (VII) add navigation bar to select city
JDBC knowledge
SD6.24集训总结
535. encryption and decryption of tinyurl / Jianzhi offer II 103 Minimum number of coins
Apache Doris 基本使用总结
Fastdfs
How to use the low code platform of the Internet of things for service management?
Sd6.24 summary of intensive training
6.29模拟赛总结
2. add customized related files to the keil5 project established by stm32cubemx
1. use stm32cubemx to establish stm32g030c8t6 project
About microservices
Stepping on the pit: json Parse and json stringify
第八届“互联网+”大赛 | 云原生赛道邀你来挑战
Sd6.22 summary of intensive training
isalpha()方法介绍