C# DP O(n) and O(1) space


  • 0
    Y
    public class Solution {
        public int MaxProfit(int[] prices) {
            int pre=0,cur=0,max=0;
            for(int i=1;i<prices.Length;i++)
            {
                int dayProfit=prices[i]-prices[i-1];
                cur = pre + dayProfit>0?pre + dayProfit:0;
                max = Math.Max(cur,max);
                pre=cur;
            }
            return max;
        }
    }

Log in to reply
 

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