当前位置:网站首页>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;
}边栏推荐
- Some problems encountered in introducing lvgl into esp32 Arduino
- HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
- This article takes you to understand the operation of vim
- YYGH-BUG-04
- GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
- to_ Bytes and from_ Bytes simple example
- QT获取某个日期是第几周
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
- (C语言)八进制转换十进制
- MSI announced that its motherboard products will cancel all paper accessories
猜你喜欢

The selected cells in Excel form have the selection effect of cross shading

How to Create a Nice Box and Whisker Plot in R

Data analysis - Matplotlib sample code

conda常用命令汇总

6方面带你认识LED软膜屏 LED软膜屏尺寸|价格|安装|应用

Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer

The computer screen is black for no reason, and the brightness cannot be adjusted.

(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?

GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R

HOW TO ADD P-VALUES TO GGPLOT FACETS
随机推荐
SCM power supply
6. Introduce you to LED soft film screen. LED soft film screen size | price | installation | application
H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
动态内存(进阶四)
Le tutoriel F - String le plus facile à comprendre de l'histoire.
PHP query distance according to longitude and latitude
Depth filter of SvO2 series
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
Fabric. JS 3 APIs to set canvas width and height
SVO2系列之深度滤波DepthFilter
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
jenkins 凭证管理
K-Means Clustering Visualization in R: Step By Step Guide
XSS labs master shooting range environment construction and 1-6 problem solving ideas
PHP 2D and multidimensional arrays are out of order, PHP_ PHP scrambles a simple example of a two-dimensional array and a multi-dimensional array. The shuffle function in PHP can only scramble one-dim
Repeat, tile and repeat in pytorch_ The difference between interleave
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
Time format display
Develop scalable contracts based on hardhat and openzeppelin (I)