Skip to content

Commit bc818ed

Browse files
committed
finish 198, 200
1 parent af9a94d commit bc818ed

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed

101-200/198. House Robber.md

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# 198. House Robber
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Dynamic Programming.
5+
- Similar Questions: Maximum Product Subarray, House Robber II, Paint House, Paint Fence, House Robber III, Non-negative Integers without Consecutive Ones, Coin Path, Delete and Earn.
6+
7+
## Problem
8+
9+
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and **it will automatically contact the police if two adjacent houses were broken into on the same night**.
10+
11+
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight **without alerting the police**.
12+
13+
**Example 1:**
14+
15+
```
16+
Input: [1,2,3,1]
17+
Output: 4
18+
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
19+
  Total amount you can rob = 1 + 3 = 4.
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: [2,7,9,3,1]
26+
Output: 12
27+
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
28+
  Total amount you can rob = 2 + 9 + 1 = 12.
29+
```
30+
31+
## Solution 1
32+
33+
```javascript
34+
/**
35+
* @param {number[]} nums
36+
* @return {number}
37+
*/
38+
var rob = function (nums) {
39+
return helper([], 0, nums);
40+
};
41+
42+
var helper = function (dp, i, nums) {
43+
if (i >= nums.length) return 0;
44+
if (dp[i] === undefined) {
45+
dp[i] = Math.max(
46+
helper(dp, i + 1, nums),
47+
nums[i] + helper(dp, i + 2, nums)
48+
);
49+
}
50+
return dp[i];
51+
};
52+
```
53+
54+
**Explain:**
55+
56+
nope.
57+
58+
**Complexity:**
59+
60+
* Time complexity : O(n).
61+
* Space complexity : O(n).
62+
63+
## Solution 2
64+
65+
```javascript
66+
/**
67+
* @param {number[]} nums
68+
* @return {number}
69+
*/
70+
var rob = function (nums) {
71+
var len = nums.length;
72+
var dp = Array(len);
73+
for (var i = len - 1; i >= 0; i--) {
74+
dp[i] = Math.max(
75+
dp[i + 1] || 0,
76+
nums[i] + (dp[i + 2] || 0)
77+
);
78+
}
79+
return dp[0] || 0;
80+
};
81+
```
82+
83+
**Explain:**
84+
85+
nope.
86+
87+
**Complexity:**
88+
89+
* Time complexity : O(n).
90+
* Space complexity : O(n).
91+
92+
## Solution 3
93+
94+
```javascript
95+
/**
96+
* @param {number[]} nums
97+
* @return {number}
98+
*/
99+
var rob = function (nums) {
100+
var len = nums.length;
101+
var dp = [0, 0];
102+
for (var i = len - 1; i >= 0; i--) {
103+
dp = [Math.max(dp[0], nums[i] + dp[1]), dp[0]];
104+
}
105+
return dp[0];
106+
};
107+
```
108+
109+
**Explain:**
110+
111+
nope.
112+
113+
**Complexity:**
114+
115+
* Time complexity : O(n).
116+
* Space complexity : O(1).

101-200/200. Number of Islands.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# 200. Number of Islands
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Depth-first Search, Breadth-first Search, Union Find.
5+
- Similar Questions: Surrounded Regions, Walls and Gates, Number of Islands II, Number of Connected Components in an Undirected Graph, Number of Distinct Islands, Max Area of Island.
6+
7+
## Problem
8+
9+
Given a 2d grid map of ```'1'```s (land) and ```'0'```s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
10+
11+
**Example 1:**
12+
13+
```
14+
Input:
15+
11110
16+
11010
17+
11000
18+
00000
19+
20+
Output: 1
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input:
27+
11000
28+
11000
29+
00100
30+
00011
31+
32+
Output: 3
33+
```
34+
35+
## Solution
36+
37+
```javascript
38+
/**
39+
* @param {character[][]} grid
40+
* @return {number}
41+
*/
42+
var numIslands = function(grid) {
43+
var m = grid.length;
44+
var n = (grid[0] || []).length;
45+
var dp = Array(m).fill(0).map(_ => Array(n));
46+
var num = 0;
47+
for (var i = 0; i < n; i++) {
48+
for (var j = 0; j < m; j++) {
49+
if (dp[j][i] !== true && grid[j][i] === '1') {
50+
num++;
51+
mark(dp, j, i, grid);
52+
}
53+
}
54+
}
55+
return num;
56+
};
57+
58+
var mark = function (dp, j, i, grid) {
59+
if (dp[j] && dp[j][i] !== true && grid[j][i] === '1') {
60+
dp[j][i] = true;
61+
mark(dp, j - 1, i, grid);
62+
mark(dp, j + 1, i, grid);
63+
mark(dp, j, i - 1, grid);
64+
mark(dp, j, i + 1, grid);
65+
}
66+
};
67+
```
68+
69+
**Explain:**
70+
71+
nope.
72+
73+
**Complexity:**
74+
75+
* Time complexity : O(m*n).
76+
* Space complexity : O(m*n).

0 commit comments

Comments
 (0)