All files / leetCode 0198.ts

100% Statements 7/7
100% Branches 2/2
100% Functions 1/1
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12  4x 2x   2x   2x 5x   2x    
export default function rob(nums: number[]): number {
  if (nums.length < 2)
    return nums[0]
 
  const res = [nums[0], Math.max(nums[0], nums[1])]
 
  for (let i = 2; i < nums.length; i++)
    res[i] = Math.max(res[i - 1], res[i - 2] + nums[i])
 
  return res[nums.length - 1]
}