Skip to content

Commit 2b373c6

Browse files
committed
[Function add]
1. Finished leetcode solution with tag dp.
1 parent d74fdbb commit 2b373c6

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@
319319

320320
[173. Binary Search Tree Iterator](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/173.%20Binary%20Search%20Tree%20Iterator.md)
321321

322+
[174. Dungeon Game](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/174.%20Dungeon20Game.md)
323+
322324
[175. Combine Two Tables](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/175.%20Combine%20Two%20Tables.md)
323325

324326
[176. Second Highest Salary](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/176.%20Second%20Highest%20Salary.md)
@@ -599,7 +601,22 @@
599601
* [312. Burst Balloons](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/312.%20Burst%20Balloons.md)
600602
* [664. Strange Printer](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/664.%20Strange%20Printer.md)
601603
* [741. Cherry Pickup](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/741.%20Cherry%20Pickup.md)
602-
604+
* [546. Remove Boxes](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/546.%20Remove%20Boxes.md)
605+
* [943. Find the Shortest Superstring](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/943.%20Find%20the%20Shortest%20Superstring.md)
606+
* [980. Unique Paths III](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/980.%20Unique%20Paths%20III.md)
607+
* [996. Number of Squareful Arrays](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/996.%20Number%20of%20Squareful%20Arrays.md)
608+
* [62. Unique Paths](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/62.%20Unique%20Paths.md)
609+
* [63. Unique Paths II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/63.%20Unique%20Paths%20II.md)
610+
* [64. Minimum Path Sum](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/64.%20Minimum%20Path%20Sum.md)
611+
* [120. Triangle](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/120.%20Triangle.md)
612+
* [174. Dungeon Game](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/174.%20Dungeon20Game.md)
613+
* [931. Minimum Falling Path Sum](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/931.%20Minimum%20Falling%20Path%20Sum.md)
614+
* [85. Maximal Rectangle](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/85.%20Maximal%20Rectangle.md)
615+
* [221. Maximal Square](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/221.%20Maximal%20Square.md)
616+
* [304. Range Sum Query 2D - Immutable](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/304.%20Range%20Sum%20Query%202D%20-%20Immutable.md)
617+
* [688. Knight Probability in Chessboard](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/688.%20Knight%20Probability%20in%20Chessboard.md)
618+
* [576. Out of Boundary Paths](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/576.%20Out%20of%20Boundary%20Paths.md)
619+
* [935. Knight Dialer](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/935.%20Knight%20Dialer.md)
603620

604621
## Algorithm(4th_Edition)
605622
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## 576. Out of Boundary Paths
2+
3+
### Question:
4+
There is an m by n grid with a ball. Given the start coordinate (i,j) of the ball, you can move the ball to adjacent cell or cross the grid boundary in four directions (up, down, left, right). However, you can at most move N times. Find out the number of paths to move the ball out of grid boundary. The answer may be very large, return it after mod 109 + 7.
5+
6+
```
7+
Example 1:
8+
9+
Input: m = 2, n = 2, N = 2, i = 0, j = 0
10+
Output: 6
11+
12+
Example 2:
13+
14+
Input: m = 1, n = 3, N = 3, i = 0, j = 1
15+
Output: 12
16+
```
17+
18+
Note:
19+
* Once you move the ball out of boundary, you cannot move it back.
20+
* The length and height of the grid is in range [1,50].
21+
* N is in range [0,50].
22+
23+
### Solution:
24+
* Method 1: DP
25+
```Java
26+
class Solution {
27+
private static final int[][] dir = new int[][]{{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
28+
public int findPaths(int m, int n, int N, int i, int j) {
29+
int[][][] dp = new int[N + 1][m][n];
30+
for(int k = 1; k <= N; k++){
31+
for(int r = 0; r < m; r++){
32+
for(int c = 0; c < n; c++){
33+
// (r, c) means the current position of a node.
34+
int tempRow = 0, tempCol = 0;
35+
for(int d = 0; d < 4; d++){
36+
// (tempRow, tempCol) means the next posible position
37+
tempRow = r + dir[d][0];
38+
tempCol = c + dir[d][1];
39+
if(tempRow < 0 || tempRow >= m || tempCol < 0 || tempCol >= n){
40+
// if next position is out of boundary, current index add 1.
41+
dp[k][r][c] += 1;
42+
}else{
43+
// if next position is within the boundary, we add that ways to current one.
44+
dp[k][r][c] = (dp[k][r][c] + dp[k - 1][tempRow][tempCol]) % 1000000007;
45+
}
46+
}
47+
}
48+
}
49+
}
50+
return dp[N][i][j];
51+
}
52+
}
53+
```
54+
55+
### Reference
56+
1. [花花酱 LeetCode 576. Out of Boundary Paths](http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-576-out-of-boundary-paths/)
57+

leetcode/935. Knight Dialer.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## 935. Knight Dialer
2+
3+
### Question:
4+
A chess knight can move as indicated in the chess diagram below:
5+
![Imgur](https://i.imgur.com/PyYrxUv.png)
6+
7+
This time, we place our chess knight on any numbered key of a phone pad (indicated above), and the knight makes N-1 hops. Each hop must be from one key to another numbered key.
8+
9+
Each time it lands on a key (including the initial placement of the knight), it presses the number of that key, pressing N digits total.
10+
11+
How many distinct numbers can you dial in this manner?
12+
13+
Since the answer may be large, output the answer modulo 10^9 + 7.
14+
15+
```
16+
Example 1:
17+
18+
Input: 1
19+
Output: 10
20+
21+
Example 2:
22+
23+
Input: 2
24+
Output: 20
25+
26+
Example 3:
27+
28+
Input: 3
29+
Output: 46
30+
```
31+
32+
Note:
33+
* 1 <= N <= 5000
34+
35+
36+
37+
### Solution:
38+
* Method 1: DP O(10 * n^3) AC 1136ms
39+
![Imgur](https://i.imgur.com/tea63Ay.png)
40+
41+
* Method 2: dp AC 18ms 93.43%
42+
1. use a rotate array to save temp result so we can reduce Memory space from O(12 * N) to O(12 * 2)
43+
2. If we can change the way we calculate for the knight moving, we just can use a 1-D array and we don't need to to any validation process.
44+
![Imgur](https://i.imgur.com/Uz06asU.png)
45+
46+
### Reference
47+
1. [花花酱 LeetCode 935. Knight Dialer](https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-935-knight-dialer/)

0 commit comments

Comments
 (0)