From d96849f759bbff73abbe92fc334d03d19fa27de2 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Mon, 21 Aug 2023 01:51:33 +0000 Subject: [PATCH] feat: add solutions to lc problems: No.2556,2563 * No.2556.Disconnect Path in a Binary Matrix by at Most One Flip * No.2563.Count the Number of Fair Pairs --- .../README.md | 29 +++++++++++++++++-- .../README_EN.md | 25 ++++++++++++++++ .../Solution.ts | 20 +++++++++++++ .../README.md | 28 ++++++++++++++++++ .../README_EN.md | 28 ++++++++++++++++++ .../Solution.ts | 23 +++++++++++++++ 6 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 solution/2500-2599/2556.Disconnect Path in a Binary Matrix by at Most One Flip/Solution.ts create mode 100644 solution/2500-2599/2563.Count the Number of Fair Pairs/Solution.ts diff --git a/solution/2500-2599/2556.Disconnect Path in a Binary Matrix by at Most One Flip/README.md b/solution/2500-2599/2556.Disconnect Path in a Binary Matrix by at Most One Flip/README.md index 99622dc08ad5c..e208655e2d6ac 100644 --- a/solution/2500-2599/2556.Disconnect Path in a Binary Matrix by at Most One Flip/README.md +++ b/solution/2500-2599/2556.Disconnect Path in a Binary Matrix by at Most One Flip/README.md @@ -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`。 @@ -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); +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2556.Disconnect Path in a Binary Matrix by at Most One Flip/README_EN.md b/solution/2500-2599/2556.Disconnect Path in a Binary Matrix by at Most One Flip/README_EN.md index 9120710110e43..4c4bccce566d6 100644 --- a/solution/2500-2599/2556.Disconnect Path in a Binary Matrix by at Most One Flip/README_EN.md +++ b/solution/2500-2599/2556.Disconnect Path in a Binary Matrix by at Most One Flip/README_EN.md @@ -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); +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2556.Disconnect Path in a Binary Matrix by at Most One Flip/Solution.ts b/solution/2500-2599/2556.Disconnect Path in a Binary Matrix by at Most One Flip/Solution.ts new file mode 100644 index 0000000000000..f91bc6fa4bdc2 --- /dev/null +++ b/solution/2500-2599/2556.Disconnect Path in a Binary Matrix by at Most One Flip/Solution.ts @@ -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); +} diff --git a/solution/2500-2599/2563.Count the Number of Fair Pairs/README.md b/solution/2500-2599/2563.Count the Number of Fair Pairs/README.md index 0c2220cdab0cc..36cd9486017e0 100644 --- a/solution/2500-2599/2563.Count the Number of Fair Pairs/README.md +++ b/solution/2500-2599/2563.Count the Number of Fair Pairs/README.md @@ -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; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2563.Count the Number of Fair Pairs/README_EN.md b/solution/2500-2599/2563.Count the Number of Fair Pairs/README_EN.md index b8f57599fb362..fcc1e39c9b458 100644 --- a/solution/2500-2599/2563.Count the Number of Fair Pairs/README_EN.md +++ b/solution/2500-2599/2563.Count the Number of Fair Pairs/README_EN.md @@ -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; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2563.Count the Number of Fair Pairs/Solution.ts b/solution/2500-2599/2563.Count the Number of Fair Pairs/Solution.ts new file mode 100644 index 0000000000000..97c972e3ad103 --- /dev/null +++ b/solution/2500-2599/2563.Count the Number of Fair Pairs/Solution.ts @@ -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; +}