122. Best Time to Buy and Sell Stock II

原创 2016年08月29日 16:04:05

题目

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) {
        if(prices.empty())
            return 0;
        int buy=-1;
        int maxProfit=0;
        for(int i=0;i<prices.size()-1;++i)
        {
            if(prices[i]<prices[i+1]&&buy==-1)//当前价格低于其后价格时买入
                buy=prices[i];
            if(prices[i]>prices[i+1]&&buy!=-1)//当前价格高于其后价格时卖出
            {
                maxProfit+=prices[i]-buy;
                buy=-1;
            }
        }
        if(buy!=-1&&prices[prices.size()-1]>buy)//考虑最后一个元素是否卖出
            maxProfit+=prices[prices.size()-1]-buy;
        return maxProfit;
    }
};


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
  • 291

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
  • 1451

【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
  • 3434

LeetCode(122) Best Time to Buy and Sell Stock II (Java)

题目如下: Say you have an array for which the ith element is the price of a given stock on day i. Desig...
  • feliciafay
  • feliciafay
  • 2015年04月19日 13:18
  • 1236

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

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

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
  • 9678

LeetCode OJ 系列之122 Best Time to Buy and Sell Stock II --Python

Problem: Say you have an array for which the ith element is the price of a given stock on day i. ...
  • ln152315
  • ln152315
  • 2015年11月26日 09:21
  • 286

【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
  • 21728

LeetCode(122)Best Time to Buy and Sell Stock2

题目如下: Say you have an array for which the ith element is the price of a given stock on day i. Design...
  • feliciafay
  • feliciafay
  • 2014年01月21日 03:11
  • 2308

[LeetCode] Best Time to Buy and Sell Stock IV

Say you have an array for which the ithi^{th} element is the price of a given stock on day ii.Design...
  • u011331383
  • u011331383
  • 2015年02月23日 00:08
  • 8757
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:122. Best Time to Buy and Sell Stock II
举报原因:
原因补充:

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