当前位置:网站首页>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;
}
}
边栏推荐
- Hcip day 11 (BGP agreement)
- 实现反向代理客户端IP透传
- Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share
- 一文搞定JVM常见工具和优化策略
- The code generator has deoptimised the styling of xx/typescript. js as it exceeds the max of 500kb
- openresty ngx_ Lua request response
- Getting started stm32--gpio (running lantern) (nanny level)
- All expansion and collapse of a-tree
- Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
- Three. Js-01 getting started
猜你喜欢
The method and principle of viewing the last modification time of the web page
Vision Transformer (ViT)
My experience and summary of the new Zhongtai model
PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
Selenium+pytest automated test framework practice
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Arduino 测量交流电流
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
30 optimization skills about mysql, super practical
Usage Summary of scriptable object in unity
随机推荐
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
How can easycvr cluster deployment solve the massive video access and concurrency requirements in the project?
Three.JS VR看房
Arduino measures AC current
Common JVM tools and optimization strategies
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
VOT Toolkit环境配置与使用
Binary tree (II) -- code implementation of heap
我把开源项目alinesno-cloud-service关闭了
VIM tail head intercept file import
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
First, redis summarizes the installation types
Global and Chinese markets for children's amusement facilities 2022-2028: Research Report on technology, participants, trends, market size and share
Why does the C# compiler allow an explicit cast between IEnumerable&lt; T&gt; and TAlmostAnything?
Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
Starting from 1.5, build a micro Service Framework -- log tracking traceid
如何快速理解复杂业务,系统思考问题?
My experience and summary of the new Zhongtai model