Skip to content

Commit

Permalink
244th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Oct 27, 2024
1 parent 066a842 commit f87f4a9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | |
| --------------------------------------------------------- | -------- | ------ |
Expand Down Expand Up @@ -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 | | |
| ---------------------------------------- | ------------- | ------ |
Expand Down Expand Up @@ -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 |
Expand All @@ -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 | | |
| ---------------------- | --------------- | ------ |
Expand Down
8 changes: 8 additions & 0 deletions src/page-2/198. House Robber/rob.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
22 changes: 22 additions & 0 deletions src/page-2/198. House Robber/rob.ts
Original file line number Diff line number Diff line change
@@ -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
};

0 comments on commit f87f4a9

Please sign in to comment.