当前位置:网站首页>Leetcode122 the best time to buy and sell stocks II
Leetcode122 the best time to buy and sell stocks II
2022-07-02 12:05:00 【Monsters 114】
Give you an array of integers prices , among prices[i] Represents a stock i Sky price .
Every day , You can decide whether to buy and / Or sell shares . You at any time most Can only hold A wave of Stocks . You can also buy... First , And then in On the same day sell .
return What you can get Maximum profits .
Input :prices = [7,1,5,3,6,4]
Output :7
explain : In the 2 God ( Stock price = 1) Buy when , In the 3 God ( Stock price = 5) Sell when , The exchange will make a profit = 5 - 1 = 4 .
And then , In the 4 God ( Stock price = 3) Buy when , In the 5 God ( Stock price = 6) Sell when , The exchange will make a profit = 6 - 3 = 3 .
The total profit is 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;
}
边栏推荐
- 时间格式化显示
- Dynamic memory (advanced 4)
- HOW TO ADD P-VALUES TO GGPLOT FACETS
- 自然语言处理系列(三)——LSTM
- Implementation of address book (file version)
- H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
- to_bytes与from_bytes简单示例
- Take you ten days to easily finish the finale of go micro services (distributed transactions)
- 机械臂速成小指南(七):机械臂位姿的描述方法
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
猜你喜欢
YYGH-BUG-04
测试左移和右移
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
[geek challenge 2019] upload
[visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl
Take you ten days to easily finish the finale of go micro services (distributed transactions)
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
How does Premiere (PR) import the preset mogrt template?
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
动态内存(进阶四)
随机推荐
Flesh-dect (media 2021) -- a viewpoint of material decomposition
pgsql 字符串转数组关联其他表,匹配 拼接后原顺序展示
The computer screen is black for no reason, and the brightness cannot be adjusted.
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
自然语言处理系列(二)——使用RNN搭建字符级语言模型
多文件程序X32dbg动态调试
Natural language processing series (I) -- RNN Foundation
SSH automatically disconnects (pretends to be dead) after a period of no operation
qt 仪表自定义控件
进入前六!博云在中国云管理软件市场销量排行持续上升
Those logs in MySQL
to_ Bytes and from_ Bytes simple example
Research on and off the Oracle chain
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
Esp32 audio frame esp-adf add key peripheral process code tracking
Fabric.js 3个api设置画布宽高
PyTorch nn. Full analysis of RNN parameters
PyTorch nn.RNN 参数全解析
Natural language processing series (II) -- building character level language model using RNN
[visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl