当前位置:网站首页>Leetcode122 买卖股票的最佳时机 II
Leetcode122 买卖股票的最佳时机 II
2022-07-02 09:42:00 【魑魅魍魉114】
给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。
在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。
返回 你能获得的 最大 利润 。
输入:prices = [7,1,5,3,6,4]
输出:7
解释:在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4 。
随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6 - 3 = 3 。
总利润为 4 + 3 = 7 。
public int maxProfit(int[] prices) {
int maxProfit = 0;
for(int i = 1; i < prices.length;i++){
maxProfit += Math.max(0,prices[i] - prices[i-1]);
}
return maxProfit;
}
边栏推荐
- GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
- 【2022 ACTF-wp】
- QT meter custom control
- Summary of flutter problems
- 2022年遭“挤爆”的三款透明LED显示屏
- How to Create a Beautiful Plots in R with Summary Statistics Labels
- FLESH-DECT(MedIA 2021)——一个material decomposition的观点
- Log4j2
- Time format display
- Easyexcel and Lombok annotations and commonly used swagger annotations
猜你喜欢
Natural language processing series (III) -- LSTM
YYGH-BUG-04
From scratch, develop a web office suite (3): mouse events
[visual studio 2019] create and import cmake project
CONDA common command summary
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
How to Easily Create Barplots with Error Bars in R
The computer screen is black for no reason, and the brightness cannot be adjusted.
Dynamic memory (advanced 4)
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
随机推荐
YYGH-BUG-05
B high and beautiful code snippet sharing image generation
Uniapp uni list item @click, uniapp uni list item jump with parameters
Natural language processing series (II) -- building character level language model using RNN
小程序链接生成
Summary of flutter problems
uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
多文件程序X32dbg动态调试
MSI announced that its motherboard products will cancel all paper accessories
SVO2系列之深度滤波DepthFilter
PX4 Position_Control RC_Remoter引入
机械臂速成小指南(七):机械臂位姿的描述方法
【2022 ACTF-wp】
PHP query distance according to longitude and latitude
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
GGPlot Examples Best Reference
Research on and off the Oracle chain
ESP32 Arduino 引入LVGL 碰到的一些问题
Data analysis - Matplotlib sample code
FLESH-DECT(MedIA 2021)——一个material decomposition的观点