Best Time to Buy and Sell Stock II Java Solution


  • 0
    V

    public class Solution {

    public int maxProfit(int[] prices) {
    
        if(prices == null || prices.length <= 1){
            return 0;
        }
        
        int[] ret = new int[prices.length - 1];
        int index = 0;
        for(int i = 1; i < prices.length; i++){
            ret[index] = prices[i] - prices[i - 1];
            index++;
        }
        
        int results = 0;
        for(int i = 0; i < ret.length; i++){
            if(ret[i] > 0){
                results = results + ret[i];
            }
        }
        
        return results;
    }
    

    }


Log in to reply
 

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