当前位置:网站首页>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;
}
}
边栏推荐
- Roman numeral to integer
- Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
- 2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
- Solve the problem of "no input file specified" when ThinkPHP starts
- The method and principle of viewing the last modification time of the web page
- Un article traite de la microstructure et des instructions de la classe
- Request preview display of binary data and Base64 format data
- Usage Summary of scriptable object in unity
- 抖音__ac_signature
- [digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
猜你喜欢

Negative sampling

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

My experience and summary of the new Zhongtai model

Selenium+pytest automated test framework practice

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

Spectrum analysis of ADC sampling sequence based on stm32

第十七周作业

All expansion and collapse of a-tree

openresty ngx_ Lua request response

关于MySQL的30条优化技巧,超实用
随机推荐
Editor extensions in unity
Global and Chinese markets for reciprocating seal compressors 2022-2028: Research Report on technology, participants, trends, market size and share
Hcip day 12 (BGP black hole, anti ring, configuration)
Overview of Fourier analysis
Thinkphp5.1 cross domain problem solving
Masked Autoencoders Are Scalable Vision Learners (MAE)
Three.js-01 入门
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
透彻理解JVM类加载子系统
Binary tree (II) -- code implementation of heap
Nacos 的安装与服务的注册
Thoroughly understand JVM class loading subsystem
Distributed solution selection
Arduino 测量交流电流
First, redis summarizes the installation types
30 optimization skills about mysql, super practical
Roman numeral to integer
[untitled]
【无标题】