Skip to content

Commit ffe5989

Browse files
authored
feat: add solutions to lc problems: No.2556,2563 (doocs#1479)
* No.2556.Disconnect Path in a Binary Matrix by at Most One Flip * No.2563.Count the Number of Fair Pairs
1 parent 5f0f44d commit ffe5989

File tree

6 files changed

+151
-2
lines changed

6 files changed

+151
-2
lines changed

solution/2500-2599/2556.Disconnect Path in a Binary Matrix by at Most One Flip/README.md

+27-2
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@
5454

5555
**方法一:两次 DFS**
5656

57-
我们先进行一次 DFS,判断从 `(0, 0)``(m - 1, n - 1)` 是否存在路径,记结果为 $a$。在 DFS 的过程中,我们将访问过的格子的值置为 $0$,以防止重复访问。
57+
我们先进行一次 DFS,判断从 $(0, 0)$$(m - 1, n - 1)$ 是否存在路径,记结果为 $a$。在 DFS 的过程中,我们将访问过的格子的值置为 $0$,以防止重复访问。
5858

59-
接下来,我们将 `(0, 0)``(m - 1, n - 1)` 的值置为 $1$,再进行一次 DFS,判断从 `(0, 0)``(m - 1, n - 1)` 是否存在路径,记结果为 $b$。在 DFS 的过程中,我们将访问过的格子的值置为 $0$,以防止重复访问
59+
接下来,我们将 $(0, 0)$$(m - 1, n - 1)$ 的值置为 $1$,再进行一次 DFS,判断从 $(0, 0)$$(m - 1, n - 1)$ 是否存在路径,记结果为 $b$。在 DFS 的过程中,我们将访问过的格子的值置为 $0$,避免重复访问
6060

6161
最后,如果 $a$ 和 $b$ 都为 `true`,则返回 `false`,否则返回 `true`
6262

@@ -168,6 +168,31 @@ func isPossibleToCutPath(grid [][]int) bool {
168168
}
169169
```
170170

171+
### **TypeScript**
172+
173+
```ts
174+
function isPossibleToCutPath(grid: number[][]): boolean {
175+
const m = grid.length;
176+
const n = grid[0].length;
177+
178+
const dfs = (i: number, j: number): boolean => {
179+
if (i >= m || j >= n || grid[i][j] !== 1) {
180+
return false;
181+
}
182+
grid[i][j] = 0;
183+
if (i === m - 1 && j === n - 1) {
184+
return true;
185+
}
186+
return dfs(i + 1, j) || dfs(i, j + 1);
187+
};
188+
const a = dfs(0, 0);
189+
grid[0][0] = 1;
190+
grid[m - 1][n - 1] = 1;
191+
const b = dfs(0, 0);
192+
return !(a && b);
193+
}
194+
```
195+
171196
### **...**
172197

173198
```

solution/2500-2599/2556.Disconnect Path in a Binary Matrix by at Most One Flip/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,31 @@ func isPossibleToCutPath(grid [][]int) bool {
145145
}
146146
```
147147

148+
### **TypeScript**
149+
150+
```ts
151+
function isPossibleToCutPath(grid: number[][]): boolean {
152+
const m = grid.length;
153+
const n = grid[0].length;
154+
155+
const dfs = (i: number, j: number): boolean => {
156+
if (i >= m || j >= n || grid[i][j] !== 1) {
157+
return false;
158+
}
159+
grid[i][j] = 0;
160+
if (i === m - 1 && j === n - 1) {
161+
return true;
162+
}
163+
return dfs(i + 1, j) || dfs(i, j + 1);
164+
};
165+
const a = dfs(0, 0);
166+
grid[0][0] = 1;
167+
grid[m - 1][n - 1] = 1;
168+
const b = dfs(0, 0);
169+
return !(a && b);
170+
}
171+
```
172+
148173
### **...**
149174

150175
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function isPossibleToCutPath(grid: number[][]): boolean {
2+
const m = grid.length;
3+
const n = grid[0].length;
4+
5+
const dfs = (i: number, j: number): boolean => {
6+
if (i >= m || j >= n || grid[i][j] !== 1) {
7+
return false;
8+
}
9+
grid[i][j] = 0;
10+
if (i === m - 1 && j === n - 1) {
11+
return true;
12+
}
13+
return dfs(i + 1, j) || dfs(i, j + 1);
14+
};
15+
const a = dfs(0, 0);
16+
grid[0][0] = 1;
17+
grid[m - 1][n - 1] = 1;
18+
const b = dfs(0, 0);
19+
return !(a && b);
20+
}

solution/2500-2599/2563.Count the Number of Fair Pairs/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,34 @@ func countFairPairs(nums []int, lower int, upper int) (ans int64) {
137137
}
138138
```
139139

140+
### **TypeScript**
141+
142+
```ts
143+
function countFairPairs(nums: number[], lower: number, upper: number): number {
144+
const search = (x: number, l: number): number => {
145+
let r = nums.length;
146+
while (l < r) {
147+
const mid = (l + r) >> 1;
148+
if (nums[mid] >= x) {
149+
r = mid;
150+
} else {
151+
l = mid + 1;
152+
}
153+
}
154+
return l;
155+
};
156+
157+
nums.sort((a, b) => a - b);
158+
let ans = 0;
159+
for (let i = 0; i < nums.length; ++i) {
160+
const j = search(lower - nums[i], i + 1);
161+
const k = search(upper - nums[i] + 1, i + 1);
162+
ans += k - j;
163+
}
164+
return ans;
165+
}
166+
```
167+
140168
### **...**
141169

142170
```

solution/2500-2599/2563.Count the Number of Fair Pairs/README_EN.md

+28
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,34 @@ func countFairPairs(nums []int, lower int, upper int) (ans int64) {
121121
}
122122
```
123123

124+
### **TypeScript**
125+
126+
```ts
127+
function countFairPairs(nums: number[], lower: number, upper: number): number {
128+
const search = (x: number, l: number): number => {
129+
let r = nums.length;
130+
while (l < r) {
131+
const mid = (l + r) >> 1;
132+
if (nums[mid] >= x) {
133+
r = mid;
134+
} else {
135+
l = mid + 1;
136+
}
137+
}
138+
return l;
139+
};
140+
141+
nums.sort((a, b) => a - b);
142+
let ans = 0;
143+
for (let i = 0; i < nums.length; ++i) {
144+
const j = search(lower - nums[i], i + 1);
145+
const k = search(upper - nums[i] + 1, i + 1);
146+
ans += k - j;
147+
}
148+
return ans;
149+
}
150+
```
151+
124152
### **...**
125153

126154
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function countFairPairs(nums: number[], lower: number, upper: number): number {
2+
const search = (x: number, l: number): number => {
3+
let r = nums.length;
4+
while (l < r) {
5+
const mid = (l + r) >> 1;
6+
if (nums[mid] >= x) {
7+
r = mid;
8+
} else {
9+
l = mid + 1;
10+
}
11+
}
12+
return l;
13+
};
14+
15+
nums.sort((a, b) => a - b);
16+
let ans = 0;
17+
for (let i = 0; i < nums.length; ++i) {
18+
const j = search(lower - nums[i], i + 1);
19+
const k = search(upper - nums[i] + 1, i + 1);
20+
ans += k - j;
21+
}
22+
return ans;
23+
}

0 commit comments

Comments
 (0)