Skip to content

Commit 14bf119

Browse files
committed
update: 198
1 parent 77a8a28 commit 14bf119

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
123123
| 190 | [Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [JavaScript](./src/reverse-bits/res.js) | Easy |
124124
| 196 | [Delete Duplicate Emails](https://leetcode.com/problems/delete-duplicate-emails/) | [SQL](./src/delete-duplicate-emails/res.txt) | Easy |
125125
| 197 | [Rising Temperature](https://leetcode.com/problems/rising-temperature/) | [SQL](./src/rising-temperature/res.txt) | Easy |
126+
| 198 | [house-robber](https://leetcode.com/problems/house-robber/) | [TypeScript](./src/house-robber/res.ts) | Medium |
126127
| 203 | [remove-linked-list-elements](https://leetcode.com/problems/remove-linked-list-elements/) | [TypeScript](./src/remove-linked-list-elements/res.ts) | Easy |
127128
| 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [JavaScript](./src/reverse-linked-list/res.js) | Easy |
128129
| 207 | [Course Schedule](https://leetcode.com/problems/course-schedule/) | [JavaScript](./src/course-schedule/res.js) | Medium |

src/house-robber/res.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function rob(nums: number[]): number {
2+
if (!nums.length) {
3+
return 0;
4+
}
5+
6+
const dp: number[] = new Array(nums.length).fill(0);
7+
8+
for (let i = 0; i < nums.length; i++) {
9+
if (i === 0) {
10+
dp[i] = nums[i];
11+
} else if (i === 1) {
12+
dp[i] = Math.max(nums[i], dp[i-1]);
13+
} else {
14+
dp[i] = Math.max(nums[i] + dp[i-2], dp[i-1]);
15+
}
16+
}
17+
18+
return dp[nums.length-1];
19+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy