1. Best Time to Buy and Sell Stock II
    买卖股票的最佳时机 II买卖股票的最佳时机 II
    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 (i.e., buy one and sell one share of the stock multiple times).
    Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
[大意]给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)Example 1:
Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.Example 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.Note that you cannot buy on day 1, buy on day 2 and sell them later, as you areengaging multiple transactions at the same time. You must sell before buying again.Example 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

思路】1.贪心算法,判断某一小段的最高点,即在这部分利益最大。最终要求的利益最大,就是各部分最大利益之和。
2.这个方法是solution的,代码很简单,原理看下图,等效于只要后一天的大于前一天就可以买入卖出。
注:此解法不是说从第3天买入,第4天卖出,然后第4天买入第5天卖出......。这里的意思是这样的算得的利益
数值上等于 第3天买入,第6天卖出。

代码1:
class Solution {
public:int maxProfit(vector<int>& prices) {int max=0,p=0,profit;        int i=0;for(int j=1;j<prices.size();j++){profit=prices[j]-prices[i];if(max>profit){i=j;                         p+=max;   max=0;}else{max=profit;if(j==prices.size()-1)p+=max;}                                       }return p;}
};代码2:class Solution {
public:int maxProfit(vector<int>& prices) {int profit = 0;if(prices.size() <= 1) return 0;for(int i =1;i < prices.size();i++){if (prices[i] > prices[i-1]){        profit+=prices[i] - prices[i-1];}}return profit; }
};

【Leetcode】122. Best Time to Buy and Sell Stock II买卖股票的最佳时机 II相关推荐

  1. 【贪心 和 DP + 卖股票】LeetCode 122. Best Time to Buy and Sell Stock II

    LeetCode 122. Best Time to Buy and Sell Stock II Solution1:我的答案 贪心和DP傻傻分不清! class Solution { public: ...

  2. [LeetCode]122. Best Time to Buy and Sell Stock II

    [LeetCode]122. Best Time to Buy and Sell Stock II 题目描述 思路 I的后续 将数组分为几个小部分, 划分标准是 [i] < [i - 1](划分 ...

  3. LeetCode 122. Best Time to Buy and Sell Stock II--贪心--Java,C++,Python解法

    题目地址:Best Time to Buy and Sell Stock II - LeetCode Say you have an array for which the ith element i ...

  4. 122 Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II

    假设有一个数组,它的第 i 个元素是一个给定的股票在第 i 天的价格. 设计一个算法来找到最大的利润.你可以完成尽可能多的交易(多次买卖股票).然而,你不能同时参与多个交易(你必须在再次购买前出售股票 ...

  5. LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)

    翻译 话说你有一个数组,其中第i个元素表示第i天的股票价格.设计一个算法以找到最大利润.你可以尽可能多的进行交易(例如,多次买入卖出股票).然而,你不能在同一时间来多次交易.(例如,你必须在下一次买入 ...

  6. LeetCode 123. Best Time to Buy and Sell Stock III--Python解法--动态规划--数学题

    此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址:Best ...

  7. leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee | 714. 买卖股票的佳最时机含手续费(递归->傻缓存->dp)

    题目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ 题解 经典的 暴力递归 - ...

  8. 【DP + 卖股票】LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee

    LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee Solution1: 参考网址:http://www.cnblog ...

  9. 【DP + 卖股票】LeetCode 309. Best Time to Buy and Sell Stock with Cooldown

    LeetCode 309. Best Time to Buy and Sell Stock with Cooldown Solution1: 比较有难度的一道动态规划题了! 参考网址:http://z ...

最新文章

  1. 一种安全的static变量Get/Set方式
  2. 生活点滴:java基础知识细化
  3. 事件总线第一次点击_用户体验研究指南3-3第一次点击测试
  4. Poj 1011 UVA - 307 Sticks
  5. 因为apple无法检查其是否包含恶意软件_新Linux恶意脚本——清理其他恶意软件后再感染...
  6. 【Kylin】60-20-040-集成-Kylin集成LDAP
  7. 3.9 神经网络的梯度下降法
  8. tomcat服务器的虚拟目录,Windows系统下安装Tomcat服务器和配置虚拟目录的方法
  9. pycharm的下载,安装及首次启动
  10. BA--干球温度、露点温度和湿球温度--概念
  11. 四叉树 gis java_GIS中四叉树索引及其分类介绍 | 麻辣GIS
  12. python3模拟键盘输入_python 模拟键盘输入
  13. 计算机主机如何睡眠,win7怎样设置电脑休眠_w7电脑设置休眠的详细步骤
  14. 手机棋牌游戏开发的流程是怎样的?
  15. JS显示团购剩下时间的代码整理
  16. 微信小程序主体如何变更?小程序迁移流程详解
  17. 小米10000MAH充电宝四个灯同时闪烁故障维修
  18. 计算机科学与技术到底是什么?
  19. Coning SMF-28 单模光纤基本参数
  20. 手机来电秀怎么开启_华为手机怎么设置视频来电秀?原来方法这么简单,手把手教你设置...

热门文章

  1. 红米K30pro变焦版与荣耀V30pro哪个好-红米K30pro变焦版与荣耀V30pro对比区别
  2. CATIA转的STP打开什么都没有_第五篇:STP
  3. 任务三 类的继承与派生
  4. Xcode 8 错误 dyld: Library not loaded: @rpath/libswiftCore.dylib 解决办法
  5. python:最大公约数和最小公倍数
  6. 2.(cesium之家)cesium加载接入百度地图
  7. 今日头条 爬虫 java_java实现简单的爬虫之今日头条
  8. python新年有趣代码_Python有趣时刻,这些代码让你大呼卧槽,怎么会这样
  9. 如何揪出修改浏览器主页的流氓软件2
  10. 倪江利:魅族推荐平台的架构演进之路