当前位置:网站首页>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;
}边栏推荐
- BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
- Log4j2
- Leetcode topic [array] -540- single element in an ordered array
- Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
- Leetcode122 买卖股票的最佳时机 II
- PX4 Position_ Control RC_ Remoter import
- 动态内存(进阶四)
- [QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
- to_ Bytes and from_ Bytes simple example
- Codeforces 771 div2 B (no one FST, refers to himself)
猜你喜欢

HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R

【C语言】十进制数转换成二进制数

GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
![[visual studio 2019] create and import cmake project](/img/51/6c2575030c5103aee6c02bec8d5e77.jpg)
[visual studio 2019] create and import cmake project

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

Take you ten days to easily finish the finale of go micro services (distributed transactions)

深入理解P-R曲线、ROC与AUC

K-Means Clustering Visualization in R: Step By Step Guide

HOW TO ADD P-VALUES TO GGPLOT FACETS

动态内存(进阶四)
随机推荐
还不会安装WSL 2?看这一篇文章就够了
ES集群中节点与分片的区别
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
Uniapp uni list item @click, uniapp uni list item jump with parameters
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
Natural language processing series (III) -- LSTM
conda常用命令汇总
基于Arduino和ESP8266的连接手机热点实验(成功)
How to Add P-Values onto Horizontal GGPLOTS
FLESH-DECT(MedIA 2021)——一个material decomposition的观点
php 二维、多维 数组打乱顺序,PHP_php打乱数组二维数组多维数组的简单实例,php中的shuffle函数只能打乱一维
自然语言处理系列(一)——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
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
全链路压测
Power Spectral Density Estimates Using FFT---MATLAB
【2022 ACTF-wp】
What week is a date obtained by QT
基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)