Best Time to Buy and Sell II


  • 0
    R
    public class Solution {
        public int maxProfit(int[] prices) {
                    int maxProfit = 0;
            List<Integer> profit = new ArrayList<>();
            int min = Integer.MAX_VALUE;
            for (int i = 0; i < prices.length; i++) {
                if (prices[i] < min) {
                    min = prices[i];
                } else if (prices[i] > min) {
                    profit.add(prices[i] - min);
                    min = prices[i];
                }
            }
            for(int p : profit) {
                maxProfit = maxProfit+p;
            }
            return maxProfit;
        }
    }
    

Log in to reply
 

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