Skip to content

Commit d2066c1

Browse files
committed
Add solution #198
1 parent a68657e commit d2066c1

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
189|[Rotate Array](./0189-rotate-array.js)|Medium|
8787
190|[Reverse Bits](./0190-reverse-bits.js)|Easy|
8888
191|[Number of 1 Bits](./0191-number-of-1-bits.js)|Easy|
89+
198|[House Robber](./0198-house-robber.js)|Medium|
8990
203|[Remove Linked List Elements](./0203-remove-linked-list-elements.js)|Easy|
9091
206|[Reverse Linked List](./0206-reverse-linked-list.js)|Easy|
9192
214|[Shortest Palindrome](./0214-shortest-palindrome.js)|Hard|

solutions/0198-house-robber.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 198. House Robber
3+
* https://leetcode.com/problems/house-robber/
4+
* Difficulty: Medium
5+
*
6+
* You are a professional robber planning to rob houses along a street. Each house has a certain
7+
* amount of money stashed, the only constraint stopping you from robbing each of them is that
8+
* adjacent houses have security systems connected and it will automatically contact the police
9+
* if two adjacent houses were broken into on the same night.
10+
*
11+
* Given an integer array nums representing the amount of money of each house, return the maximum
12+
* amount of money you can rob tonight without alerting the police.
13+
*/
14+
15+
/**
16+
* @param {number[]} nums
17+
* @return {number}
18+
*/
19+
var rob = function(nums) {
20+
let previous = 0;
21+
let current = 0;
22+
23+
for (const n of nums) {
24+
const temp = previous;
25+
previous = current;
26+
current = Math.max(temp + n, previous);
27+
}
28+
29+
return Math.max(current, previous);
30+
};

0 commit comments

Comments
 (0)