当前位置:网站首页>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 market of water treatment technology 2022-2028: Research Report on technology, participants, trends, market size and share
- Tiktok__ ac_ signature
- Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
- The code generator has deoptimised the styling of xx/typescript.js as it exceeds the max of 500kb
- Composition of interface
- Overview of Fourier analysis
- Getting started stm32--gpio (running lantern) (nanny level)
- Exponential weighted average and its deviation elimination
- 2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
- d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
猜你喜欢
Error when LabVIEW opens Ni instance finder
Codeforces Global Round 19
d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
MCU case -int0 and INT1 interrupt count
[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]
audiopolicy
Starting from 1.5, build a micro Service Framework -- log tracking traceid
Tensor attribute statistics
Overview of Fourier analysis
Un article traite de la microstructure et des instructions de la classe
随机推荐
openresty ngx_lua请求响应
Leecode learning notes
Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
Solve the problem of "no input file specified" when ThinkPHP starts
谷歌地图案例
Nanjing: full use of electronic contracts for commercial housing sales
PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
Record several frequently asked questions (202207)
Vcomp110.dll download -vcomp110 What if DLL is lost
openresty ngx_ Lua regular expression
The code generator has deoptimised the styling of xx/typescript. js as it exceeds the max of 500kb
抖音__ac_signature
Boring boring
Marginal probability and conditional probability
Common JVM tools and optimization strategies
利用LNMP实现wordpress站点搭建
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
Matlab smooth curve connection scatter diagram
Arduino 测量交流电流
Nacos installation and service registration