【LeetCode从零单刷】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 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).

解答:

开始看到题目就没懂,以为有很多 stock 可以选择。还想到什么在最小利润的那天买入,最大利润的那天卖出之类的,还要保证最小利润在最大利润之前。要不要DP之类。

其实是只有一个stock,而且每天买进卖出的次数还不限制

如果出现这种情况:[1, 2, 3] 第一天买入,第二天卖出,第三天就没东西了,也就没有利益最大化。但是这么想:第一天买入,第二天卖出,然后发现第三天价格更高,可以视为第二天在卖出之后又买入了,这样收益就是(2-1)+(3-2)=(3-1)与第一天买入第三天卖出是一样的。

多此一举的操作大大简化了思路,将整体的关系化解耦成了前后两天的关系。只要前一天价格小于下一天就求差。

注意空集……这个又让我错了一次(总吃这个亏!)。如果对 vector 不了解的可以看看这个:Vector (STL)

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

时间: 2024-10-11 16:39:27

【LeetCode从零单刷】Best Time to Buy and Sell Stock II的相关文章

LeetCode练题——122. Best Time to Buy and Sell Stock II

1.题目 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 示例 1: 输入: [7,1,5,3,6,4]输出: 7解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 .  随后,在第 4 天(股票价格 = 3)的时候买入,在第 5

LeetCode 122, 123, 188. Best Time to Buy and Sell Stock II+III+IV

Best Time to Buy and Sell Stock II class Solution { public: int maxProfit(vector<int>& prices) { int res=0; for (int i=1;i<prices.size();++i){ if (prices[i]>prices[i-1]){ res += prices[i]-prices[i-1]; } } return res; } }; Best Time to Buy

leetcode第一刷_Best Time to Buy and Sell Stock II

sched.c sched.h 代码分析笔记 首先上header file sched.h #ifndef _SCHED_H #define _SCHED_H #define HZ 100 #define NR_TASKS 64 #define TASK_SIZE 0x04000000 #define LIBRARY_SIZE 0x00400000 #if (TASK_SIZE & 0x3fffff) #error "TASK_SIZE must be multiple of 4M&qu

[LeetCode][JavaScript]Best Time to Buy and Sell Stock II

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 algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one sha

【LeetCode】Best Time to Buy and Sell Stock II

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 algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one sha

【leetcode】122.Best Time to Buy and Sell Stock II

@requires_authorization @author johnsondu @create_time 2015.7.19 21:01 @url [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) /************************ * @description: dynamic programming. * 相邻元素做

LeetCode: 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 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

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

Best Time to Buy and Sell Stock II leetcode java

题目: 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)

LeetCode之“动态规划”:Best Time to Buy and Sell Stock I &amp;&amp; II &amp;&amp; III &amp;&amp; IV

1. Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), desi