【LeetCode】122. Best Time to Buy and Sell Stock II
来源:互联网 发布:小学网络教学 编辑:程序博客网 时间:2024/06/17 13:10
- Difficulty: Medium
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).
分析
贪心法,低进高出,把所有正的价格差价相加起来
把原始价格序列变成差分序列,本题也可以做成最大m子序列和,m为数组长度
时间复杂度为O(n)
class Solution {public:int maxProfit(vector<int> &prices) {int sum = 0;for (int i = 1; i < prices.size(); i++) {int diff = prices[i] - prices[i - 1];if (diff > 0) sum += diff;}return sum;}};
0 0
- LeetCode 122. Best Time to Buy and Sell Stock II
- [Leetcode] 122. Best Time to Buy and Sell Stock II
- [leetcode] 122.Best Time to Buy and Sell Stock II
- 【leetcode】122.Best Time to Buy and Sell Stock II
- 【LeetCode】122.Best Time to Buy and Sell Stock II
- [leetcode] 122. Best Time to Buy and Sell Stock II
- LeetCode #122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II LeetCode
- [LeetCode]122. Best Time to Buy and Sell Stock II
- Leetcode 122. Best Time to Buy and Sell Stock II
- leetcode 122. Best Time to Buy and Sell Stock II
- LeetCode 122. Best Time to Buy and Sell Stock II
- LeetCode *** 122. Best Time to Buy and Sell Stock II
- leetcode-122. Best Time to Buy and Sell Stock II
- leetcode 122. Best Time to Buy and Sell Stock II
- leetcode 122. Best Time to Buy and Sell Stock II
- 【LeetCode】122. Best Time to Buy and Sell Stock II
- LeetCode - 122. Best Time to Buy and Sell Stock II
- windows server 2008r2 服务器下的项目部署
- 初入Qt巨坑第一天
- JDK配置,Sublime3调用CMD运行JAVA&python
- [生存志] 第113节 项羽驻鸿门
- Android 资源(resource)学习小结
- 【LeetCode】122. Best Time to Buy and Sell Stock II
- input autofocus focus() 在iphone上无效,无法在手机上默认获取焦点
- Log4J.xml
- 学习阶段总结
- PAT乙级1042
- CS231n课程笔记翻译:神经网络笔记1
- PSI授权HTTPS
- python 集合set的创建,更改,遍历,元算合并,交集,补集
- #分数阶Fourier变换的总体认知(From 陶然著)