当前位置:网站首页>Leetcode121 timing of buying and selling stocks
Leetcode121 timing of buying and selling stocks
2022-06-25 15:10:00 【Milanien】
difficulty : Simple
Title Description
Ideas
Find the maximum difference between the future price of the stock minus the past price , That is, the maximum profit , If the future price is always lower than the past price , Then the profit is 0.
Code
1. Exhaustive method
class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = 0
for i in range(len(prices)):
for j in range(i+1,len(prices)):
tmp = prices[j]-prices[i]
if tmp > profit:
profit = tmp
return profit
Time limit exceeded .
2. Calculate the maximum profit while finding the lowest price
class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = 0
minprice = 20000000
for i in range(len(prices)):
tmp = prices[i]-minprice
if tmp > profit:
profit = tmp
if prices[i] < minprice:
minprice = prices[i]
return profit
边栏推荐
- @Font face fonts only work on their own domain - @font-face fonts only work on their own domain
- Design and implementation of timer
- Use Matplotlib to draw a line chart
- A deformation problem of Hanoi Tower
- Power automatic test system nsat-8000, accurate, high-speed and reliable power test equipment
- Automatic correlation between QT signal and slot
- Software packaging and deployment
- One question per day,
- (translation) json-rpc 2.0 specification (Chinese version)
- 55 specific ways to improve program design (1)
猜你喜欢
Esp8266 building smart home system
Learning notes on February 5, 2022 (C language)
Review of arrays and pointers triggered by a topic
[C language] implementation of magic square array (the most complete)
Dynamic memory allocation
15 -- k points closest to the origin
GDB debugging
QQ love talk candy love talk content acquisition and storage
Common dynamic memory errors
Judging the number of leap years from 1 to N years
随机推荐
GDB debugging
RDB and AOF persistence of redis
搭建极简GB28181 网守和网关服务器,建立AI推理和3d服务场景,然后开源代码(一)
semget No space left on device
User defined data type - structure
Js- get the mouse coordinates and follow them
多张动图怎样合成一张gif?仅需三步快速生成gif动画图片
QQ情话糖果情话内容获取并保存
About?: Notes for
Build a minimalist gb28181 gatekeeper and gateway server, establish AI reasoning and 3D service scenarios, and then open source code (I)
One code per day - day one
Power automatic test system nsat-8000, accurate, high-speed and reliable power test equipment
@Font face fonts only work on their own domain - @font-face fonts only work on their own domain
Brain tree (I)
How to make GIF animation online? Try this GIF online production tool
Is it normal to dig for money? Is it safe to open a stock account?
Golang channel reading data
Vs2019 scanf error
14 -- validate palindrome string II
p1408