Skip to content

Commit d0ee54c

Browse files
committed
feat: solve No.576,1074
1 parent 4af151b commit d0ee54c

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# 1074. Number of Submatrices That Sum to Target
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Array, Hash Table, Matrix, Prefix Sum.
5+
- Similar Questions: Disconnect Path in a Binary Matrix by at Most One Flip.
6+
7+
## Problem
8+
9+
Given a `matrix` and a `target`, return the number of non-empty submatrices that sum to target.
10+
11+
A submatrix `x1, y1, x2, y2` is the set of all cells `matrix[x][y]` with `x1 <= x <= x2` and `y1 <= y <= y2`.
12+
13+
Two submatrices `(x1, y1, x2, y2)` and `(x1', y1', x2', y2')` are different if they have some coordinate that is different: for example, if `x1 != x1'`.
14+
15+
 
16+
Example 1:
17+
18+
![](https://assets.leetcode.com/uploads/2020/09/02/mate1.jpg)
19+
20+
```
21+
Input: matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
22+
Output: 4
23+
Explanation: The four 1x1 submatrices that only contain 0.
24+
```
25+
26+
Example 2:
27+
28+
```
29+
Input: matrix = [[1,-1],[-1,1]], target = 0
30+
Output: 5
31+
Explanation: The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix.
32+
```
33+
34+
Example 3:
35+
36+
```
37+
Input: matrix = [[904]], target = 0
38+
Output: 0
39+
```
40+
41+
 
42+
**Constraints:**
43+
44+
45+
46+
- `1 <= matrix.length <= 100`
47+
48+
- `1 <= matrix[0].length <= 100`
49+
50+
- `-1000 <= matrix[i] <= 1000`
51+
52+
- `-10^8 <= target <= 10^8`
53+
54+
55+
56+
## Solution
57+
58+
```javascript
59+
/**
60+
* @param {number[][]} matrix
61+
* @param {number} target
62+
* @return {number}
63+
*/
64+
var numSubmatrixSumTarget = function(matrix, target) {
65+
var m = matrix.length;
66+
var n = matrix[0].length;
67+
for (var i = 0; i < m; i++) {
68+
for (var j = 1; j < n; j++) {
69+
matrix[i][j] += matrix[i][j - 1];
70+
}
71+
}
72+
var res = 0;
73+
for (var j1 = 0; j1 < n; j1++) {
74+
for (var j2 = j1; j2 < n; j2++) {
75+
var map = {};
76+
var sum = 0;
77+
map[0] = 1;
78+
for (var i = 0; i < m; i++) {
79+
sum += matrix[i][j2] - (matrix[i][j1 - 1] || 0);
80+
if (map[sum - target]) res += map[sum - target];
81+
map[sum] = (map[sum] || 0) + 1;
82+
}
83+
}
84+
}
85+
return res;
86+
};
87+
```
88+
89+
**Explain:**
90+
91+
Prefix sum and hash map.
92+
93+
**Complexity:**
94+
95+
* Time complexity : O(n * n * m).
96+
* Space complexity : O(n * m).

501-600/576. Out of Boundary Paths.md

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# 576. Out of Boundary Paths
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Dynamic Programming.
5+
- Similar Questions: Knight Probability in Chessboard, Execution of All Suffix Instructions Staying in a Grid.
6+
7+
## Problem
8+
9+
There is an `m x n` grid with a ball. The ball is initially at the position `[startRow, startColumn]`. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply **at most** `maxMove` moves to the ball.
10+
11+
Given the five integers `m`, `n`, `maxMove`, `startRow`, `startColumn`, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it **modulo** `109 + 7`.
12+
13+
 
14+
Example 1:
15+
16+
![](https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_1.png)
17+
18+
```
19+
Input: m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
20+
Output: 6
21+
```
22+
23+
Example 2:
24+
25+
![](https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_2.png)
26+
27+
```
28+
Input: m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
29+
Output: 12
30+
```
31+
32+
 
33+
**Constraints:**
34+
35+
36+
37+
- `1 <= m, n <= 50`
38+
39+
- `0 <= maxMove <= 50`
40+
41+
- `0 <= startRow < m`
42+
43+
- `0 <= startColumn < n`
44+
45+
46+
47+
## Solution
48+
49+
```javascript
50+
/**
51+
* @param {number} m
52+
* @param {number} n
53+
* @param {number} maxMove
54+
* @param {number} startRow
55+
* @param {number} startColumn
56+
* @return {number}
57+
*/
58+
var findPaths = function(m, n, maxMove, startRow, startColumn) {
59+
var matrix = Array(m).fill(0).map(() => Array(n).fill(0));
60+
matrix[startRow][startColumn] = 1;
61+
var res = 0;
62+
var mod = Math.pow(10, 9) + 7;
63+
for (var k = 0; k < maxMove; k++) {
64+
var newMatrix = Array(m).fill(0).map(() => Array(n).fill(0));
65+
for (var i = 0; i < m; i++) {
66+
for (var j = 0; j < n; j++) {
67+
newMatrix[i][j] = (
68+
(matrix[i - 1] ? matrix[i - 1][j] : 0) +
69+
(matrix[i][j - 1] || 0) +
70+
(matrix[i + 1] ? matrix[i + 1][j] : 0) +
71+
(matrix[i][j + 1] || 0)
72+
) % mod;
73+
if (i === 0) res += matrix[i][j];
74+
if (i === m - 1) res += matrix[i][j];
75+
if (j === 0) res += matrix[i][j];
76+
if (j === n - 1) res += matrix[i][j];
77+
res %= mod;
78+
}
79+
}
80+
matrix = newMatrix;
81+
}
82+
return res;
83+
};
84+
```
85+
86+
**Explain:**
87+
88+
Dynamic programming.
89+
90+
**Complexity:**
91+
92+
* Time complexity : O(n).
93+
* Space complexity : O(n).

0 commit comments

Comments
 (0)