当前位置:网站首页>The best time to buy and sell stocks to get the maximum return
The best time to buy and sell stocks to get the maximum return
2022-06-26 08:27:00 【xuanningmeng】
The best time to buy and sell stocks is to get the maximum return
Given an array prices , It's the first i Elements prices[i] Represents the number of shares in a given stock i Sky price .
You can only choose One day Buy this stock , And choose A different day in the future Sell the stock . Design an algorithm to calculate the maximum profit you can get .
Return the maximum profit you can make from the deal . If you can't make any profit , return 0
Their thinking
(1) Use of violence , Find the two elements in the array with the largest difference , The difference is the maximum return , At the same time, the selling price is greater than the buying price , namely The first j The first element is greater than the i Elements ,j>i.
def maxProfit(prices):
result = 0
for i in range(len(prices)):
for j in range(i + 1, len(prices)):
result = max(result, prices[j] - prices[i])
return result
But the time complexity of violence law is relatively high , When the array is very large .
(2) Dynamic planning ideas , Find the lowest value , Calculate the difference between other elements and the lowest value , Dynamically update the minimum number , Calculate the maximum difference .
def maxProfit(prices):
minprice = int(1e9)
profit = 0
for price in prices:
profit = max(price - minprice, profit)
minprice = min(price, minprice)
return profit
This method loops through the array once , Low time complexity .
Record the process of question brushing , insist , insist , insist .
边栏推荐
- How to debug plug-ins using vs Code
- 2020-10-17
- STM32 project design: smart home system design based on stm32
- Idea automatically sets author information and date
- 在 KubeSphere 部署 Wiki 系统 wiki.js 并启用中文全文检索
- Learn signal integrity from zero (SIPI) - (1)
- Relevant knowledge of DRF
- Undefined symbols for architecture i386 is related to third-party compiled static libraries
- Detailed explanation of SOC multi-core startup process
- 教你几招:30句哄女孩的“霸道”温馨话,不看后悔!
猜你喜欢

And are two numbers of S

Use of jupyter notebook

51 MCU project design: Based on 51 MCU clock perpetual calendar
GHUnit: Unit Testing Objective-C for the iPhone

鲸会务一站式智能会议系统帮助主办方实现数字化会议管理

Baoyan postgraduate entrance examination interview - Network

Rewrite string() method in go language

Oracle database self study notes

Fabrication of modulation and demodulation circuit

MySQL insert Chinese error
随机推荐
Design based on STM32 works: multi-functional atmosphere lamp, wireless control ws2812 of mobile app, MCU wireless upgrade program
static const与static constexpr的类内数据成员初始化
Go语言浅拷贝与深拷贝
HEVC学习之码流分析
Use a switch to control the lighting and extinguishing of LEP lamp
Area of Blue Bridge Cup 2 circle
Mapping '/var/mobile/Library/Caches/com. apple. keyboards/images/tmp. gcyBAl37' failed: 'Invalid argume
你为什么会浮躁
2020-10-29
Uploading pictures with FileReader object
Delete dictionary from list
Mapping '/var/mobile/Library/Caches/com.apple.keyboards/images/tmp.gcyBAl37' failed: 'Invalid argume
optee中支持的时间函数
加密的JS代码,变量名能破解还原吗?
Macro task, micro task, async, await principle of interview
MySQL insert Chinese error
73b2d wireless charging and receiving chip scheme
xxl-job配置告警邮件通知
. eslintrc. JS configuration
Reflection example of ads2020 simulation signal