Skip to content

Commit 44fdf5d

Browse files
committed
feat: add js solution to lc problem: No.0198
1 parent 3a466c1 commit 44fdf5d

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

solution/0100-0199/0198.House Robber/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,20 @@ function rob(nums: number[]): number {
156156
}
157157
```
158158

159+
#### JavaScript
160+
161+
```js
162+
function rob(nums) {
163+
const n = nums.length;
164+
const f = Array(n + 1).fill(0);
165+
f[1] = nums[0];
166+
for (let i = 2; i <= n; ++i) {
167+
f[i] = Math.max(f[i - 1], f[i - 2] + nums[i - 1]);
168+
}
169+
return f[n];
170+
}
171+
```
172+
159173
#### Rust
160174

161175
```rust

solution/0100-0199/0198.House Robber/README_EN.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ function rob(nums: number[]): number {
155155
}
156156
```
157157

158+
#### JavaScript
159+
160+
```js
161+
function rob(nums) {
162+
const n = nums.length;
163+
const f = Array(n + 1).fill(0);
164+
f[1] = nums[0];
165+
for (let i = 2; i <= n; ++i) {
166+
f[i] = Math.max(f[i - 1], f[i - 2] + nums[i - 1]);
167+
}
168+
return f[n];
169+
}
170+
```
171+
158172
#### Rust
159173

160174
```rust
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function rob(nums) {
2+
const n = nums.length;
3+
const f = Array(n + 1).fill(0);
4+
f[1] = nums[0];
5+
for (let i = 2; i <= n; ++i) {
6+
f[i] = Math.max(f[i - 1], f[i - 2] + nums[i - 1]);
7+
}
8+
return f[n];
9+
}

0 commit comments

Comments
 (0)