Skip to content

feat: add solutions to lc problems: No.2556,2563 #1479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@

**方法一:两次 DFS**

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

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

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

Expand Down Expand Up @@ -168,6 +168,31 @@ func isPossibleToCutPath(grid [][]int) bool {
}
```

### **TypeScript**

```ts
function isPossibleToCutPath(grid: number[][]): boolean {
const m = grid.length;
const n = grid[0].length;

const dfs = (i: number, j: number): boolean => {
if (i >= m || j >= n || grid[i][j] !== 1) {
return false;
}
grid[i][j] = 0;
if (i === m - 1 && j === n - 1) {
return true;
}
return dfs(i + 1, j) || dfs(i, j + 1);
};
const a = dfs(0, 0);
grid[0][0] = 1;
grid[m - 1][n - 1] = 1;
const b = dfs(0, 0);
return !(a && b);
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,31 @@ func isPossibleToCutPath(grid [][]int) bool {
}
```

### **TypeScript**

```ts
function isPossibleToCutPath(grid: number[][]): boolean {
const m = grid.length;
const n = grid[0].length;

const dfs = (i: number, j: number): boolean => {
if (i >= m || j >= n || grid[i][j] !== 1) {
return false;
}
grid[i][j] = 0;
if (i === m - 1 && j === n - 1) {
return true;
}
return dfs(i + 1, j) || dfs(i, j + 1);
};
const a = dfs(0, 0);
grid[0][0] = 1;
grid[m - 1][n - 1] = 1;
const b = dfs(0, 0);
return !(a && b);
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function isPossibleToCutPath(grid: number[][]): boolean {
const m = grid.length;
const n = grid[0].length;

const dfs = (i: number, j: number): boolean => {
if (i >= m || j >= n || grid[i][j] !== 1) {
return false;
}
grid[i][j] = 0;
if (i === m - 1 && j === n - 1) {
return true;
}
return dfs(i + 1, j) || dfs(i, j + 1);
};
const a = dfs(0, 0);
grid[0][0] = 1;
grid[m - 1][n - 1] = 1;
const b = dfs(0, 0);
return !(a && b);
}
28 changes: 28 additions & 0 deletions solution/2500-2599/2563.Count the Number of Fair Pairs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,34 @@ func countFairPairs(nums []int, lower int, upper int) (ans int64) {
}
```

### **TypeScript**

```ts
function countFairPairs(nums: number[], lower: number, upper: number): number {
const search = (x: number, l: number): number => {
let r = nums.length;
while (l < r) {
const mid = (l + r) >> 1;
if (nums[mid] >= x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
};

nums.sort((a, b) => a - b);
let ans = 0;
for (let i = 0; i < nums.length; ++i) {
const j = search(lower - nums[i], i + 1);
const k = search(upper - nums[i] + 1, i + 1);
ans += k - j;
}
return ans;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ func countFairPairs(nums []int, lower int, upper int) (ans int64) {
}
```

### **TypeScript**

```ts
function countFairPairs(nums: number[], lower: number, upper: number): number {
const search = (x: number, l: number): number => {
let r = nums.length;
while (l < r) {
const mid = (l + r) >> 1;
if (nums[mid] >= x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
};

nums.sort((a, b) => a - b);
let ans = 0;
for (let i = 0; i < nums.length; ++i) {
const j = search(lower - nums[i], i + 1);
const k = search(upper - nums[i] + 1, i + 1);
ans += k - j;
}
return ans;
}
```

### **...**

```
Expand Down
23 changes: 23 additions & 0 deletions solution/2500-2599/2563.Count the Number of Fair Pairs/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function countFairPairs(nums: number[], lower: number, upper: number): number {
const search = (x: number, l: number): number => {
let r = nums.length;
while (l < r) {
const mid = (l + r) >> 1;
if (nums[mid] >= x) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
};

nums.sort((a, b) => a - b);
let ans = 0;
for (let i = 0; i < nums.length; ++i) {
const j = search(lower - nums[i], i + 1);
const k = search(upper - nums[i] + 1, i + 1);
ans += k - j;
}
return ans;
}