当前位置:网站首页>Best time to buy and sell stock II
Best time to buy and sell stock II
2022-06-23 08:59:00 【ruochen】
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Sell as long as there is profit , It's the best solution .
public int maxProfit(int[] prices) {
int profit = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i - 1] < prices[i]) {
profit += prices[i] - prices[i - 1];
}
}
return profit;
}边栏推荐
- 636. Exclusive Time of Functions
- @Response
- Paper reading [quovadis, action recognition? A new model and the dynamics dataset]
- node request模块cookie使用
- 523. Continuous Subarray Sum
- 986. Interval List Intersections
- Hongmeng reads the resource file
- Flink error --caused by: org apache. calcite. sql. parser. SqlParseException: Encountered “time“
- Flutter achieves the effect of selecting seats in the cinema!
- Lighthouse cloud desktop experience
猜你喜欢

986. Interval List Intersections

简易学生管理

Hongmeng reads the resource file

636. Exclusive Time of Functions

Community article | mosn building subset optimization ideas sharing

GeoServer adding mongodb data source

【云原生 | Kubernetes篇】Kubernetes原理与安装(二)

自定义标签——jsp标签增强

Le rapport d'analyse de l'industrie chinoise des bases de données a été publié en juin. Le vent intelligent se lève, les colonnes se régénèrent

Linux MySQL installation
随机推荐
Optimize your gradle module with a clean architecture
Custom tags - JSP tag enhancements
@Response
USB peripheral driver - debug
MQTT+Flink实现实时消息的订阅与发布
[qnx hypervisor 2.2 user manual]6.2 network
How thingjs enables low threshold 3D visualization development
[cloud native | kubernetes] kubernetes principle and installation (II)
How to restore visualizations and dashboards after kibana rebuilds the index
438. Find All Anagrams in a String
TDesign update weekly report (the first week of January 2022)
'coach, I want to play basketball!'—— AI Learning Series booklet for system students
MySQL故障案例 | mysqldump: Couldn’t execute ‘SELECT COLUMN_NAME
Monitor the cache update of Eureka client
JSP入门总结
Leetcode topic analysis h-index II
MySQL fault case | error 1071 (42000): specified key was too long
Leetcode topic analysis set matrix zeroes
173. Binary Search Tree Iterator
Leetcode topic analysis count primes