Here is my simple solution

  • 0

    I think it is almost identical to the one answered below but I am posting it anyways.
    So the time complextity is just O(n), with no additional space

  • -3

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

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.