From 12739add9570dee504ee68c8f2821c61a4a46f48 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 2 Jun 2025 16:33:46 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.3566,3567 * No.3566.Partition Array into Two Equal Product Subsets * No.3567.Minimum Absolute Difference in Sliding Submatrix --- .../README.md | 110 +++++++++++++- .../README_EN.md | 110 +++++++++++++- .../Solution.cpp | 23 +++ .../Solution.go | 20 +++ .../Solution.java | 19 +++ .../Solution.py | 13 ++ .../Solution.ts | 20 +++ .../README.md | 142 +++++++++++++++++- .../README_EN.md | 142 +++++++++++++++++- .../Solution.cpp | 26 ++++ .../Solution.go | 38 +++++ .../Solution.java | 27 ++++ .../Solution.py | 14 ++ .../Solution.ts | 24 +++ 14 files changed, 712 insertions(+), 16 deletions(-) create mode 100644 solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.cpp create mode 100644 solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.go create mode 100644 solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.java create mode 100644 solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.py create mode 100644 solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.ts create mode 100644 solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.cpp create mode 100644 solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.go create mode 100644 solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.java create mode 100644 solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.py create mode 100644 solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.ts diff --git a/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/README.md b/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/README.md index 337ab00ad9ce8..1ff1921cc2a34 100644 --- a/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/README.md +++ b/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/README.md @@ -61,32 +61,134 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3566.Pa -### 方法一 +### 方法一:二进制枚举 + +我们可以使用二进制枚举的方式来检查所有可能的子集划分。对于每个子集划分,我们可以计算两个子集的乘积,并检查它们是否都等于目标值。 + +具体地,我们可以使用一个整数 $i$ 来表示子集划分的状态,其中 $i$ 的二进制位表示每个元素是否属于第一个子集。对于每个可能的 $i$,我们可以计算两个子集的乘积,并检查它们是否都等于目标值。 + +时间复杂度 $O(2^n \times n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def checkEqualPartitions(self, nums: List[int], target: int) -> bool: + n = len(nums) + for i in range(1 << n): + x = y = 1 + for j in range(n): + if i >> j & 1: + x *= nums[j] + else: + y *= nums[j] + if x == target and y == target: + return True + return False ``` #### Java ```java - +class Solution { + public boolean checkEqualPartitions(int[] nums, long target) { + int n = nums.length; + for (int i = 0; i < 1 << n; ++i) { + long x = 1, y = 1; + for (int j = 0; j < n; ++j) { + if ((i >> j & 1) == 1) { + x *= nums[j]; + } else { + y *= nums[j]; + } + } + if (x == target && y == target) { + return true; + } + } + return false; + } +} ``` #### C++ ```cpp - +class Solution { +public: + bool checkEqualPartitions(vector& nums, long long target) { + int n = nums.size(); + for (int i = 0; i < 1 << n; ++i) { + long long x = 1, y = 1; + for (int j = 0; j < n; ++j) { + if ((i >> j & 1) == 1) { + x *= nums[j]; + } else { + y *= nums[j]; + } + if (x > target || y > target) { + break; + } + } + if (x == target && y == target) { + return true; + } + } + return false; + } +}; ``` #### Go ```go +func checkEqualPartitions(nums []int, target int64) bool { + n := len(nums) + for i := 0; i < 1<>j&1 == 1 { + x *= int64(v) + } else { + y *= int64(v) + } + if x > target || y > target { + break + } + } + if x == target && y == target { + return true + } + } + return false +} +``` +#### TypeScript + +```ts +function checkEqualPartitions(nums: number[], target: number): boolean { + const n = nums.length; + for (let i = 0; i < 1 << n; ++i) { + let [x, y] = [1, 1]; + for (let j = 0; j < n; ++j) { + if (((i >> j) & 1) === 1) { + x *= nums[j]; + } else { + y *= nums[j]; + } + if (x > target || y > target) { + break; + } + } + if (x === target && y === target) { + return true; + } + } + return false; +} ``` diff --git a/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/README_EN.md b/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/README_EN.md index cc5aa90e7eea5..07caa202fe748 100644 --- a/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/README_EN.md +++ b/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/README_EN.md @@ -57,32 +57,134 @@ A subset of an array is a selection of elements of the array. -### Solution 1 +### Solution 1: Binary Enumeration + +We can use binary enumeration to check all possible subset partitions. For each subset partition, we can calculate the product of the two subsets and check whether both are equal to the target value. + +Specifically, we can use an integer $i$ to represent the state of the subset partition, where the binary bits of $i$ indicate whether each element belongs to the first subset. For each possible $i$, we calculate the product of the two subsets and check whether both are equal to the target value. + +The time complexity is $O(2^n \times n)$, where $n$ is the length of the array. The space complexity is $O(1)$. #### Python3 ```python - +class Solution: + def checkEqualPartitions(self, nums: List[int], target: int) -> bool: + n = len(nums) + for i in range(1 << n): + x = y = 1 + for j in range(n): + if i >> j & 1: + x *= nums[j] + else: + y *= nums[j] + if x == target and y == target: + return True + return False ``` #### Java ```java - +class Solution { + public boolean checkEqualPartitions(int[] nums, long target) { + int n = nums.length; + for (int i = 0; i < 1 << n; ++i) { + long x = 1, y = 1; + for (int j = 0; j < n; ++j) { + if ((i >> j & 1) == 1) { + x *= nums[j]; + } else { + y *= nums[j]; + } + } + if (x == target && y == target) { + return true; + } + } + return false; + } +} ``` #### C++ ```cpp - +class Solution { +public: + bool checkEqualPartitions(vector& nums, long long target) { + int n = nums.size(); + for (int i = 0; i < 1 << n; ++i) { + long long x = 1, y = 1; + for (int j = 0; j < n; ++j) { + if ((i >> j & 1) == 1) { + x *= nums[j]; + } else { + y *= nums[j]; + } + if (x > target || y > target) { + break; + } + } + if (x == target && y == target) { + return true; + } + } + return false; + } +}; ``` #### Go ```go +func checkEqualPartitions(nums []int, target int64) bool { + n := len(nums) + for i := 0; i < 1<>j&1 == 1 { + x *= int64(v) + } else { + y *= int64(v) + } + if x > target || y > target { + break + } + } + if x == target && y == target { + return true + } + } + return false +} +``` +#### TypeScript + +```ts +function checkEqualPartitions(nums: number[], target: number): boolean { + const n = nums.length; + for (let i = 0; i < 1 << n; ++i) { + let [x, y] = [1, 1]; + for (let j = 0; j < n; ++j) { + if (((i >> j) & 1) === 1) { + x *= nums[j]; + } else { + y *= nums[j]; + } + if (x > target || y > target) { + break; + } + } + if (x === target && y === target) { + return true; + } + } + return false; +} ``` diff --git a/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.cpp b/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.cpp new file mode 100644 index 0000000000000..83dfa6f4cea80 --- /dev/null +++ b/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + bool checkEqualPartitions(vector& nums, long long target) { + int n = nums.size(); + for (int i = 0; i < 1 << n; ++i) { + long long x = 1, y = 1; + for (int j = 0; j < n; ++j) { + if ((i >> j & 1) == 1) { + x *= nums[j]; + } else { + y *= nums[j]; + } + if (x > target || y > target) { + break; + } + } + if (x == target && y == target) { + return true; + } + } + return false; + } +}; \ No newline at end of file diff --git a/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.go b/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.go new file mode 100644 index 0000000000000..8ef01224c83b2 --- /dev/null +++ b/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.go @@ -0,0 +1,20 @@ +func checkEqualPartitions(nums []int, target int64) bool { + n := len(nums) + for i := 0; i < 1<>j&1 == 1 { + x *= int64(v) + } else { + y *= int64(v) + } + if x > target || y > target { + break + } + } + if x == target && y == target { + return true + } + } + return false +} \ No newline at end of file diff --git a/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.java b/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.java new file mode 100644 index 0000000000000..b0634edbfd02e --- /dev/null +++ b/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.java @@ -0,0 +1,19 @@ +class Solution { + public boolean checkEqualPartitions(int[] nums, long target) { + int n = nums.length; + for (int i = 0; i < 1 << n; ++i) { + long x = 1, y = 1; + for (int j = 0; j < n; ++j) { + if ((i >> j & 1) == 1) { + x *= nums[j]; + } else { + y *= nums[j]; + } + } + if (x == target && y == target) { + return true; + } + } + return false; + } +} \ No newline at end of file diff --git a/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.py b/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.py new file mode 100644 index 0000000000000..c172245a614d4 --- /dev/null +++ b/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.py @@ -0,0 +1,13 @@ +class Solution: + def checkEqualPartitions(self, nums: List[int], target: int) -> bool: + n = len(nums) + for i in range(1 << n): + x = y = 1 + for j in range(n): + if i >> j & 1: + x *= nums[j] + else: + y *= nums[j] + if x == target and y == target: + return True + return False diff --git a/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.ts b/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.ts new file mode 100644 index 0000000000000..f8dce64e43480 --- /dev/null +++ b/solution/3500-3599/3566.Partition Array into Two Equal Product Subsets/Solution.ts @@ -0,0 +1,20 @@ +function checkEqualPartitions(nums: number[], target: number): boolean { + const n = nums.length; + for (let i = 0; i < 1 << n; ++i) { + let [x, y] = [1, 1]; + for (let j = 0; j < n; ++j) { + if (((i >> j) & 1) === 1) { + x *= nums[j]; + } else { + y *= nums[j]; + } + if (x > target || y > target) { + break; + } + } + if (x === target && y === target) { + return true; + } + } + return false; +} diff --git a/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/README.md b/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/README.md index 6878b7eff9b81..a25611668a269 100644 --- a/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/README.md +++ b/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/README.md @@ -107,32 +107,166 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3567.Mi -### 方法一 +### 方法一:枚举 + +我们可以枚举所有可能的 $k \times k$ 子矩阵的左上角坐标 $(i, j)$,对于每个子矩阵,我们可以提取出其中的所有元素,放入一个列表 $\textit{nums}$ 中。然后对 $\textit{nums}$ 进行排序,接着计算相邻的不同元素之间的绝对差,找到最小的绝对差值。最后将结果存储在一个二维数组中。 + +时间复杂度 $O((m - k + 1) \times (n - k + 1) \times k^2 \log(k))$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数,而 $k$ 是子矩阵的大小。空间复杂度 $O(k^2)$,用于存储每个子矩阵的元素。 #### Python3 ```python - +class Solution: + def minAbsDiff(self, grid: List[List[int]], k: int) -> List[List[int]]: + m, n = len(grid), len(grid[0]) + ans = [[0] * (n - k + 1) for _ in range(m - k + 1)] + for i in range(m - k + 1): + for j in range(n - k + 1): + nums = [] + for x in range(i, i + k): + for y in range(j, j + k): + nums.append(grid[x][y]) + nums.sort() + d = min((abs(a - b) for a, b in pairwise(nums) if a != b), default=0) + ans[i][j] = d + return ans ``` #### Java ```java - +class Solution { + public int[][] minAbsDiff(int[][] grid, int k) { + int m = grid.length, n = grid[0].length; + int[][] ans = new int[m - k + 1][n - k + 1]; + for (int i = 0; i <= m - k; i++) { + for (int j = 0; j <= n - k; j++) { + List nums = new ArrayList<>(); + for (int x = i; x < i + k; x++) { + for (int y = j; y < j + k; y++) { + nums.add(grid[x][y]); + } + } + Collections.sort(nums); + int d = Integer.MAX_VALUE; + for (int t = 1; t < nums.size(); t++) { + int a = nums.get(t - 1); + int b = nums.get(t); + if (a != b) { + d = Math.min(d, Math.abs(a - b)); + } + } + ans[i][j] = (d == Integer.MAX_VALUE) ? 0 : d; + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector> minAbsDiff(vector>& grid, int k) { + int m = grid.size(), n = grid[0].size(); + vector> ans(m - k + 1, vector(n - k + 1, 0)); + for (int i = 0; i <= m - k; ++i) { + for (int j = 0; j <= n - k; ++j) { + vector nums; + for (int x = i; x < i + k; ++x) { + for (int y = j; y < j + k; ++y) { + nums.push_back(grid[x][y]); + } + } + sort(nums.begin(), nums.end()); + int d = INT_MAX; + for (int t = 1; t < nums.size(); ++t) { + if (nums[t] != nums[t - 1]) { + d = min(d, abs(nums[t] - nums[t - 1])); + } + } + ans[i][j] = (d == INT_MAX) ? 0 : d; + } + } + return ans; + } +}; ``` #### Go ```go +func minAbsDiff(grid [][]int, k int) [][]int { + m, n := len(grid), len(grid[0]) + ans := make([][]int, m-k+1) + for i := range ans { + ans[i] = make([]int, n-k+1) + } + for i := 0; i <= m-k; i++ { + for j := 0; j <= n-k; j++ { + var nums []int + for x := i; x < i+k; x++ { + for y := j; y < j+k; y++ { + nums = append(nums, grid[x][y]) + } + } + sort.Ints(nums) + d := math.MaxInt + for t := 1; t < len(nums); t++ { + if nums[t] != nums[t-1] { + diff := abs(nums[t] - nums[t-1]) + if diff < d { + d = diff + } + } + } + if d != math.MaxInt { + ans[i][j] = d + } + } + } + return ans +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} +``` +#### TypeScript + +```ts +function minAbsDiff(grid: number[][], k: number): number[][] { + const m = grid.length; + const n = grid[0].length; + const ans: number[][] = Array.from({ length: m - k + 1 }, () => Array(n - k + 1).fill(0)); + for (let i = 0; i <= m - k; i++) { + for (let j = 0; j <= n - k; j++) { + const nums: number[] = []; + for (let x = i; x < i + k; x++) { + for (let y = j; y < j + k; y++) { + nums.push(grid[x][y]); + } + } + nums.sort((a, b) => a - b); + let d = Number.MAX_SAFE_INTEGER; + for (let t = 1; t < nums.length; t++) { + if (nums[t] !== nums[t - 1]) { + d = Math.min(d, Math.abs(nums[t] - nums[t - 1])); + } + } + ans[i][j] = d === Number.MAX_SAFE_INTEGER ? 0 : d; + } + } + return ans; +} ``` diff --git a/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/README_EN.md b/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/README_EN.md index bc495e3ae9e2d..166ac578a158e 100644 --- a/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/README_EN.md +++ b/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/README_EN.md @@ -103,32 +103,166 @@ A submatrix (x1, y1, x2, y2) is a matrix that is formed by choosing -### Solution 1 +### Solution 1: Enumeration + +We can enumerate all possible $k \times k$ submatrices by their top-left coordinates $(i, j)$. For each submatrix, we extract all its elements into a list $\textit{nums}$. Then, we sort $\textit{nums}$ and compute the absolute differences between adjacent distinct elements to find the minimum absolute difference. Finally, we store the result in a 2D array. + +The time complexity is $O((m - k + 1) \times (n - k + 1) \times k^2 \log(k))$, where $m$ and $n$ are the number of rows and columns of the matrix, and $k$ is the size of the submatrix. The space complexity is $O(k^2)$, used to store the elements of each submatrix. #### Python3 ```python - +class Solution: + def minAbsDiff(self, grid: List[List[int]], k: int) -> List[List[int]]: + m, n = len(grid), len(grid[0]) + ans = [[0] * (n - k + 1) for _ in range(m - k + 1)] + for i in range(m - k + 1): + for j in range(n - k + 1): + nums = [] + for x in range(i, i + k): + for y in range(j, j + k): + nums.append(grid[x][y]) + nums.sort() + d = min((abs(a - b) for a, b in pairwise(nums) if a != b), default=0) + ans[i][j] = d + return ans ``` #### Java ```java - +class Solution { + public int[][] minAbsDiff(int[][] grid, int k) { + int m = grid.length, n = grid[0].length; + int[][] ans = new int[m - k + 1][n - k + 1]; + for (int i = 0; i <= m - k; i++) { + for (int j = 0; j <= n - k; j++) { + List nums = new ArrayList<>(); + for (int x = i; x < i + k; x++) { + for (int y = j; y < j + k; y++) { + nums.add(grid[x][y]); + } + } + Collections.sort(nums); + int d = Integer.MAX_VALUE; + for (int t = 1; t < nums.size(); t++) { + int a = nums.get(t - 1); + int b = nums.get(t); + if (a != b) { + d = Math.min(d, Math.abs(a - b)); + } + } + ans[i][j] = (d == Integer.MAX_VALUE) ? 0 : d; + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector> minAbsDiff(vector>& grid, int k) { + int m = grid.size(), n = grid[0].size(); + vector> ans(m - k + 1, vector(n - k + 1, 0)); + for (int i = 0; i <= m - k; ++i) { + for (int j = 0; j <= n - k; ++j) { + vector nums; + for (int x = i; x < i + k; ++x) { + for (int y = j; y < j + k; ++y) { + nums.push_back(grid[x][y]); + } + } + sort(nums.begin(), nums.end()); + int d = INT_MAX; + for (int t = 1; t < nums.size(); ++t) { + if (nums[t] != nums[t - 1]) { + d = min(d, abs(nums[t] - nums[t - 1])); + } + } + ans[i][j] = (d == INT_MAX) ? 0 : d; + } + } + return ans; + } +}; ``` #### Go ```go +func minAbsDiff(grid [][]int, k int) [][]int { + m, n := len(grid), len(grid[0]) + ans := make([][]int, m-k+1) + for i := range ans { + ans[i] = make([]int, n-k+1) + } + for i := 0; i <= m-k; i++ { + for j := 0; j <= n-k; j++ { + var nums []int + for x := i; x < i+k; x++ { + for y := j; y < j+k; y++ { + nums = append(nums, grid[x][y]) + } + } + sort.Ints(nums) + d := math.MaxInt + for t := 1; t < len(nums); t++ { + if nums[t] != nums[t-1] { + diff := abs(nums[t] - nums[t-1]) + if diff < d { + d = diff + } + } + } + if d != math.MaxInt { + ans[i][j] = d + } + } + } + return ans +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} +``` +#### TypeScript + +```ts +function minAbsDiff(grid: number[][], k: number): number[][] { + const m = grid.length; + const n = grid[0].length; + const ans: number[][] = Array.from({ length: m - k + 1 }, () => Array(n - k + 1).fill(0)); + for (let i = 0; i <= m - k; i++) { + for (let j = 0; j <= n - k; j++) { + const nums: number[] = []; + for (let x = i; x < i + k; x++) { + for (let y = j; y < j + k; y++) { + nums.push(grid[x][y]); + } + } + nums.sort((a, b) => a - b); + let d = Number.MAX_SAFE_INTEGER; + for (let t = 1; t < nums.length; t++) { + if (nums[t] !== nums[t - 1]) { + d = Math.min(d, Math.abs(nums[t] - nums[t - 1])); + } + } + ans[i][j] = d === Number.MAX_SAFE_INTEGER ? 0 : d; + } + } + return ans; +} ``` diff --git a/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.cpp b/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.cpp new file mode 100644 index 0000000000000..2b07a219fb45d --- /dev/null +++ b/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + vector> minAbsDiff(vector>& grid, int k) { + int m = grid.size(), n = grid[0].size(); + vector> ans(m - k + 1, vector(n - k + 1, 0)); + for (int i = 0; i <= m - k; ++i) { + for (int j = 0; j <= n - k; ++j) { + vector nums; + for (int x = i; x < i + k; ++x) { + for (int y = j; y < j + k; ++y) { + nums.push_back(grid[x][y]); + } + } + sort(nums.begin(), nums.end()); + int d = INT_MAX; + for (int t = 1; t < nums.size(); ++t) { + if (nums[t] != nums[t - 1]) { + d = min(d, abs(nums[t] - nums[t - 1])); + } + } + ans[i][j] = (d == INT_MAX) ? 0 : d; + } + } + return ans; + } +}; diff --git a/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.go b/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.go new file mode 100644 index 0000000000000..7a808b904e60f --- /dev/null +++ b/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.go @@ -0,0 +1,38 @@ +func minAbsDiff(grid [][]int, k int) [][]int { + m, n := len(grid), len(grid[0]) + ans := make([][]int, m-k+1) + for i := range ans { + ans[i] = make([]int, n-k+1) + } + for i := 0; i <= m-k; i++ { + for j := 0; j <= n-k; j++ { + var nums []int + for x := i; x < i+k; x++ { + for y := j; y < j+k; y++ { + nums = append(nums, grid[x][y]) + } + } + sort.Ints(nums) + d := math.MaxInt + for t := 1; t < len(nums); t++ { + if nums[t] != nums[t-1] { + diff := abs(nums[t] - nums[t-1]) + if diff < d { + d = diff + } + } + } + if d != math.MaxInt { + ans[i][j] = d + } + } + } + return ans +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} diff --git a/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.java b/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.java new file mode 100644 index 0000000000000..25b8e42fbfd6d --- /dev/null +++ b/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.java @@ -0,0 +1,27 @@ +class Solution { + public int[][] minAbsDiff(int[][] grid, int k) { + int m = grid.length, n = grid[0].length; + int[][] ans = new int[m - k + 1][n - k + 1]; + for (int i = 0; i <= m - k; i++) { + for (int j = 0; j <= n - k; j++) { + List nums = new ArrayList<>(); + for (int x = i; x < i + k; x++) { + for (int y = j; y < j + k; y++) { + nums.add(grid[x][y]); + } + } + Collections.sort(nums); + int d = Integer.MAX_VALUE; + for (int t = 1; t < nums.size(); t++) { + int a = nums.get(t - 1); + int b = nums.get(t); + if (a != b) { + d = Math.min(d, Math.abs(a - b)); + } + } + ans[i][j] = (d == Integer.MAX_VALUE) ? 0 : d; + } + } + return ans; + } +} diff --git a/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.py b/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.py new file mode 100644 index 0000000000000..a197ff09958d4 --- /dev/null +++ b/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.py @@ -0,0 +1,14 @@ +class Solution: + def minAbsDiff(self, grid: List[List[int]], k: int) -> List[List[int]]: + m, n = len(grid), len(grid[0]) + ans = [[0] * (n - k + 1) for _ in range(m - k + 1)] + for i in range(m - k + 1): + for j in range(n - k + 1): + nums = [] + for x in range(i, i + k): + for y in range(j, j + k): + nums.append(grid[x][y]) + nums.sort() + d = min((abs(a - b) for a, b in pairwise(nums) if a != b), default=0) + ans[i][j] = d + return ans diff --git a/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.ts b/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.ts new file mode 100644 index 0000000000000..192e8ccbdea58 --- /dev/null +++ b/solution/3500-3599/3567.Minimum Absolute Difference in Sliding Submatrix/Solution.ts @@ -0,0 +1,24 @@ +function minAbsDiff(grid: number[][], k: number): number[][] { + const m = grid.length; + const n = grid[0].length; + const ans: number[][] = Array.from({ length: m - k + 1 }, () => Array(n - k + 1).fill(0)); + for (let i = 0; i <= m - k; i++) { + for (let j = 0; j <= n - k; j++) { + const nums: number[] = []; + for (let x = i; x < i + k; x++) { + for (let y = j; y < j + k; y++) { + nums.push(grid[x][y]); + } + } + nums.sort((a, b) => a - b); + let d = Number.MAX_SAFE_INTEGER; + for (let t = 1; t < nums.length; t++) { + if (nums[t] !== nums[t - 1]) { + d = Math.min(d, Math.abs(nums[t] - nums[t - 1])); + } + } + ans[i][j] = d === Number.MAX_SAFE_INTEGER ? 0 : d; + } + } + return ans; +}