Skip to content

Commit faa54f8

Browse files
committed
feat: No.935,1727
1 parent 6cf2380 commit faa54f8

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# 1727. Largest Submatrix With Rearrangements
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Greedy, Sorting, Matrix.
5+
- Similar Questions: Max Area of Island.
6+
7+
## Problem
8+
9+
You are given a binary matrix `matrix` of size `m x n`, and you are allowed to rearrange the **columns** of the `matrix` in any order.
10+
11+
Return **the area of the largest submatrix within **`matrix`** where **every** element of the submatrix is **`1`** after reordering the columns optimally.**
12+
13+
 
14+
Example 1:
15+
16+
![](https://assets.leetcode.com/uploads/2020/12/29/screenshot-2020-12-30-at-40536-pm.png)
17+
18+
```
19+
Input: matrix = [[0,0,1],[1,1,1],[1,0,1]]
20+
Output: 4
21+
Explanation: You can rearrange the columns as shown above.
22+
The largest submatrix of 1s, in bold, has an area of 4.
23+
```
24+
25+
Example 2:
26+
27+
![](https://assets.leetcode.com/uploads/2020/12/29/screenshot-2020-12-30-at-40852-pm.png)
28+
29+
```
30+
Input: matrix = [[1,0,1,0,1]]
31+
Output: 3
32+
Explanation: You can rearrange the columns as shown above.
33+
The largest submatrix of 1s, in bold, has an area of 3.
34+
```
35+
36+
Example 3:
37+
38+
```
39+
Input: matrix = [[1,1,0],[1,0,1]]
40+
Output: 2
41+
Explanation: Notice that you must rearrange entire columns, and there is no way to make a submatrix of 1s larger than an area of 2.
42+
```
43+
44+
 
45+
**Constraints:**
46+
47+
48+
49+
- `m == matrix.length`
50+
51+
- `n == matrix[i].length`
52+
53+
- `1 <= m * n <= 105`
54+
55+
- `matrix[i][j]` is either `0` or `1`.
56+
57+
58+
59+
## Solution
60+
61+
```javascript
62+
/**
63+
* @param {number[][]} matrix
64+
* @return {number}
65+
*/
66+
var largestSubmatrix = function(matrix) {
67+
var max = 0;
68+
for (var i = 0; i < matrix.length; i++) {
69+
for (var j = 0; j < matrix[i].length; j++) {
70+
if (matrix[i][j] !== 0 && i > 0) {
71+
matrix[i][j] = matrix[i - 1][j] + 1;
72+
}
73+
}
74+
var arr = [...matrix[i]].sort((a, b) => b - a);
75+
for (var j = 0; j < arr.length; j++) {
76+
if (arr[j] === 0) break;
77+
max = Math.max(max, arr[j] * (j + 1));
78+
}
79+
}
80+
return max;
81+
};
82+
```
83+
84+
**Explain:**
85+
86+
nope.
87+
88+
**Complexity:**
89+
90+
* Time complexity : O(n).
91+
* Space complexity : O(n).

901-1000/935. Knight Dialer.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# 935. Knight Dialer
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Dynamic Programming.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
The chess knight has a **unique movement**, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an **L**). The possible movements of chess knight are shown in this diagaram:
10+
11+
A chess knight can move as indicated in the chess diagram below:
12+
13+
![](https://assets.leetcode.com/uploads/2020/08/18/chess.jpg)
14+
15+
We have a chess knight and a phone pad as shown below, the knight **can only stand on a numeric cell** (i.e. blue cell).
16+
17+
![](https://assets.leetcode.com/uploads/2020/08/18/phone.jpg)
18+
19+
Given an integer `n`, return how many distinct phone numbers of length `n` we can dial.
20+
21+
You are allowed to place the knight **on any numeric cell** initially and then you should perform `n - 1` jumps to dial a number of length `n`. All jumps should be **valid** knight jumps.
22+
23+
As the answer may be very large, **return the answer modulo** `109 + 7`.
24+
25+
 
26+
Example 1:
27+
28+
```
29+
Input: n = 1
30+
Output: 10
31+
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
32+
```
33+
34+
Example 2:
35+
36+
```
37+
Input: n = 2
38+
Output: 20
39+
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
40+
```
41+
42+
Example 3:
43+
44+
```
45+
Input: n = 3131
46+
Output: 136006598
47+
Explanation: Please take care of the mod.
48+
```
49+
50+
 
51+
**Constraints:**
52+
53+
54+
55+
- `1 <= n <= 5000`
56+
57+
58+
59+
## Solution
60+
61+
```javascript
62+
/**
63+
* @param {number} n
64+
* @return {number}
65+
*/
66+
var knightDialer = function(n) {
67+
var res = 0;
68+
var mod = Math.pow(10, 9) + 7;
69+
var dp = Array(10).fill(0).map(() => Array(n + 1));
70+
for (var i = 0; i < 10; i++) {
71+
res += helper(n, i, dp);
72+
res %= mod;
73+
}
74+
return res;
75+
};
76+
77+
var helper = function(n, i, dp) {
78+
if (n === 1) return 1;
79+
if (i === 5) return 0;
80+
if (dp[i][n] !== undefined) return dp[i][n];
81+
var mod = Math.pow(10, 9) + 7;
82+
var jumpMap = [
83+
[4,6],
84+
[6,8],
85+
[7,9],
86+
[4,8],
87+
[3,9,0],
88+
[],
89+
[1,7,0],
90+
[2,6],
91+
[1,3],
92+
[2,4]
93+
];
94+
var res = 0;
95+
for (var j = 0; j < jumpMap[i].length; j++) {
96+
res += helper(n - 1, jumpMap[i][j], dp);
97+
res %= mod;
98+
}
99+
dp[i][n] = res;
100+
return res;
101+
};
102+
```
103+
104+
**Explain:**
105+
106+
nope.
107+
108+
**Complexity:**
109+
110+
* Time complexity : O(n).
111+
* Space complexity : O(n).

0 commit comments

Comments
 (0)