A short DP C# Solution


  • 1
    L
    public class Solution {
        public int MaxProfit(int[] prices) {
            if(prices.Count() < 2) return 0;
            int result = 0, buy = prices[0];
            for(int i = 0; i < prices.Count(); i++)
            {
                buy = Math.Min(prices[i], buy);
                int max = Math.Max(buy, prices[i]);
                result += max - buy;
                if(max - buy != 0) buy = prices[i];
            }
            return result;
        }
    }

Log in to reply
 

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