当前位置:网站首页>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;
}边栏推荐
- HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
- H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
- Yygh-10-wechat payment
- YYGH-BUG-04
- ESP32存储配网信息+LED显示配网状态+按键清除配网信息(附源码)
- YYGH-BUG-04
- HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
- (C语言)八进制转换十进制
- Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
- 通讯录的实现(文件版本)
猜你喜欢

excel表格中选中单元格出现十字带阴影的选中效果

自然语言处理系列(二)——使用RNN搭建字符级语言模型
![[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)](/img/18/f0c9ef6250a717f8e66c95da4de08c.jpg)
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)

HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R

2022年遭“挤爆”的三款透明LED显示屏

Flesh-dect (media 2021) -- a viewpoint of material decomposition

Esp32 stores the distribution network information +led displays the distribution network status + press the key to clear the distribution network information (source code attached)

YYGH-BUG-04

PyTorch nn.RNN 参数全解析

Develop scalable contracts based on hardhat and openzeppelin (I)
随机推荐
Log4j2
YYGH-BUG-04
Dynamic memory (advanced 4)
多文件程序X32dbg动态调试
A sharp tool for exposing data inconsistencies -- a real-time verification system
Easyexcel and Lombok annotations and commonly used swagger annotations
自然语言处理系列(一)——RNN基础
Filtre de profondeur de la série svo2
How to Create a Nice Box and Whisker Plot in R
文件操作(详解!)
Log4j2
K-Means Clustering Visualization in R: Step By Step Guide
SVO2系列之深度滤波DepthFilter
H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
Seriation in R: How to Optimally Order Objects in a Data Matrice
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
Fabric. JS 3 APIs to set canvas width and height
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R