当前位置:网站首页>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;
}
边栏推荐
- Develop scalable contracts based on hardhat and openzeppelin (I)
- R HISTOGRAM EXAMPLE QUICK REFERENCE
- (C语言)八进制转换十进制
- Summary of flutter problems
- QT获取某个日期是第几周
- H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
- 自然语言处理系列(一)——RNN基础
- 深入理解P-R曲线、ROC与AUC
- Dynamic memory (advanced 4)
- 机械臂速成小指南(七):机械臂位姿的描述方法
猜你喜欢
小程序链接生成
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
Power Spectral Density Estimates Using FFT---MATLAB
Esp32 stores the distribution network information +led displays the distribution network status + press the key to clear the distribution network information (source code attached)
The selected cells in Excel form have the selection effect of cross shading
How to Create a Beautiful Plots in R with Summary Statistics Labels
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
[geek challenge 2019] upload
XSS labs master shooting range environment construction and 1-6 problem solving ideas
YYGH-9-预约下单
随机推荐
Log4j2
Power Spectral Density Estimates Using FFT---MATLAB
Repeat, tile and repeat in pytorch_ The difference between interleave
K-Means Clustering Visualization in R: Step By Step Guide
Seriation in R: How to Optimally Order Objects in a Data Matrice
HR wonderful dividing line
GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
Le tutoriel F - String le plus facile à comprendre de l'histoire.
How to Add P-Values onto Horizontal GGPLOTS
MSI announced that its motherboard products will cancel all paper accessories
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
Filtre de profondeur de la série svo2
PyTorch中repeat、tile与repeat_interleave的区别
GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
自然语言处理系列(一)——RNN基础
The most understandable f-string tutorial in history, collecting this one is enough
【C语言】十进制数转换成二进制数
YYGH-BUG-04
PX4 Position_ Control RC_ Remoter import
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R