当前位置:网站首页>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;
}
}
边栏推荐
- Element positioning of Web Automation
- 记录几个常见问题(202207)
- Vcomp110.dll download -vcomp110 What if DLL is lost
- Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
- Codeforces Global Round 19
- Evolution of APK reinforcement technology, APK reinforcement technology and shortcomings
- Three.js-01 入门
- audiopolicy
- Getting started stm32--gpio (running lantern) (nanny level)
- PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
猜你喜欢

Fix the memory structure of JVM in one article

audiopolicy

Lesson 1: serpentine matrix

鏈錶之雙指針(快慢指針,先後指針,首尾指針)

Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)

d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file

Nanjing: full use of electronic contracts for commercial housing sales

MCU case -int0 and INT1 interrupt count

Alibaba Tianchi SQL training camp task4 learning notes

Activate function and its gradient
随机推荐
Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
抖音__ac_signature
MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
Nacos 的安装与服务的注册
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
我把开源项目alinesno-cloud-service关闭了
分布式解决方案选型
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
Overview of Fourier analysis
Business introduction of Zhengda international futures company
Request preview display of binary data and Base64 format data
All expansion and collapse of a-tree
Tiktok__ ac_ signature
视频标准二三事
openresty ngx_ Lua regular expression
npm ELECTRON_ Mirror is set as domestic source (npmmirror China mirror)
Google Maps case
Roman numeral to integer
Global and Chinese market of diesel fire pump 2022-2028: Research Report on technology, participants, trends, market size and share