当前位置:网站首页>121. The best time to buy and sell stocks
121. The best time to buy and sell stocks
2022-07-07 16:02:00 【Zzu dish】
121. The best time to buy and sell stocks
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 <= 105
0 <= prices[i] <= 104
reflection
First, let's assume that we sell stocks on the first day , That is, the current selling price of the stock is sellPrice
If the next i The stock price of days is lower than the stock price sold sellPrice = prices[i]
otherwise max = Math.max(max,prices[i]-sellPrice);
public int maxProfit(int[] prices) {
int sellPrice = prices[0];
int max = Integer.MIN_VALUE;
for (int i=1;i<prices.length;i++){
if(prices[i]<sellPrice){
sellPrice = prices[i];
}else {
max = Math.max(max,prices[i]-sellPrice);
}
}
if (max<0) return 0;
return max;
}
边栏推荐
- Learn good-looking custom scroll bars in 1 minute
- Getting started with webgl (2)
- Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
- 安科瑞电网智能化发展的必然趋势电力系统采用微机保护装置是
- TS as a general cache method
- A JS script can be directly put into the browser to perform operations
- Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
- leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
- torch.numel作用
- Regular expression string
猜你喜欢
Syntax of generator function (state machine)
有钱人买房就是不一样
星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
OpenGL's distinction and understanding of VAO, VBO and EBO
numpy--疫情数据分析案例
Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?
SPI master rx time out中断
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
Cocos creator collision and collision callback do not take effect
Three. JS introductory learning notes 04: external model import - no material obj model
随机推荐
The "go to definition" in VS2010 does not respond or prompts the solution of "symbol not found"
Three. Introduction to JS learning notes 17: mouse control of 3D model rotation of JSON file
通知Notification使用全解析
Write sequence frame animation with shader
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
Please supervise the 2022 plan
Detailed explanation of unity hot update knowledge points and introduction to common solution principles
企业级日志分析系统ELK
Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
U3D_ Infinite Bessel curve
Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
C4D learning notes 1- animation - animation key frames
Wireless sensor networks -- ZigBee and 6LoWPAN
Ue4/ue5 multi thread development attachment plug-in download address
LeetCode3_ Longest substring without duplicate characters
尤雨溪,来了!
Three. JS introductory learning notes 03: perspective projection camera
numpy---基础学习笔记
A JS script can be directly put into the browser to perform operations