110_leetcode_Best Time to Buy and sell Stock II
LeetCode to and II Time Best 110 Buy
2023-09-11 14:20:59 时间
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).
1:注意特殊情况;2:找到数组相邻的凹点和凸点;3:两者的差值是当前的最大值。4:在查找凸凹值的时候注意边界
int maxProfit(vector<int> &prices) { if(prices.size() <= 1) { return 0; } int maxValue = 0; int start = 0; int end = 0; int size = (int)prices.size(); while(start < size) { while(start < size - 1 && prices[start] >= prices[start + 1]) { start++; } end = start + 1; while(end < size - 1 && prices[end] <= prices[end + 1]) { end++; } if(end == size) { break; } else { maxValue += prices[end] - prices[start]; } start = end + 1; } return maxValue; }
相关文章
- Leetcode: Additive Number
- Leetcode: Shortest Word Distance II
- Leetcode: Best Time to Buy and Sell Stock III
- Leetcode: Best Time to Buy and Sell Stock II
- How To Bind a Combobox to a Dictionary in WPF C#
- JS leetcode II. 左旋转字符串 题解分析
- mysql更新字段值提示You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode
- 【LeetCode】123. Best Time to Buy and Sell Stock III
- Leetcode Best Time to Buy and Sell Stock II
- leetcode - Flatten Binary Tree to Linked List
- [LeetCode] 1342. Number of Steps to Reduce a Number to Zero 将数字变成 0 的操作次数
- [LeetCode] 1326. Minimum Number of Taps to Open to Water a Garden 灌溉花园的最少水龙头数目
- [LeetCode] 1022. Sum of Root To Leaf Binary Numbers 从根结点到叶结点的二进制数之和
- [LeetCode] 945. Minimum Increment to Make Array Unique 使数组没有重复数字的最小增量
- [LeetCode] 849. Maximize Distance to Closest Person 离最近的人的最大距离
- [LeetCode] 532. K-diff Pairs in an Array 数组中差为K的数对
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
- [LeetCode] 263. Ugly Number 丑陋数
- [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
- [LeetCode] 12. Integer to Roman 整数转化成罗马数字
- [LeetCode] 13. Roman to Integer 罗马数字转化成整数