当前位置:网站首页>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;
}
边栏推荐
- U3D_ Infinite Bessel curve
- Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
- 山东老博会,2022中国智慧养老展会,智能化养老、适老科技展
- Summary of knowledge points of xlua hot update solution
- 喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配
- SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
- C4D learning notes 1- animation - animation key frames
- Three. JS introductory learning notes 11:three JS group composite object
- Learn good-looking custom scroll bars in 1 minute
- Three. JS introductory learning notes 18: how to export JSON files with Blender
猜你喜欢

星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”

Numpy --- basic learning notes

AB package details in unity (super detail, features, packaging, loading, manager)
通知Notification使用全解析

企业级日志分析系统ELK

10 schemes to ensure interface data security

Webgl texture

Mesh merging under ue4/ue5 runtime

一大波开源小抄来袭

Wireless sensor networks -- ZigBee and 6LoWPAN
随机推荐
Simple understanding and application of TS generics
OpenGL common functions
Iterator and for of.. loop
2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
Numpy -- epidemic data analysis case
航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
Webgl texture
numpy---基础学习笔记
OpenGL's distinction and understanding of VAO, VBO and EBO
Detailed explanation of Cocos creator 2.4.0 rendering process
Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping
强化实时数据管理,英方软件助力医保平台安全建设
Cocos creator collision and collision callback do not take effect
Clang compile link ffmpeg FAQ
Vertex shader to slice shader procedure, varying variable
Write sequence frame animation with shader
Virtual memory, physical memory /ram what
Spin animation of Cocos performance optimization
Postman generate timestamp, future timestamp
Limit of total fields [1000] in index has been exceeded