My Java solution using HashMap. Any suggestion?


  • 1
    X
    public class Solution {
    public int rob(int[] nums) {
    	return robHelper(nums, 0);
    }
    
    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    private int robHelper(int[] nums, int i) {
    	if (i >= nums.length)
    		return 0;
    
    	if (map.containsKey(i))
    		return map.get(i);
    	else {
    		int max = Math.max(nums[i] + robHelper(nums, i + 2),
    				robHelper(nums, i + 1));
    		map.put(i, max);
    		return max;
    	}
    }
    }

  • 0
    P

    You can use dp solve this. No hashmap needed


  • 0
    X

    What I am doing is not DP?


Log in to reply
 

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