From f87f4a96756da6324587220b2339e28fc0fe958a Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Sun, 27 Oct 2024 20:27:11 +0800 Subject: [PATCH] 244th Commit --- README.md | 21 ++++++++++++--------- src/page-2/198. House Robber/rob.test.ts | 8 ++++++++ src/page-2/198. House Robber/rob.ts | 22 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 src/page-2/198. House Robber/rob.test.ts create mode 100644 src/page-2/198. House Robber/rob.ts diff --git a/README.md b/README.md index 7bb8d8e..034e33c 100644 --- a/README.md +++ b/README.md @@ -244,11 +244,12 @@ Ace Coding Interview with 75 Qs | ------------------------------ | ---------------- | ------ | | 1137. N-th Tribonacci Number | [Solution][1137] | Easy | | 746. Min Cost Climbing Stairs | [Solution][746] | Easy | -| 198. House Robber | Solution | Medium | +| 198. House Robber | [Solution][198] | Medium | | 790. Domino and Tromino Tiling | Solution | Medium | [1137]: ./src/page-11/1137.%20N-th%20Tribonacci%20Number/tribonacci.ts [746]: ./src/page-7/746.%20Min%20Cost%20Climbing%20Stairs/minCostClimbingStairs.ts +[198]: ./src/page-2/198.%20House%20Robber/rob.ts | DP - Multidimensional | | | | --------------------------------------------------------- | -------- | ------ | @@ -550,15 +551,16 @@ Must-do List for Interview Prep [69]: ./src/page-1/69.%20Sqrt(x)/mySqrt.ts [50]: ./src/page-1/50.%20Pow(x,%20n)/myPow.ts -| 1D DP | | | -| ----------------------------------- | -------------- | ------ | -| 70. Climbing Stairs | [Solution][70] | Easy | -| 198. House Robber | Solution | Medium | -| 139. Word Break | Solution | Medium | -| 322. Coin Change | Solution | Medium | -| 300. Longest Increasing Subsequence | Solution | Medium | +| 1D DP | | | +| ----------------------------------- | --------------- | ------ | +| 70. Climbing Stairs | [Solution][70] | Easy | +| 198. House Robber | [Solution][198] | Medium | +| 139. Word Break | Solution | Medium | +| 322. Coin Change | Solution | Medium | +| 300. Longest Increasing Subsequence | Solution | Medium | [70]: ./src/page-1/70.%20Climbing%20Stairs/climbStairs.ts +[198]: ./src/page-2/198.%20House%20Robber/rob.ts | Multidimensional DP | | | | ---------------------------------------- | ------------- | ------ | @@ -651,7 +653,7 @@ Must-do List for Interview Prep | 118. Pascal's Triangle | [Solution][118] | Easy | | 139. Word Break | Solution | Medium | | 152. Maximum Product Subarray | Solution | Medium | -| 198. House Robber | Solution | Medium | +| 198. House Robber | [Solution][198] | Medium | | 279. Perfect Squares | Solution | Medium | | 300. Longest Increasing Subsequence | Solution | Medium | | 322. Coin Change | Solution | Medium | @@ -661,6 +663,7 @@ Must-do List for Interview Prep [5]: ./src/page-1/5.%20Longest%20Palindromic%20Substring/longestPalindrome.ts [70]: ./src/page-1/70.%20Climbing%20Stairs/climbStairs.ts [118]: ./src/page-2/118.%20Pascal's%20Triangle/generate.ts +[198]: ./src/page-2/198.%20House%20Robber/rob.ts | Graph | | | | ---------------------- | --------------- | ------ | diff --git a/src/page-2/198. House Robber/rob.test.ts b/src/page-2/198. House Robber/rob.test.ts new file mode 100644 index 0000000..4b92ddd --- /dev/null +++ b/src/page-2/198. House Robber/rob.test.ts @@ -0,0 +1,8 @@ +import { rob } from './rob'; + +describe('198. House Robber', () => { + test('rob', () => { + expect(rob([1, 2, 3, 1])).toBe(4); + expect(rob([2, 7, 9, 3, 1])).toBe(12); + }); +}); diff --git a/src/page-2/198. House Robber/rob.ts b/src/page-2/198. House Robber/rob.ts new file mode 100644 index 0000000..752f2fd --- /dev/null +++ b/src/page-2/198. House Robber/rob.ts @@ -0,0 +1,22 @@ +type Rob = (nums: number[]) => number; + +/** + * Accepted + */ +export const rob: Rob = (nums) => { + const n = nums.length; + + if (n === 0) return 0; + if (n === 1) return nums[0]; + + let prev2 = 0; // This represents dp[i - 2] + let prev1 = nums[0]; // This represents dp[i - 1] + + for (let i = 1; i < n; i++) { + const current = Math.max(nums[i] + prev2, prev1); + prev2 = prev1; + prev1 = current; + } + + return prev1; // prev1 now represents the max amount that can be robbed up to the last house +};