Java O(n) space O(1) similar approach 0ms


  • 0
    public int rob(int[] A) {
          
            if(A == null || A.length == 0) return 0;
    
            int robCur = 0;
            int notRobCur = 0;
            int tot = 0;
            
            for (int i = 0; i <A.length; i++) {
                tot = Math.max(robCur + A[i], notRobCur);
                robCur = notRobCur;
                notRobCur = tot;
            }
            return tot;
        }
    

Log in to reply
 

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