Skip to content

Commit d74fdbb

Browse files
committed
[Function add]
1. Add leetcode solutions with tag dp.
1 parent f242a69 commit d74fdbb

6 files changed

+300
-2
lines changed

leetcode/174. Dungeon Game.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## 174. Dungeon Game
2+
3+
### Question:
4+
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.
5+
6+
The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.
7+
8+
Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers).
9+
10+
In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.
11+
12+
Write a function to determine the knight's minimum initial health so that he is able to rescue the princess.
13+
14+
For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN.
15+
```
16+
-2 (K) -3 3
17+
-5 -10 1
18+
10 30 -5 (P)
19+
```
20+
21+
Note:
22+
* The knight's health has no upper bound.
23+
* Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.
24+
25+
### Solution:
26+
* Method 1: DP O(MN) AC 96.21%
27+
```Java
28+
class Solution {
29+
public int calculateMinimumHP(int[][] dungeon) {
30+
int height = dungeon.length, width = dungeon[0].length;
31+
int[][] dp = new int[height][width];
32+
dp[height - 1][width - 1] = Math.max(1, -dungeon[height - 1][width - 1] + 1);
33+
for(int i = height - 2; i >= 0; i--)
34+
dp[i][width - 1] = Math.max(-dungeon[i][width - 1] + dp[i + 1][width - 1], 1);
35+
for(int i = width - 2; i >= 0; i--)
36+
dp[height - 1][i] = Math.max(1, -dungeon[height - 1][i] + dp[height - 1][i + 1]);
37+
for(int i = height - 2; i >= 0; i--){
38+
for(int j = width - 2; j >= 0; j--){
39+
dp[i][j] = Math.max(Math.min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j], 1);
40+
}
41+
}
42+
return dp[0][0];
43+
}
44+
}
45+
```
46+
47+
### Reference
48+
1. [花花酱 LeetCode 174. Dungeon Game](https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-174-dungeon-game/)
49+

leetcode/221. Maximal Square.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,32 @@ class Solution {
112112
return max * max;
113113
}
114114
}
115-
```
115+
```
116+
117+
### Fourth Time
118+
* Method 1: dp
119+
```Java
120+
class Solution {
121+
public int maximalSquare(char[][] matrix) {
122+
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
123+
int height = matrix.length, width = matrix[0].length;
124+
int[][] dp = new int[height][width];
125+
int side = 0;
126+
for(int i = 0; i < height; i++){
127+
for(int j = 0; j < width; j++){
128+
if(matrix[i][j] == '0') continue;
129+
else{
130+
dp[i][j] = 1;
131+
if(i > 0 && j > 0){
132+
if(dp[i - 1][j] > 0 && dp[i][j - 1] > 0 && dp[i - 1][j - 1] > 0){
133+
dp[i][j] = Math.min(dp[i - 1][j], Math.min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
134+
}
135+
}
136+
side = Math.max(side, dp[i][j]);
137+
}
138+
}
139+
}
140+
return side * side;
141+
}
142+
}
143+
```

leetcode/304. Range Sum Query 2D - Immutable.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,36 @@ class NumMatrix {
135135
* NumMatrix obj = new NumMatrix(matrix);
136136
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
137137
*/
138-
```
138+
```
139+
140+
### Thrid Time
141+
* Method 1: dp
142+
```Java
143+
class NumMatrix {
144+
private int[][] dp;
145+
public NumMatrix(int[][] matrix) {
146+
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return;
147+
int height = matrix.length, width = matrix[0].length;
148+
dp = new int[height][width];
149+
dp[0][0] = matrix[0][0];
150+
for(int i = 1; i < width; i++) dp[0][i] += dp[0][i - 1] + matrix[0][i];
151+
for(int i = 1; i < height; i++) dp[i][0] += dp[i - 1][0] +matrix[i][0];
152+
for(int i = 1; i < height; i++){
153+
for(int j = 1; j < width; j++){
154+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + matrix[i][j];
155+
}
156+
}
157+
}
158+
public int sumRegion(int row1, int col1, int row2, int col2) {
159+
return dp[row2][col2]
160+
- (col1 > 0 ? dp[row2][col1 - 1]: 0)
161+
- (row1 > 0 ? dp[row1 - 1][col2]: 0)
162+
+ (row1 > 0 && col1 > 0 ? dp[row1 - 1][col1 - 1]: 0);
163+
}
164+
}
165+
/**
166+
* Your NumMatrix object will be instantiated and called as such:
167+
* NumMatrix obj = new NumMatrix(matrix);
168+
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
169+
*/
170+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## 688. Knight Probability in Chessboard
2+
3+
### Question:
4+
On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K moves. The rows and columns are 0 indexed, so the top-left square is (0, 0), and the bottom-right square is (N-1, N-1).
5+
6+
A chess knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.
7+
8+
![Imgur](https://i.imgur.com/qkALNuc.png)
9+
10+
Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.
11+
12+
The knight continues moving until it has made exactly K moves or has moved off the chessboard. Return the probability that the knight remains on the board after it has stopped moving.
13+
14+
15+
```
16+
Example:
17+
18+
Input: 3, 2, 0, 0
19+
Output: 0.0625
20+
Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board.
21+
From each of those positions, there are also two moves that will keep the knight on the board.
22+
The total probability the knight stays on the board is 0.0625.
23+
```
24+
25+
Note:
26+
* N will be between 1 and 25.
27+
* K will be between 0 and 100.
28+
* The knight always initially starts on the board.
29+
30+
31+
32+
Note:
33+
* 1 <= A.length == A[0].length <= 100
34+
* -100 <= A[i][j] <= 100
35+
36+
37+
### Solution:
38+
* Method 1: DP O(MN) AC
39+
```Java
40+
class Solution {
41+
public double knightProbability(int N, int K, int r, int c) {
42+
double[][] pre = new double[N][N];
43+
pre[r][c] = 1D;
44+
for(int k = 1; k <= K; k++){
45+
double[][] dp = new double[N][N];
46+
for(int i = 0; i < N; i++){
47+
for(int j = 0; j < N; j++){
48+
dp[i][j] = ((i - 1 >= 0 && j - 2 >= 0) ? pre[i - 1][j - 2]: 0)
49+
+ ((i - 2 >= 0 && j - 1 >= 0) ? pre[i - 2][j - 1]: 0)
50+
+ ((i + 1 < N && j - 2 >= 0) ? pre[i + 1][j - 2]: 0)
51+
+ ((i + 2 < N && j - 1 >= 0) ? pre[i + 2][j - 1]: 0)
52+
+ ((i - 2 >= 0 && j + 1 < N) ? pre[i - 2][j + 1]: 0)
53+
+ ((i - 1 >= 0 && j + 2 < N) ? pre[i - 1][j + 2]: 0)
54+
+ ((i + 1 < N && j + 2 < N) ? pre[i + 1][j + 2]: 0)
55+
+ ((i + 2 < N && j + 1 < N) ? pre[i + 2][j + 1]: 0);
56+
}
57+
}
58+
pre = dp;
59+
}
60+
double sum = 0;
61+
for(int i = 0; i < N; i++)
62+
for(int j = 0; j < N; j++)
63+
sum += pre[i][j];
64+
return sum / Math.pow(8, K);
65+
}
66+
}
67+
```
68+

leetcode/85. Maximal Rectangle.md

+71
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,77 @@ class Solution {
118118
}
119119
}
120120
```
121+
122+
### Second Time
123+
* Method 1: Stack
124+
```Java
125+
class Solution {
126+
public int maximalRectangle(char[][] matrix) {
127+
if(matrix.length == 0 || matrix[0].length == 0) return 0;
128+
int height = matrix.length, width = matrix[0].length;
129+
int[] dp = new int[width];
130+
int res = 0;
131+
for(int i = 0; i < height; i++){
132+
for(int j = 0; j < width; j++){
133+
if(matrix[i][j] == '1') dp[j]++;
134+
else dp[j] = 0;
135+
}
136+
Stack<Integer> stack = new Stack<>();
137+
for(int c = 0; c <= width; c++){
138+
int hh = c == width ? 0: dp[c];
139+
while(!stack.isEmpty() && hh < dp[stack.peek()]){
140+
int h = dp[stack.pop()];
141+
int index = stack.isEmpty() ? -1: stack.peek();
142+
int area = h * (c - index - 1);
143+
res = Math.max(res, area);
144+
}
145+
stack.push(c);
146+
}
147+
}
148+
return res;
149+
}
150+
}
151+
```
152+
153+
* Method 2: dp
154+
```Java
155+
class Solution {
156+
public int maximalRectangle(char[][] matrix) {
157+
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
158+
int h = matrix.length, width = matrix[0].length;
159+
int[] height = new int[width];
160+
int[] left = new int[width];
161+
int[] right = new int[width];
162+
Arrays.fill(right, width);
163+
int res = 0;
164+
for(int i = 0; i < h; i++){
165+
int curLeft = 0, curRight = width;
166+
for(int j = 0; j < width; j++){ // Calculate the height array
167+
if(matrix[i][j] == '1') height[j]++;
168+
else height[j] = 0;
169+
}
170+
for(int j = 0; j < width; j++){
171+
if(matrix[i][j] == '1') left[j] = Math.max(curLeft, left[j]);
172+
else{
173+
left[j] = 0;
174+
curLeft = j + 1;
175+
}
176+
}
177+
for(int j = width - 1; j >= 0; j--){
178+
if(matrix[i][j] == '1') right[j] = Math.min(curRight, right[j]);
179+
else{
180+
right[j] = width;
181+
curRight = j;
182+
}
183+
}
184+
for(int j = 0; j < width; j++){
185+
res = Math.max(res, height[j] * (right[j] - left[j]));
186+
}
187+
}
188+
return res;
189+
}
190+
}
191+
```
121192

122193

123194
### Reference
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## 931. Minimum Falling Path Sum
2+
3+
### Question:
4+
Given a square array of integers A, we want the minimum sum of a falling path through A.
5+
6+
A falling path starts at any element in the first row, and chooses one element from each row. The next row's choice must be in a column that is different from the previous row's column by at most one.
7+
8+
```
9+
Example 1:
10+
11+
Input: [[1,2,3],[4,5,6],[7,8,9]]
12+
Output: 12
13+
Explanation:
14+
The possible falling paths are:
15+
* [1,4,7], [1,4,8], [1,5,7], [1,5,8], [1,5,9]
16+
* [2,4,7], [2,4,8], [2,5,7], [2,5,8], [2,5,9], [2,6,8], [2,6,9]
17+
* [3,5,7], [3,5,8], [3,5,9], [3,6,8], [3,6,9]
18+
19+
The falling path with the smallest sum is [1,4,7], so the answer is 12.
20+
```
21+
22+
Note:
23+
* 1 <= A.length == A[0].length <= 100
24+
* -100 <= A[i][j] <= 100
25+
26+
27+
### Solution:
28+
* Method 1: DP O(MN) AC 96.21%
29+
```Java
30+
class Solution {
31+
public int minFallingPathSum(int[][] A) {
32+
int len = A.length;
33+
if(len == 0) return 0;
34+
int[][] dp = new int[len][len];
35+
for(int i = 0; i < len; i++){
36+
dp[0][i] = A[0][i];
37+
}
38+
for(int i = 1; i < len; i++){
39+
for(int j = 0; j < len; j++){
40+
dp[i][j] = Math.min(Math.min(j > 0 ? dp[i - 1][j - 1]: Integer.MAX_VALUE, j + 1 < len ? dp[i - 1][j + 1]: Integer.MAX_VALUE), dp[i - 1][j]) + A[i][j];
41+
}
42+
}
43+
int res = Integer.MAX_VALUE;
44+
for(int i = 0; i < len; i++)
45+
res = Math.min(res, dp[len - 1][i]);
46+
return res;
47+
}
48+
}
49+
```
50+

0 commit comments

Comments
 (0)