Best Time to Buy and Sell Stock II

Description

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

Tags: Array, Greedy

思路

题意是给出一个数组代表每天的股票金额,在每天只能买或卖的情况下求出收益最高值,这…,这也太简单了吧,把所有相邻递增的值都加起来即可。

java:

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

kotlin(220ms/84.32%):

class Solution {
    fun maxProfit(prices: IntArray): Int {
        var sum = 0
        for (i in 1 until prices.size) {
            if (prices[i] > prices[i - 1]) sum += prices[i] - prices[i - 1]
        }
        return sum
    }
}

JavaScript

var maxProfit = function(prices) {
    let profit = 0;
    for (let i = 0; i < prices.length - 1; i++) {
        const possibleProfit = prices[i + 1] - prices[i];
        profit = Math.max(profit + possibleProfit, profit);
    }
    return profit;
};

结语

如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:LeetCode-Solution