File tree 2 files changed +31
-0
lines changed
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 86
86
189|[ Rotate Array] ( ./0189-rotate-array.js ) |Medium|
87
87
190|[ Reverse Bits] ( ./0190-reverse-bits.js ) |Easy|
88
88
191|[ Number of 1 Bits] ( ./0191-number-of-1-bits.js ) |Easy|
89
+ 198|[ House Robber] ( ./0198-house-robber.js ) |Medium|
89
90
203|[ Remove Linked List Elements] ( ./0203-remove-linked-list-elements.js ) |Easy|
90
91
206|[ Reverse Linked List] ( ./0206-reverse-linked-list.js ) |Easy|
91
92
214|[ Shortest Palindrome] ( ./0214-shortest-palindrome.js ) |Hard|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments