LeetCode刷题——Array篇(Best Time to Buy and Sell Stock II)

原创 2018年04月16日 16:22:48

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).

思路:
官网提供的一种是将所有递增的结果加起来;
这个有点像电路里的上升沿和下降沿,因此,在上升沿加入买入,下降沿售出才能使收益最大
代码如下

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int sum = 0;
        bool flag = false;
        int begin = 0;
        int end = 1;
        prices.push_back(0);
        for (int i = 0; i < prices.size(); i++)
        {
            if (prices[i + 1] > prices[i]&&flag==false)
            {
                flag = true;
                begin = i;
            }
            else if (prices[i + 1]< prices[i]&&flag==true)
            {
                flag = false;
                end = i;
                sum += prices[end] - prices[begin];
            }
        }
        return sum;

    }
};

Microsoft Agent技术在Delphi中的应用

解放军合肥炮兵学院 张建军 中保财险安徽省铜陵市分公司 韩莹 ---- Microsoft Agent是微软公司于1997年9月发布的一项代理软件开发技术,而后被集成到Internet Explore...
  • deardelphi
  • deardelphi
  • 2000-08-26 10:06:00
  • 689

【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

Best Time to Buy and Sell Stock I题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求...
  • ljiabin
  • ljiabin
  • 2015-04-06 11:10:34
  • 22525

Leetcode_122_Best Time to Buy and Sell Stock II

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43155725 Say you have an a...
  • pistolove
  • pistolove
  • 2015-01-26 18:28:17
  • 1739

leetcode【121+122+123 best time to buy and sell stock】【python】

我们先拿出来前三道题,因为他们都是array中的题目。这是leetcode种经典的一系列题,涉及到动态规划和贪心算法。按照我的理解,贪心是满足当前条件的最优值我们就将它最为最优解,也就是大家说的局部最...
  • u014251967
  • u014251967
  • 2016-09-12 21:04:35
  • 1151

【LeetCode】【Python题解】Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i. Design an...
  • u011613729
  • u011613729
  • 2014-07-24 10:11:44
  • 3572

LeetCode -- Best Time to Buy and Sell Stock II (贪心策略,差分序列)

LeetCode -- Best Time to Buy and Sell Stock II (贪心策略,差分序列) IT code Interview 程序员面试题,贪心策略,差分序列...
  • mason_mow
  • mason_mow
  • 2014-05-27 15:44:01
  • 1396

关于LeetCode中Best Time to Buy and Sell Stock一题的理解

题目如下: Say you have an array for which the ith element is the price of a given stock on day i. If yo...
  • zsy112371
  • zsy112371
  • 2016-09-09 13:55:36
  • 1342

Best Time to Buy and Sell Stock II -- LeetCode

原题链接: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/  这道题跟Best Time to Buy and...
  • linhuanmars
  • linhuanmars
  • 2014-04-09 05:33:51
  • 9856

leetcode_c++:Best Time to Buy and Sell Stock II(122)

题目Say you have an array for which the ith element is the price of a given stock on day i.Design an a...
  • mijian1207mijian
  • mijian1207mijian
  • 2016-06-10 23:19:48
  • 353

Leetcode算法学习日志-714 Best Time to Buy and Sell Stock with Transaction Fee

Leetcode 714 Best Time to Buy and Sell Stock with Transaction Fee 题目原文 Your are given an array o...
  • Zarlove
  • Zarlove
  • 2017-10-23 21:16:05
  • 1808
收藏助手
不良信息举报
您举报文章:LeetCode刷题——Array篇(Best Time to Buy and Sell Stock II)
举报原因:
原因补充:

(最多只允许输入30个字)