Is this solution correct? Why it doesn't pass?


  • 0
    S

    public class Solution {
    public int maxProfit(int []prices) {
    int min=prices[0];
    int profit=0;
    int length= prices.length;

        for(int i=0;i<length;i++){
            if(min>prices[i]){
                min= prices[i];   
            }
            else{
                if(profit<prices[i]-min){
                    profit=prices[i]-min;
                }
            }
        }
        return profit;
    }
    

    }


Log in to reply
 

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