122. Best Time to Buy and Sell Stock II

6人阅读 评论(0) 收藏 举报
分类:

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

int maxProfit(vector<int> &prices) {
    int ret = 0;
    for (size_t p = 1; p < prices.size(); ++p) 
      ret += max(prices[p] - prices[p - 1], 0);    
    return ret;
}
suppose the first sequence is “a <= b <= c <= d”, the profit is “d - a = (b - a) + (c - b) + (d - c)” without a doubt. And suppose another one is “a <= b >= b’ <= c <= d”, the profit is not difficult to be figured out as “(b - a) + (d - b’)”. So you just target at monotone sequences.
查看评论

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

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

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

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

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

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

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:34
  • 1256

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

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

关于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
  • 1322

Best Time to Buy and Sell Stock -- LeetCode

原题链接: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/  这道题求进行一次交易能得到的最大利润。如果用brute ...
  • linhuanmars
  • linhuanmars
  • 2014-04-09 05:33:32
  • 17646
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 1万+
    积分: 1368
    排名: 3万+
    博客专栏
    最新评论