当前位置:网站首页>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;
}
}
边栏推荐
- Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
- audiopolicy
- LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
- Three. JS VR house viewing
- Marginal probability and conditional probability
- Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
- 第一讲:蛇形矩阵
- openresty ngx_lua请求响应
- [untitled]
- 2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
猜你喜欢
一文搞定class的微观结构和指令
Marginal probability and conditional probability
终于搞懂什么是动态规划的
如何快速理解复杂业务,系统思考问题?
Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
Ieventsystemhandler event interface
Vcomp110.dll download -vcomp110 What if DLL is lost
Un article traite de la microstructure et des instructions de la classe
513. Find the value in the lower left corner of the tree
Distributed resource management and task scheduling framework yarn
随机推荐
Usage Summary of scriptable object in unity
Solve the problem of "no input file specified" when ThinkPHP starts
东南亚电商指南,卖家如何布局东南亚市场?
Hcip day 11 (BGP agreement)
APK加固技术的演变,APK加固技术和不足之处
Thoroughly understand JVM class loading subsystem
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
Event trigger requirements of the function called by the event trigger
Nangou Gili hard Kai font TTF Download with installation tutorial
一文搞定JVM的内存结构
Yiwen gets rid of the garbage collector
Selenium+Pytest自动化测试框架实战
2022.02.13 - SX10-30. Home raiding II
Element operation and element waiting in Web Automation
实现反向代理客户端IP透传
Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
Methods modified by static
Function default parameters, function placeholder parameters, function overloading and precautions
Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]