198. House Robber

# Easy

Comes from Google interview. Classical DP problem.

Solution 1: regular sequence DP array.

  1. robs[x] means the max money can rob in house [0:x]

  2. Initialize robs[0] and robs[1]. Initialize as much as possible.

  3. Time complexity = O(n)O(n) , spcase complexity = O(n)O(n)

When robber wants to rob (2, 7, 9, 3, 1) houses, two options: (2, 7, 9, 3) or (2, 7, 9) + 1

Solution 2: optimize space complexity, because the previous robs are not useful

  1. Only robs[x-2] and robs[x-1] are used for computing robs[x].

  2. Initialization is important.

  3. Time complexity = O(n)O(n) , space complexity = O(1)O(1)

Last updated

Was this helpful?