Most two continuous "no"s


  • 0

    I use no represent not rob, yes rob, dno double no.

    Because there are no more than two consecutive nos when robbing.

    public class Solution {
        public int rob(int[] nums) {
            int no = 0, yes = 0, dno = 0; // double no
            for (int i = 0; i < nums.length; i++) {
                int tmp = yes;
                yes = Math.max(dno, no) + nums[i];
                dno = no;
                no = tmp;
            }
            return Math.max(no, yes);
        }
    }
    
    /// another solution
    
    public class Solution {
        public int rob(int[] nums) {
            int no = 0, yes = 0, dno = 0; // double no
            for (int i = 0; i < nums.length; i++) {
                int tmp = yes;
                yes = Math.max(dno, no) + nums[i];
                dno = no;
                no = tmp;
            }
            return Math.max(no, yes);
        }
    }
    
    
    public class Solution {
        public int rob(int[] nums) {
            int yes = 0, no = 0, n = nums.length;
            for (int i = 0; i < n; i++) {
                int tmp = no;
                no = Math.max(no, yes);
                yes = tmp + nums[i];
            }
            return Math.max(no, yes);
        }
    }
    
    

Log in to reply
 

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