当前位置:网站首页>Leetcode buys and sells stocks
Leetcode buys and sells stocks
2022-07-05 23:01:00 【Procedural ape does not lose hair 2】
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 .
Example 1:
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 ; meanwhile , You can't sell stocks before you buy them .
Example 2:
Input :prices = [7,6,4,3,1]
Output :0
explain : under these circumstances , No deal is done , So the biggest profit is 0.
Tips :
1 <= prices.length <= 10^5
0 <= prices[i] <= 10^4
java Code :
class Solution {
public int maxProfit(int[] prices) {
int max = 0;
int minPrice = Integer.MAX_VALUE;
for(int n:prices) {
if(n< minPrice) {
minPrice = n;
}else {
int diff = n - minPrice;
max = Math.max(diff, max);
}
}
return max;
}
}
边栏推荐
- Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share
- Nail error code Encyclopedia
- Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
- 513. Find the value in the lower left corner of the tree
- Tensor attribute statistics
- Overview of Fourier analysis
- Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
- Getting started stm32--gpio (running lantern) (nanny level)
- 从 1.5 开始搭建一个微服务框架——日志追踪 traceId
- Thinkphp5.1 cross domain problem solving
猜你喜欢
![[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]](/img/df/9aa83ac5bd9f614942310a040a6dff.jpg)
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]

Selenium+Pytest自动化测试框架实战

Google Maps case

Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
![[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]](/img/41/4de83d2c81b9e3485d503758e12108.jpg)
[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]

LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)

d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL

Vision Transformer (ViT)

Element positioning of Web Automation

Ieventsystemhandler event interface
随机推荐
Getting started stm32--gpio (running lantern) (nanny level)
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
我对新中台模型的一些经验思考总结
鏈錶之雙指針(快慢指針,先後指針,首尾指針)
Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
2022.02.13 - SX10-30. Home raiding II
视频标准二三事
Selenium+pytest automated test framework practice
如何快速理解复杂业务,系统思考问题?
openresty ngx_ Lua request response
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
Error when LabVIEW opens Ni instance finder
All expansion and collapse of a-tree
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Nacos 的安装与服务的注册
APK加固技术的演变,APK加固技术和不足之处
The code generator has deoptimised the styling of xx/typescript. js as it exceeds the max of 500kb
d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL