1. 题目1.1 英文题目You are given an array prices where prices[i] is the price of a given stock on the ith day.
Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
1.2 中文题目给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格 。
设计一个算法来计算你所能获取的最大利润 。你可以尽可能地完成更多的交易(多次买卖一支股票) 。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票) 。
1.3输入输出输入输出prices = [7,1,5,3,6,4]7prices = [1,2,3,4,5]4prices = [7,6,4,3,1]01.4 约束条件
- 1 <= prices.length <= 3 * 104
- 0 <= prices[i] <= 104
IDE版本:16.10.1
语言:c++11
3. 分析这一题可以借鉴121题的方法,也就是Kadane's Algorithm,具体来说就是求出相邻两个元素间的差值,组成数组,再将数组中的正值加到一起,即可 。具体代码如下:
【Leetcode No.122 Best Time to Buy and Sell Stock II Easy(c++实现)】
class Solution {public:int maxProfit(vector<int>& prices) {int maxpro = 0;for (int i = 1; i < prices.size(); i++)maxpro += max(0, prices[i] - prices[i - 1]);return maxpro;}};作者:云梦士出处:http://www.cnblogs.com/yunmeng-shi/本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利 。
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
