Java readability DP.


  • 1
    Z
    public int rob(int[] ns) {
        if(ns == null || ns.length == 0)
            return 0;
            
        int[] rob = new int[ns.length]; // rob.
        int[] norob = new int[ns.length]; // dont rob. 
        
        for(int i =0; i < ns.length;i ++){
            norob[i] = Math.max(i -1 >=0 ?rob[i-1] : 0, i -1 >=0 ? norob[i-1] : 0);
            rob[i] = ns[i] + (i -1 >=0 ? norob[i-1]: 0);
        }
        
        return Math.max(rob[ns.length-1] , norob[ns.length-1]);
    }

Log in to reply
 

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