From a4f40f7ca95dec726f5883451ad4ffab5a4e7e5e Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 1 Apr 2024 20:32:06 +0800 Subject: [PATCH 1/2] feat: add solutions to lc problems: No.3098,3102 --- .../README.md | 124 ++++++++++++++++- .../README_EN.md | 124 ++++++++++++++++- .../Solution.cpp | 28 ++++ .../Solution.go | 29 ++++ .../Solution.java | 30 +++++ .../Solution.py | 18 +++ .../README.md | 126 +++++++++++++++++- .../README_EN.md | 96 ++++++++++++- .../Solution.cpp | 22 +++ .../Solution.go | 27 ++++ .../Solution.java | 25 ++++ .../Solution.py | 18 +++ 12 files changed, 648 insertions(+), 19 deletions(-) create mode 100644 solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.cpp create mode 100644 solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.go create mode 100644 solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.java create mode 100644 solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.py create mode 100644 solution/3100-3199/3102.Minimize Manhattan Distances/Solution.cpp create mode 100644 solution/3100-3199/3102.Minimize Manhattan Distances/Solution.go create mode 100644 solution/3100-3199/3102.Minimize Manhattan Distances/Solution.java create mode 100644 solution/3100-3199/3102.Minimize Manhattan Distances/Solution.py diff --git a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README.md b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README.md index a4bd4514f76f7..7cf902e877a25 100644 --- a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README.md +++ b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README.md @@ -66,24 +66,138 @@ ## 解法 -### 方法一 +### 方法一:记忆化搜索 + +我们设计一个函数 $dfs(i, j, k, mi)$,表示当前处理到第 $i$ 个元素,上一个选取的是第 $j$ 个元素,还需要选取 $k$ 个元素,当前的最小差值为 $mi$ 时,能量和的值。那么答案就是 $dfs(0, n, k, +\infty)$。 + +函数 $dfs(i, j, k, mi)$ 的执行过程如下: + +- 如果 $i \geq n$,表示已经处理完了所有的元素,如果 $k = 0$,返回 $mi$,否则返回 $0$; +- 否则,我们可以选择不选取第 $i$ 个元素,可以获得的能量和为 $dfs(i + 1, j, k, mi)$; +- 也可以选择选取第 $i$ 个元素。如果 $j = n$,表示之前没有选取过元素,那么可以获得的能量和为 $dfs(i + 1, i, k - 1, mi)$;否则,可以获得的能量和为 $dfs(i + 1, i, k - 1, \min(mi, \text{nums}[i] - \text{nums}[j]))$。 +- 我们累加上述结果,并对 $10^9 + 7$ 取模后返回。 + +为了避免重复计算,我们可以使用记忆化搜索的方法,将已经计算过的结果保存起来。 + +时间复杂度 $O(n^5)$,空间复杂度 $O(n^5)$。其中 $n$ 为数组的长度。 ```python - +class Solution: + def sumOfPowers(self, nums: List[int], k: int) -> int: + @cache + def dfs(i: int, j: int, k: int, mi: int) -> int: + if i >= n: + return mi if k == 0 else 0 + ans = dfs(i + 1, j, k, mi) + if j == n: + ans += dfs(i + 1, i, k - 1, mi) + else: + ans += dfs(i + 1, i, k - 1, min(mi, nums[i] - nums[j])) + ans %= mod + return ans + + mod = 10**9 + 7 + n = len(nums) + nums.sort() + return dfs(0, n, k, inf) ``` ```java - +class Solution { + private Map f = new HashMap<>(); + private final int mod = (int) 1e9 + 7; + private int[] nums; + + public int sumOfPowers(int[] nums, int k) { + Arrays.sort(nums); + this.nums = nums; + return dfs(0, nums.length, k, Integer.MAX_VALUE); + } + + private int dfs(int i, int j, int k, int mi) { + if (i >= nums.length) { + return k == 0 ? mi : 0; + } + long key = (1L * mi) << 18 | (i << 12) | (j << 6) | k; + if (f.containsKey(key)) { + return f.get(key); + } + int ans = dfs(i + 1, j, k, mi); + if (j == nums.length) { + ans += dfs(i + 1, i, k - 1, mi); + } else { + ans += dfs(i + 1, i, k - 1, Math.min(mi, nums[i] - nums[j])); + } + ans %= mod; + f.put(key, ans); + return ans; + } +} ``` ```cpp - +class Solution { +public: + int sumOfPowers(vector& nums, int k) { + unordered_map f; + const int mod = 1e9 + 7; + int n = nums.size(); + sort(nums.begin(), nums.end()); + function dfs = [&](int i, int j, int k, int mi) { + if (i >= n) { + return k == 0 ? mi : 0; + } + long long key = (1LL * mi) << 18 | (i << 12) | (j << 6) | k; + if (f.contains(key)) { + return f[key]; + } + long long ans = dfs(i + 1, j, k, mi); + if (j == n) { + ans += dfs(i + 1, i, k - 1, mi); + } else { + ans += dfs(i + 1, i, k - 1, min(mi, nums[i] - nums[j])); + } + ans %= mod; + f[key] = ans; + return f[key]; + }; + return dfs(0, n, k, INT_MAX); + } +}; ``` ```go - +func sumOfPowers(nums []int, k int) int { + const mod int = 1e9 + 7 + sort.Ints(nums) + n := len(nums) + f := map[int]int{} + var dfs func(i, j, k, mi int) int + dfs = func(i, j, k, mi int) int { + if i >= n { + if k == 0 { + return mi + } + return 0 + } + key := mi<<18 | (i << 12) | (j << 6) | k + if v, ok := f[key]; ok { + return v + } + ans := dfs(i+1, j, k, mi) + if j == n { + ans += dfs(i+1, i, k-1, mi) + } else { + ans += dfs(i+1, i, k-1, min(mi, nums[i]-nums[j])) + } + ans %= mod + f[key] = ans + return ans + } + return dfs(0, n, k, math.MaxInt) +} ``` diff --git a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README_EN.md b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README_EN.md index 55d99f7895c85..61e06c094e5da 100644 --- a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README_EN.md +++ b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/README_EN.md @@ -62,24 +62,138 @@ ## Solutions -### Solution 1 +### Solution 1: Memoization Search + +We design a function $dfs(i, j, k, mi)$, which represents the energy sum value when we are currently processing the $i$-th element, the last selected element is the $j$-th element, we still need to select $k$ elements, and the current minimum difference is $mi$. The answer is $dfs(0, n, k, +\infty)$. + +The execution process of the function $dfs(i, j, k, mi)$ is as follows: + +- If $i \geq n$, it means that all elements have been processed. If $k = 0$, return $mi$, otherwise return $0$; +- Otherwise, we can choose not to select the $i$-th element, and the energy sum obtained is $dfs(i + 1, j, k, mi)$; +- We can also choose to select the $i$-th element. If $j = n$, it means that no element has been selected before, and the energy sum obtained is $dfs(i + 1, i, k - 1, mi)$; otherwise, the energy sum obtained is $dfs(i + 1, i, k - 1, \min(mi, \text{nums}[i] - \text{nums}[j]))$. +- We add up the above results, take the modulus of $10^9 + 7$, and return. + +To avoid repeated calculations, we can use the method of memoization search to save the calculated results. + +The time complexity is $O(n^5)$, and the space complexity is $O(n^5)$. Where $n$ is the length of the array. ```python - +class Solution: + def sumOfPowers(self, nums: List[int], k: int) -> int: + @cache + def dfs(i: int, j: int, k: int, mi: int) -> int: + if i >= n: + return mi if k == 0 else 0 + ans = dfs(i + 1, j, k, mi) + if j == n: + ans += dfs(i + 1, i, k - 1, mi) + else: + ans += dfs(i + 1, i, k - 1, min(mi, nums[i] - nums[j])) + ans %= mod + return ans + + mod = 10**9 + 7 + n = len(nums) + nums.sort() + return dfs(0, n, k, inf) ``` ```java - +class Solution { + private Map f = new HashMap<>(); + private final int mod = (int) 1e9 + 7; + private int[] nums; + + public int sumOfPowers(int[] nums, int k) { + Arrays.sort(nums); + this.nums = nums; + return dfs(0, nums.length, k, Integer.MAX_VALUE); + } + + private int dfs(int i, int j, int k, int mi) { + if (i >= nums.length) { + return k == 0 ? mi : 0; + } + long key = (1L * mi) << 18 | (i << 12) | (j << 6) | k; + if (f.containsKey(key)) { + return f.get(key); + } + int ans = dfs(i + 1, j, k, mi); + if (j == nums.length) { + ans += dfs(i + 1, i, k - 1, mi); + } else { + ans += dfs(i + 1, i, k - 1, Math.min(mi, nums[i] - nums[j])); + } + ans %= mod; + f.put(key, ans); + return ans; + } +} ``` ```cpp - +class Solution { +public: + int sumOfPowers(vector& nums, int k) { + unordered_map f; + const int mod = 1e9 + 7; + int n = nums.size(); + sort(nums.begin(), nums.end()); + function dfs = [&](int i, int j, int k, int mi) { + if (i >= n) { + return k == 0 ? mi : 0; + } + long long key = (1LL * mi) << 18 | (i << 12) | (j << 6) | k; + if (f.contains(key)) { + return f[key]; + } + long long ans = dfs(i + 1, j, k, mi); + if (j == n) { + ans += dfs(i + 1, i, k - 1, mi); + } else { + ans += dfs(i + 1, i, k - 1, min(mi, nums[i] - nums[j])); + } + ans %= mod; + f[key] = ans; + return f[key]; + }; + return dfs(0, n, k, INT_MAX); + } +}; ``` ```go - +func sumOfPowers(nums []int, k int) int { + const mod int = 1e9 + 7 + sort.Ints(nums) + n := len(nums) + f := map[int]int{} + var dfs func(i, j, k, mi int) int + dfs = func(i, j, k, mi int) int { + if i >= n { + if k == 0 { + return mi + } + return 0 + } + key := mi<<18 | (i << 12) | (j << 6) | k + if v, ok := f[key]; ok { + return v + } + ans := dfs(i+1, j, k, mi) + if j == n { + ans += dfs(i+1, i, k-1, mi) + } else { + ans += dfs(i+1, i, k-1, min(mi, nums[i]-nums[j])) + } + ans %= mod + f[key] = ans + return ans + } + return dfs(0, n, k, math.MaxInt) +} ``` diff --git a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.cpp b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.cpp new file mode 100644 index 0000000000000..6b6e24b913e51 --- /dev/null +++ b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int sumOfPowers(vector& nums, int k) { + unordered_map f; + const int mod = 1e9 + 7; + int n = nums.size(); + sort(nums.begin(), nums.end()); + function dfs = [&](int i, int j, int k, int mi) { + if (i >= n) { + return k == 0 ? mi : 0; + } + long long key = (1LL * mi) << 18 | (i << 12) | (j << 6) | k; + if (f.contains(key)) { + return f[key]; + } + long long ans = dfs(i + 1, j, k, mi); + if (j == n) { + ans += dfs(i + 1, i, k - 1, mi); + } else { + ans += dfs(i + 1, i, k - 1, min(mi, nums[i] - nums[j])); + } + ans %= mod; + f[key] = ans; + return f[key]; + }; + return dfs(0, n, k, INT_MAX); + } +}; \ No newline at end of file diff --git a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.go b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.go new file mode 100644 index 0000000000000..75b1528dde09e --- /dev/null +++ b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.go @@ -0,0 +1,29 @@ +func sumOfPowers(nums []int, k int) int { + const mod int = 1e9 + 7 + sort.Ints(nums) + n := len(nums) + f := map[int]int{} + var dfs func(i, j, k, mi int) int + dfs = func(i, j, k, mi int) int { + if i >= n { + if k == 0 { + return mi + } + return 0 + } + key := mi<<18 | (i << 12) | (j << 6) | k + if v, ok := f[key]; ok { + return v + } + ans := dfs(i+1, j, k, mi) + if j == n { + ans += dfs(i+1, i, k-1, mi) + } else { + ans += dfs(i+1, i, k-1, min(mi, nums[i]-nums[j])) + } + ans %= mod + f[key] = ans + return ans + } + return dfs(0, n, k, math.MaxInt) +} \ No newline at end of file diff --git a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.java b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.java new file mode 100644 index 0000000000000..ee6e753cf1ada --- /dev/null +++ b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.java @@ -0,0 +1,30 @@ +class Solution { + private Map f = new HashMap<>(); + private final int mod = (int) 1e9 + 7; + private int[] nums; + + public int sumOfPowers(int[] nums, int k) { + Arrays.sort(nums); + this.nums = nums; + return dfs(0, nums.length, k, Integer.MAX_VALUE); + } + + private int dfs(int i, int j, int k, int mi) { + if (i >= nums.length) { + return k == 0 ? mi : 0; + } + long key = (1L * mi) << 18 | (i << 12) | (j << 6) | k; + if (f.containsKey(key)) { + return f.get(key); + } + int ans = dfs(i + 1, j, k, mi); + if (j == nums.length) { + ans += dfs(i + 1, i, k - 1, mi); + } else { + ans += dfs(i + 1, i, k - 1, Math.min(mi, nums[i] - nums[j])); + } + ans %= mod; + f.put(key, ans); + return ans; + } +} \ No newline at end of file diff --git a/solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.py b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.py new file mode 100644 index 0000000000000..89d6cc5d3688e --- /dev/null +++ b/solution/3000-3099/3098.Find the Sum of Subsequence Powers/Solution.py @@ -0,0 +1,18 @@ +class Solution: + def sumOfPowers(self, nums: List[int], k: int) -> int: + @cache + def dfs(i: int, j: int, k: int, mi: int) -> int: + if i >= n: + return mi if k == 0 else 0 + ans = dfs(i + 1, j, k, mi) + if j == n: + ans += dfs(i + 1, i, k - 1, mi) + else: + ans += dfs(i + 1, i, k - 1, min(mi, nums[i] - nums[j])) + ans %= mod + return ans + + mod = 10**9 + 7 + n = len(nums) + nums.sort() + return dfs(0, n, k, inf) diff --git a/solution/3100-3199/3102.Minimize Manhattan Distances/README.md b/solution/3100-3199/3102.Minimize Manhattan Distances/README.md index 9b33fbe048b88..9cc6eb17a2ad3 100644 --- a/solution/3100-3199/3102.Minimize Manhattan Distances/README.md +++ b/solution/3100-3199/3102.Minimize Manhattan Distances/README.md @@ -49,24 +49,140 @@ ## 解法 -### 方法一 +### 方法一:有序集合 + +对于两个点 $(x_1, y_1)$ 和 $(x_2, y_2)$,它们的曼哈顿距离为 $|x_1 - x_2| + |y_1 - y_2|$。我们可以将其转化为 $\max(x_1 - x_2, x_2 - x_1) + \max(y_1 - y_2, y_2 - y_1)$,即: + +$$ +|x_1 - x_2| + |y_1 - y_2| = \max \begin{cases} +x_1 - x_2 + y_1 - y_2 \\ +x_2 - x_1 + y_2 - y_1 \\ +x_1 - x_2 + y_2 - y_1 \\ +x_2 - x_1 + y_1 - y_2 +\end{cases} +$$ + +整理可得: + +$$ +|x_1 - x_2| + |y_1 - y_2| = \max \begin{cases} +(x_1 + y_1) - (x_2 + y_2) \\ +(x_2 + y_2) - (x_1 + y_1) \\ +(x_1 - y_1) - (x_2 - y_2) \\ +(x_2 - y_2) - (x_1 - y_1) +\end{cases} +$$ + +其中,前两项可以表示为 $\max(\max(x_1 + y_1, x_2 + y_2) - \min(x_1 + y_1, x_2 + y_2))$,后两项可以表示为 $\max(\max(x_1 - y_1, x_2 - y_2) - \min(x_1 - y_1, x_2 - y_2))$。 + +因此,我们可以将所有点按照 $x + y$ 和 $x - y$ 的值分别存入两个有序集合中,然后枚举每个点,移除该点后,更新有序集合中的值,计算最大值和最小值的差值,取最小值即可。 + +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为点的个数。 ```python - +from sortedcontainers import SortedList + + +class Solution: + def minimumDistance(self, points: List[List[int]]) -> int: + sl1 = SortedList() + sl2 = SortedList() + for x, y in points: + sl1.add(x + y) + sl2.add(x - y) + ans = inf + for x, y in points: + sl1.remove(x + y) + sl2.remove(x - y) + ans = min(ans, max(sl1[-1] - sl1[0], sl2[-1] - sl2[0])) + sl1.add(x + y) + sl2.add(x - y) + return ans ``` ```java - +class Solution { + public int minimumDistance(int[][] points) { + TreeMap tm1 = new TreeMap<>(); + TreeMap tm2 = new TreeMap<>(); + for (int[] p : points) { + int x = p[0], y = p[1]; + tm1.merge(x + y, 1, Integer::sum); + tm2.merge(x - y, 1, Integer::sum); + } + int ans = Integer.MAX_VALUE; + for (int[] p : points) { + int x = p[0], y = p[1]; + if (tm1.merge(x + y, -1, Integer::sum) == 0) { + tm1.remove(x + y); + } + if (tm2.merge(x - y, -1, Integer::sum) == 0) { + tm2.remove(x - y); + } + ans = Math.min(ans, Math.max(tm1.lastKey() - tm1.firstKey(), tm2.lastKey() - tm2.firstKey())); + tm1.merge(x + y, 1, Integer::sum); + tm2.merge(x - y, 1, Integer::sum); + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + int minimumDistance(vector>& points) { + multiset st1; + multiset st2; + for (auto& p : points) { + int x = p[0], y = p[1]; + st1.insert(x + y); + st2.insert(x - y); + } + int ans = INT_MAX; + for (auto& p : points) { + int x = p[0], y = p[1]; + st1.erase(st1.find(x + y)); + st2.erase(st2.find(x - y)); + ans = min(ans, max(*st1.rbegin() - *st1.begin(), *st2.rbegin() - *st2.begin())); + st1.insert(x + y); + st2.insert(x - y); + } + return ans; + } +}; ``` ```go - +func minimumDistance(points [][]int) int { + st1 := redblacktree.New[int, int]() + st2 := redblacktree.New[int, int]() + merge := func(st *redblacktree.Tree[int, int], x, v int) { + c, _ := st.Get(x) + if c+v == 0 { + st.Remove(x) + } else { + st.Put(x, c+v) + } + } + for _, p := range points { + x, y := p[0], p[1] + merge(st1, x+y, 1) + merge(st2, x-y, 1) + } + ans := math.MaxInt + for _, p := range points { + x, y := p[0], p[1] + merge(st1, x+y, -1) + merge(st2, x-y, -1) + ans = min(ans, max(st1.Right().Key-st1.Left().Key, st2.Right().Key-st2.Left().Key)) + merge(st1, x+y, 1) + merge(st2, x-y, 1) + } + return ans +} ``` diff --git a/solution/3100-3199/3102.Minimize Manhattan Distances/README_EN.md b/solution/3100-3199/3102.Minimize Manhattan Distances/README_EN.md index e95d6677ccc10..870ab1387d5aa 100644 --- a/solution/3100-3199/3102.Minimize Manhattan Distances/README_EN.md +++ b/solution/3100-3199/3102.Minimize Manhattan Distances/README_EN.md @@ -50,19 +50,107 @@ It can be seen that 12 is the minimum possible maximum distance between any two ```python - +from sortedcontainers import SortedList + + +class Solution: + def minimumDistance(self, points: List[List[int]]) -> int: + sl1 = SortedList() + sl2 = SortedList() + for x, y in points: + sl1.add(x + y) + sl2.add(x - y) + ans = inf + for x, y in points: + sl1.remove(x + y) + sl2.remove(x - y) + ans = min(ans, max(sl1[-1] - sl1[0], sl2[-1] - sl2[0])) + sl1.add(x + y) + sl2.add(x - y) + return ans ``` ```java - +class Solution { + public int minimumDistance(int[][] points) { + TreeMap tm1 = new TreeMap<>(); + TreeMap tm2 = new TreeMap<>(); + for (int[] p : points) { + int x = p[0], y = p[1]; + tm1.merge(x + y, 1, Integer::sum); + tm2.merge(x - y, 1, Integer::sum); + } + int ans = Integer.MAX_VALUE; + for (int[] p : points) { + int x = p[0], y = p[1]; + if (tm1.merge(x + y, -1, Integer::sum) == 0) { + tm1.remove(x + y); + } + if (tm2.merge(x - y, -1, Integer::sum) == 0) { + tm2.remove(x - y); + } + ans = Math.min(ans, Math.max(tm1.lastKey() - tm1.firstKey(), tm2.lastKey() - tm2.firstKey())); + tm1.merge(x + y, 1, Integer::sum); + tm2.merge(x - y, 1, Integer::sum); + } + return ans; + } +} ``` ```cpp - +class Solution { +public: + int minimumDistance(vector>& points) { + multiset st1; + multiset st2; + for (auto& p : points) { + int x = p[0], y = p[1]; + st1.insert(x + y); + st2.insert(x - y); + } + int ans = INT_MAX; + for (auto& p : points) { + int x = p[0], y = p[1]; + st1.erase(st1.find(x + y)); + st2.erase(st2.find(x - y)); + ans = min(ans, max(*st1.rbegin() - *st1.begin(), *st2.rbegin() - *st2.begin())); + st1.insert(x + y); + st2.insert(x - y); + } + return ans; + } +}; ``` ```go - +func minimumDistance(points [][]int) int { + st1 := redblacktree.New[int, int]() + st2 := redblacktree.New[int, int]() + merge := func(st *redblacktree.Tree[int, int], x, v int) { + c, _ := st.Get(x) + if c+v == 0 { + st.Remove(x) + } else { + st.Put(x, c+v) + } + } + for _, p := range points { + x, y := p[0], p[1] + merge(st1, x+y, 1) + merge(st2, x-y, 1) + } + ans := math.MaxInt + for _, p := range points { + x, y := p[0], p[1] + merge(st1, x+y, -1) + merge(st2, x-y, -1) + ans = min(ans, max(st1.Right().Key-st1.Left().Key, st2.Right().Key-st2.Left().Key)) + merge(st1, x+y, 1) + merge(st2, x-y, 1) + } + return ans +} ``` diff --git a/solution/3100-3199/3102.Minimize Manhattan Distances/Solution.cpp b/solution/3100-3199/3102.Minimize Manhattan Distances/Solution.cpp new file mode 100644 index 0000000000000..e34b0a576f485 --- /dev/null +++ b/solution/3100-3199/3102.Minimize Manhattan Distances/Solution.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int minimumDistance(vector>& points) { + multiset st1; + multiset st2; + for (auto& p : points) { + int x = p[0], y = p[1]; + st1.insert(x + y); + st2.insert(x - y); + } + int ans = INT_MAX; + for (auto& p : points) { + int x = p[0], y = p[1]; + st1.erase(st1.find(x + y)); + st2.erase(st2.find(x - y)); + ans = min(ans, max(*st1.rbegin() - *st1.begin(), *st2.rbegin() - *st2.begin())); + st1.insert(x + y); + st2.insert(x - y); + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3100-3199/3102.Minimize Manhattan Distances/Solution.go b/solution/3100-3199/3102.Minimize Manhattan Distances/Solution.go new file mode 100644 index 0000000000000..041ebfc165243 --- /dev/null +++ b/solution/3100-3199/3102.Minimize Manhattan Distances/Solution.go @@ -0,0 +1,27 @@ +func minimumDistance(points [][]int) int { + st1 := redblacktree.New[int, int]() + st2 := redblacktree.New[int, int]() + merge := func(st *redblacktree.Tree[int, int], x, v int) { + c, _ := st.Get(x) + if c+v == 0 { + st.Remove(x) + } else { + st.Put(x, c+v) + } + } + for _, p := range points { + x, y := p[0], p[1] + merge(st1, x+y, 1) + merge(st2, x-y, 1) + } + ans := math.MaxInt + for _, p := range points { + x, y := p[0], p[1] + merge(st1, x+y, -1) + merge(st2, x-y, -1) + ans = min(ans, max(st1.Right().Key-st1.Left().Key, st2.Right().Key-st2.Left().Key)) + merge(st1, x+y, 1) + merge(st2, x-y, 1) + } + return ans +} \ No newline at end of file diff --git a/solution/3100-3199/3102.Minimize Manhattan Distances/Solution.java b/solution/3100-3199/3102.Minimize Manhattan Distances/Solution.java new file mode 100644 index 0000000000000..323dd17ae5c41 --- /dev/null +++ b/solution/3100-3199/3102.Minimize Manhattan Distances/Solution.java @@ -0,0 +1,25 @@ +class Solution { + public int minimumDistance(int[][] points) { + TreeMap tm1 = new TreeMap<>(); + TreeMap tm2 = new TreeMap<>(); + for (int[] p : points) { + int x = p[0], y = p[1]; + tm1.merge(x + y, 1, Integer::sum); + tm2.merge(x - y, 1, Integer::sum); + } + int ans = Integer.MAX_VALUE; + for (int[] p : points) { + int x = p[0], y = p[1]; + if (tm1.merge(x + y, -1, Integer::sum) == 0) { + tm1.remove(x + y); + } + if (tm2.merge(x - y, -1, Integer::sum) == 0) { + tm2.remove(x - y); + } + ans = Math.min(ans, Math.max(tm1.lastKey() - tm1.firstKey(), tm2.lastKey() - tm2.firstKey())); + tm1.merge(x + y, 1, Integer::sum); + tm2.merge(x - y, 1, Integer::sum); + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3100-3199/3102.Minimize Manhattan Distances/Solution.py b/solution/3100-3199/3102.Minimize Manhattan Distances/Solution.py new file mode 100644 index 0000000000000..a03ba42c01950 --- /dev/null +++ b/solution/3100-3199/3102.Minimize Manhattan Distances/Solution.py @@ -0,0 +1,18 @@ +from sortedcontainers import SortedList + + +class Solution: + def minimumDistance(self, points: List[List[int]]) -> int: + sl1 = SortedList() + sl2 = SortedList() + for x, y in points: + sl1.add(x + y) + sl2.add(x - y) + ans = inf + for x, y in points: + sl1.remove(x + y) + sl2.remove(x - y) + ans = min(ans, max(sl1[-1] - sl1[0], sl2[-1] - sl2[0])) + sl1.add(x + y) + sl2.add(x - y) + return ans From 79a5715e21702cdf465d07f150d687a60e517ba4 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 1 Apr 2024 20:34:52 +0800 Subject: [PATCH 2/2] fix: style --- solution/3100-3199/3102.Minimize Manhattan Distances/README.md | 3 ++- .../3100-3199/3102.Minimize Manhattan Distances/README_EN.md | 3 ++- .../3100-3199/3102.Minimize Manhattan Distances/Solution.java | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/solution/3100-3199/3102.Minimize Manhattan Distances/README.md b/solution/3100-3199/3102.Minimize Manhattan Distances/README.md index 9cc6eb17a2ad3..1bf145bf4152b 100644 --- a/solution/3100-3199/3102.Minimize Manhattan Distances/README.md +++ b/solution/3100-3199/3102.Minimize Manhattan Distances/README.md @@ -121,7 +121,8 @@ class Solution { if (tm2.merge(x - y, -1, Integer::sum) == 0) { tm2.remove(x - y); } - ans = Math.min(ans, Math.max(tm1.lastKey() - tm1.firstKey(), tm2.lastKey() - tm2.firstKey())); + ans = Math.min( + ans, Math.max(tm1.lastKey() - tm1.firstKey(), tm2.lastKey() - tm2.firstKey())); tm1.merge(x + y, 1, Integer::sum); tm2.merge(x - y, 1, Integer::sum); } diff --git a/solution/3100-3199/3102.Minimize Manhattan Distances/README_EN.md b/solution/3100-3199/3102.Minimize Manhattan Distances/README_EN.md index 870ab1387d5aa..2ac31a6545cdc 100644 --- a/solution/3100-3199/3102.Minimize Manhattan Distances/README_EN.md +++ b/solution/3100-3199/3102.Minimize Manhattan Distances/README_EN.md @@ -89,7 +89,8 @@ class Solution { if (tm2.merge(x - y, -1, Integer::sum) == 0) { tm2.remove(x - y); } - ans = Math.min(ans, Math.max(tm1.lastKey() - tm1.firstKey(), tm2.lastKey() - tm2.firstKey())); + ans = Math.min( + ans, Math.max(tm1.lastKey() - tm1.firstKey(), tm2.lastKey() - tm2.firstKey())); tm1.merge(x + y, 1, Integer::sum); tm2.merge(x - y, 1, Integer::sum); } diff --git a/solution/3100-3199/3102.Minimize Manhattan Distances/Solution.java b/solution/3100-3199/3102.Minimize Manhattan Distances/Solution.java index 323dd17ae5c41..d42e2afa76947 100644 --- a/solution/3100-3199/3102.Minimize Manhattan Distances/Solution.java +++ b/solution/3100-3199/3102.Minimize Manhattan Distances/Solution.java @@ -16,7 +16,8 @@ public int minimumDistance(int[][] points) { if (tm2.merge(x - y, -1, Integer::sum) == 0) { tm2.remove(x - y); } - ans = Math.min(ans, Math.max(tm1.lastKey() - tm1.firstKey(), tm2.lastKey() - tm2.firstKey())); + ans = Math.min( + ans, Math.max(tm1.lastKey() - tm1.firstKey(), tm2.lastKey() - tm2.firstKey())); tm1.merge(x + y, 1, Integer::sum); tm2.merge(x - y, 1, Integer::sum); }