Leetcode 122: Best Time to Buy and Sell Stock II

原创 2016年08月29日 12:55:40

Leetcode 122: Best Time to Buy and Sell Stock II
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/

1, I got an array, the ith element is the price of a given stock on day i
2, design an algorithm to find the max profit, I can have as many as transactions as I like
3, restrains: I have to sell the stock in order to buy again

Thought map:
1, the stock array would looks like [12,3,24,3,343,24,53,345,654,6]
2, use dp to solve that problem because this is the type of questions that let you find max/min values in an array
3, I need to know when I buy a stock, what will the profit I gonna have in following days
4, then I notic that I can have as many as transactions as I like
5, In that case, that seems to find all the uptick in the array and find the total profit
6, when next number is larger than current number, find the current profit and add to total
7, if next number is smaller than current numberm then do nothing, continue the for loop

public class Solution {
    public int maxProfit(int[] prices) {
        int maxProfit = 0;
        for (int i = 0; i < prices.length - 1; i++) {
            if (prices[i + 1] > prices[i])
                maxProfit += prices[i + 1] - prices[i];
        }
        return maxProfit;

    }
}

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

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

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

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

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

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

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

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

[leetcode 309]Best Time to Buy and Sell Stock with Cooldown--买卖股票with冷却期(DP法)

Question: Say you have an array for which the ith element is the price of a given stock on day ...
  • Xiaohei00000
  • Xiaohei00000
  • 2016年03月10日 23:02
  • 1742

[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
  • 8743

关于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
  • 830
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Leetcode 122: Best Time to Buy and Sell Stock II
举报原因:
原因补充:

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