From 3909c239524daf7a0ec1b04713e5b13b862d95c4 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 14 Nov 2025 07:17:08 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2536 --- .../2527.Find Xor-Beauty of Array/README.md | 6 +- .../README_EN.md | 14 ++- .../README.md | 86 ++----------------- .../README_EN.md | 86 ++----------------- .../Solution.go | 21 ++--- .../Solution2.cpp | 15 ---- .../Solution2.go | 16 ---- .../Solution2.py | 9 -- .../README.md | 51 ++++++++++- .../README_EN.md | 45 ++++++++++ .../Solution.cs | 40 +++++++++ 11 files changed, 168 insertions(+), 221 deletions(-) delete mode 100644 solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution2.cpp delete mode 100644 solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution2.go delete mode 100644 solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution2.py create mode 100644 solution/2500-2599/2536.Increment Submatrices by One/Solution.cs diff --git a/solution/2500-2599/2527.Find Xor-Beauty of Array/README.md b/solution/2500-2599/2527.Find Xor-Beauty of Array/README.md index 4230ac5dd8991..78f983a522a57 100644 --- a/solution/2500-2599/2527.Find Xor-Beauty of Array/README.md +++ b/solution/2500-2599/2527.Find Xor-Beauty of Array/README.md @@ -51,7 +51,7 @@ tags: - (1,0,0) 有效值为 ((4 | 1) & 1) = 1 - (1,0,1) 有效值为 ((4 | 1) & 4) = 4 - (1,1,0) 有效值为 ((4 | 4) & 1) = 0 -- (1,1,1) 有效值为 ((4 | 4) & 4) = 4 +- (1,1,1) 有效值为 ((4 | 4) & 4) = 4 数组的异或美丽值为所有有效值的按位异或 1 ^ 0 ^ 1 ^ 4 ^ 1 ^ 4 ^ 0 ^ 4 = 5 。

示例 2:

@@ -79,9 +79,9 @@ tags: ### 方法一:位运算 -我们首先考虑 $i$ 与 $j$ 不相等的情况,此时 $(nums[i] | nums[j]) \& nums[k]$ 与 $(nums[j] | nums[i]) \& nums[k]$ 的结果是相同的,两者的异或结果为 $0$。 +我们首先考虑 $i$ 与 $j$ 不相等的情况,此时 `((nums[i] | nums[j]) & nums[k])` 与 `((nums[j] | nums[i]) & nums[k])` 的结果是相同的,两者的异或结果为 $0$。 -因此,我们只需要考虑 $i$ 与 $j$ 相等的情况。此时 $(nums[i] | nums[j]) \& nums[k] = nums[i] \& nums[k]$,如果 $i \neq k$,那么与 $nums[k] \& nums[i]$ 的结果是相同的,这些值的异或结果为 $0$。 +因此,我们只需要考虑 $i$ 与 $j$ 相等的情况。此时 `((nums[i] | nums[j]) & nums[k]) = (nums[i] & nums[k])`,如果 $i \neq k$,那么与 `nums[k] & nums[i]` 的结果是相同的,这些值的异或结果为 $0$。 因此,我们最终只需要考虑 $i = j = k$ 的情况,那么答案就是所有 $nums[i]$ 的异或结果。 diff --git a/solution/2500-2599/2527.Find Xor-Beauty of Array/README_EN.md b/solution/2500-2599/2527.Find Xor-Beauty of Array/README_EN.md index c1552595eaffe..33e52f4f49aab 100644 --- a/solution/2500-2599/2527.Find Xor-Beauty of Array/README_EN.md +++ b/solution/2500-2599/2527.Find Xor-Beauty of Array/README_EN.md @@ -41,7 +41,7 @@ tags:
 Input: nums = [1,4]
 Output: 5
-Explanation: 
+Explanation:
 The triplets and their corresponding effective values are listed below:
 - (0,0,0) with effective value ((1 | 1) & 1) = 1
 - (0,0,1) with effective value ((1 | 1) & 4) = 0
@@ -50,7 +50,7 @@ The triplets and their corresponding effective values are listed below:
 - (1,0,0) with effective value ((4 | 1) & 1) = 1
 - (1,0,1) with effective value ((4 | 1) & 4) = 4
 - (1,1,0) with effective value ((4 | 4) & 1) = 0
-- (1,1,1) with effective value ((4 | 4) & 4) = 4 
+- (1,1,1) with effective value ((4 | 4) & 4) = 4
 Xor-beauty of array will be bitwise XOR of all beauties = 1 ^ 0 ^ 1 ^ 4 ^ 1 ^ 4 ^ 0 ^ 4 = 5.

Example 2:

@@ -75,7 +75,15 @@ Xor-beauty of array will be bitwise XOR of all beauties = 1 ^ 0 ^ 1 ^ 4 ^ 1 ^ 4 -### Solution 1 +### Solution 1: Bit Manipulation + +We first consider the case where $i$ and $j$ are not equal. In this case, `((nums[i] | nums[j]) & nums[k])` and `((nums[j] | nums[i]) & nums[k])` produce the same result, and their XOR result is $0$. + +Therefore, we only need to consider the case where $i$ and $j$ are equal. In this case, `((nums[i] | nums[j]) & nums[k]) = (nums[i] & nums[k])`. If $i \neq k$, then this is the same as the result of `nums[k] & nums[i]`, and the XOR result of these values is $0$. + +Therefore, we ultimately only need to consider the case where $i = j = k$, and the answer is the XOR result of all $nums[i]$. + +The time complexity is $O(n)$ and the space complexity is $O(1)$, where $n$ is the length of the array. diff --git a/solution/2500-2599/2530.Maximal Score After Applying K Operations/README.md b/solution/2500-2599/2530.Maximal Score After Applying K Operations/README.md index d37deee80b869..b5d42e57cf552 100644 --- a/solution/2500-2599/2530.Maximal Score After Applying K Operations/README.md +++ b/solution/2500-2599/2530.Maximal Score After Applying K Operations/README.md @@ -139,12 +139,12 @@ public: ```go func maxKelements(nums []int, k int) (ans int64) { - h := &hp{nums} - heap.Init(h) + h := hp{nums} + heap.Init(&h) for ; k > 0; k-- { - v := h.pop() - ans += int64(v) - h.push((v + 2) / 3) + ans += int64(h.IntSlice[0]) + h.IntSlice[0] = (h.IntSlice[0] + 2) / 3 + heap.Fix(&h, 0) } return } @@ -152,15 +152,8 @@ func maxKelements(nums []int, k int) (ans int64) { type hp struct{ sort.IntSlice } func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() any { - a := h.IntSlice - v := a[len(a)-1] - h.IntSlice = a[:len(a)-1] - return v -} -func (h *hp) push(v int) { heap.Push(h, v) } -func (h *hp) pop() int { return heap.Pop(h).(int) } +func (hp) Push(any) {} +func (hp) Pop() (_ any) { return } ``` #### TypeScript @@ -206,69 +199,4 @@ impl Solution { - - -### 方法二 - - - -#### Python3 - -```python -class Solution: - def maxKelements(self, nums: List[int], k: int) -> int: - for i, v in enumerate(nums): - nums[i] = -v - heapify(nums) - ans = 0 - for _ in range(k): - ans -= heapreplace(nums, -ceil(-nums[0] / 3)) - return ans -``` - -#### C++ - -```cpp -class Solution { -public: - long long maxKelements(vector& nums, int k) { - make_heap(nums.begin(), nums.end()); - long long ans = 0; - while (k--) { - int v = nums[0]; - ans += v; - pop_heap(nums.begin(), nums.end()); - nums.back() = (v + 2) / 3; - push_heap(nums.begin(), nums.end()); - } - return ans; - } -}; -``` - -#### Go - -```go -func maxKelements(nums []int, k int) (ans int64) { - h := hp{nums} - heap.Init(&h) - for ; k > 0; k-- { - ans += int64(h.IntSlice[0]) - h.IntSlice[0] = (h.IntSlice[0] + 2) / 3 - heap.Fix(&h, 0) - } - return -} - -type hp struct{ sort.IntSlice } - -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (hp) Push(any) {} -func (hp) Pop() (_ any) { return } -``` - - - - - diff --git a/solution/2500-2599/2530.Maximal Score After Applying K Operations/README_EN.md b/solution/2500-2599/2530.Maximal Score After Applying K Operations/README_EN.md index 8e6240a02b140..70af479f1a491 100644 --- a/solution/2500-2599/2530.Maximal Score After Applying K Operations/README_EN.md +++ b/solution/2500-2599/2530.Maximal Score After Applying K Operations/README_EN.md @@ -137,12 +137,12 @@ public: ```go func maxKelements(nums []int, k int) (ans int64) { - h := &hp{nums} - heap.Init(h) + h := hp{nums} + heap.Init(&h) for ; k > 0; k-- { - v := h.pop() - ans += int64(v) - h.push((v + 2) / 3) + ans += int64(h.IntSlice[0]) + h.IntSlice[0] = (h.IntSlice[0] + 2) / 3 + heap.Fix(&h, 0) } return } @@ -150,15 +150,8 @@ func maxKelements(nums []int, k int) (ans int64) { type hp struct{ sort.IntSlice } func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() any { - a := h.IntSlice - v := a[len(a)-1] - h.IntSlice = a[:len(a)-1] - return v -} -func (h *hp) push(v int) { heap.Push(h, v) } -func (h *hp) pop() int { return heap.Pop(h).(int) } +func (hp) Push(any) {} +func (hp) Pop() (_ any) { return } ``` #### TypeScript @@ -204,69 +197,4 @@ impl Solution { - - -### Solution 2 - - - -#### Python3 - -```python -class Solution: - def maxKelements(self, nums: List[int], k: int) -> int: - for i, v in enumerate(nums): - nums[i] = -v - heapify(nums) - ans = 0 - for _ in range(k): - ans -= heapreplace(nums, -ceil(-nums[0] / 3)) - return ans -``` - -#### C++ - -```cpp -class Solution { -public: - long long maxKelements(vector& nums, int k) { - make_heap(nums.begin(), nums.end()); - long long ans = 0; - while (k--) { - int v = nums[0]; - ans += v; - pop_heap(nums.begin(), nums.end()); - nums.back() = (v + 2) / 3; - push_heap(nums.begin(), nums.end()); - } - return ans; - } -}; -``` - -#### Go - -```go -func maxKelements(nums []int, k int) (ans int64) { - h := hp{nums} - heap.Init(&h) - for ; k > 0; k-- { - ans += int64(h.IntSlice[0]) - h.IntSlice[0] = (h.IntSlice[0] + 2) / 3 - heap.Fix(&h, 0) - } - return -} - -type hp struct{ sort.IntSlice } - -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (hp) Push(any) {} -func (hp) Pop() (_ any) { return } -``` - - - - - diff --git a/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution.go b/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution.go index e06fdda4d23a7..45f61523fd29c 100644 --- a/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution.go +++ b/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution.go @@ -1,10 +1,10 @@ func maxKelements(nums []int, k int) (ans int64) { - h := &hp{nums} - heap.Init(h) + h := hp{nums} + heap.Init(&h) for ; k > 0; k-- { - v := h.pop() - ans += int64(v) - h.push((v + 2) / 3) + ans += int64(h.IntSlice[0]) + h.IntSlice[0] = (h.IntSlice[0] + 2) / 3 + heap.Fix(&h, 0) } return } @@ -12,12 +12,5 @@ func maxKelements(nums []int, k int) (ans int64) { type hp struct{ sort.IntSlice } func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) } -func (h *hp) Pop() any { - a := h.IntSlice - v := a[len(a)-1] - h.IntSlice = a[:len(a)-1] - return v -} -func (h *hp) push(v int) { heap.Push(h, v) } -func (h *hp) pop() int { return heap.Pop(h).(int) } \ No newline at end of file +func (hp) Push(any) {} +func (hp) Pop() (_ any) { return } diff --git a/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution2.cpp b/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution2.cpp deleted file mode 100644 index 9f71cefbcfdee..0000000000000 --- a/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution2.cpp +++ /dev/null @@ -1,15 +0,0 @@ -class Solution { -public: - long long maxKelements(vector& nums, int k) { - make_heap(nums.begin(), nums.end()); - long long ans = 0; - while (k--) { - int v = nums[0]; - ans += v; - pop_heap(nums.begin(), nums.end()); - nums.back() = (v + 2) / 3; - push_heap(nums.begin(), nums.end()); - } - return ans; - } -}; \ No newline at end of file diff --git a/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution2.go b/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution2.go deleted file mode 100644 index 43b47d58ddc66..0000000000000 --- a/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution2.go +++ /dev/null @@ -1,16 +0,0 @@ -func maxKelements(nums []int, k int) (ans int64) { - h := hp{nums} - heap.Init(&h) - for ; k > 0; k-- { - ans += int64(h.IntSlice[0]) - h.IntSlice[0] = (h.IntSlice[0] + 2) / 3 - heap.Fix(&h, 0) - } - return -} - -type hp struct{ sort.IntSlice } - -func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (hp) Push(any) {} -func (hp) Pop() (_ any) { return } \ No newline at end of file diff --git a/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution2.py b/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution2.py deleted file mode 100644 index 919bec63516f4..0000000000000 --- a/solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution2.py +++ /dev/null @@ -1,9 +0,0 @@ -class Solution: - def maxKelements(self, nums: List[int], k: int) -> int: - for i, v in enumerate(nums): - nums[i] = -v - heapify(nums) - ans = 0 - for _ in range(k): - ans -= heapreplace(nums, -ceil(-nums[0] / 3)) - return ans diff --git a/solution/2500-2599/2536.Increment Submatrices by One/README.md b/solution/2500-2599/2536.Increment Submatrices by One/README.md index ae2c9fddb0e40..13e0ddbe297ab 100644 --- a/solution/2500-2599/2536.Increment Submatrices by One/README.md +++ b/solution/2500-2599/2536.Increment Submatrices by One/README.md @@ -40,8 +40,8 @@ tags: 输入:n = 3, queries = [[1,1,2,2],[0,0,1,1]] 输出:[[1,1,0],[1,2,1],[0,1,1]] 解释:上图所展示的分别是:初始矩阵、执行完第一个操作后的矩阵、执行完第二个操作后的矩阵。 -- 第一个操作:将左上角为 (1, 1) 且右下角为 (2, 2) 的子矩阵中的每个元素加 1 。 -- 第二个操作:将左上角为 (0, 0) 且右下角为 (1, 1) 的子矩阵中的每个元素加 1 。 +- 第一个操作:将左上角为 (1, 1) 且右下角为 (2, 2) 的子矩阵中的每个元素加 1 。 +- 第二个操作:将左上角为 (0, 0) 且右下角为 (1, 1) 的子矩阵中的每个元素加 1 。

示例 2:

@@ -51,7 +51,7 @@ tags:
 输入:n = 2, queries = [[0,0,1,1]]
 输出:[[1,1],[1,1]]
-解释:上图所展示的分别是:初始矩阵、执行完第一个操作后的矩阵。 
+解释:上图所展示的分别是:初始矩阵、执行完第一个操作后的矩阵。
 - 第一个操作:将矩阵中的每个元素加 1 。

 

@@ -297,6 +297,51 @@ impl Solution { } ``` +#### C# + +```cs +public class Solution { + public int[][] RangeAddQueries(int n, int[][] queries) { + int[][] mat = new int[n][]; + for (int i = 0; i < n; i++) { + mat[i] = new int[n]; + } + + foreach (var q in queries) { + int x1 = q[0], y1 = q[1], x2 = q[2], y2 = q[3]; + + mat[x1][y1]++; + + if (x2 + 1 < n) { + mat[x2 + 1][y1]--; + } + if (y2 + 1 < n) { + mat[x1][y2 + 1]--; + } + if (x2 + 1 < n && y2 + 1 < n) { + mat[x2 + 1][y2 + 1]++; + } + } + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (i > 0) { + mat[i][j] += mat[i - 1][j]; + } + if (j > 0) { + mat[i][j] += mat[i][j - 1]; + } + if (i > 0 && j > 0) { + mat[i][j] -= mat[i - 1][j - 1]; + } + } + } + + return mat; + } +} +``` + diff --git a/solution/2500-2599/2536.Increment Submatrices by One/README_EN.md b/solution/2500-2599/2536.Increment Submatrices by One/README_EN.md index 32c8a2e4439c1..d2e6f236538b1 100644 --- a/solution/2500-2599/2536.Increment Submatrices by One/README_EN.md +++ b/solution/2500-2599/2536.Increment Submatrices by One/README_EN.md @@ -292,6 +292,51 @@ impl Solution { } ``` +#### C# + +```cs +public class Solution { + public int[][] RangeAddQueries(int n, int[][] queries) { + int[][] mat = new int[n][]; + for (int i = 0; i < n; i++) { + mat[i] = new int[n]; + } + + foreach (var q in queries) { + int x1 = q[0], y1 = q[1], x2 = q[2], y2 = q[3]; + + mat[x1][y1]++; + + if (x2 + 1 < n) { + mat[x2 + 1][y1]--; + } + if (y2 + 1 < n) { + mat[x1][y2 + 1]--; + } + if (x2 + 1 < n && y2 + 1 < n) { + mat[x2 + 1][y2 + 1]++; + } + } + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (i > 0) { + mat[i][j] += mat[i - 1][j]; + } + if (j > 0) { + mat[i][j] += mat[i][j - 1]; + } + if (i > 0 && j > 0) { + mat[i][j] -= mat[i - 1][j - 1]; + } + } + } + + return mat; + } +} +``` + diff --git a/solution/2500-2599/2536.Increment Submatrices by One/Solution.cs b/solution/2500-2599/2536.Increment Submatrices by One/Solution.cs new file mode 100644 index 0000000000000..8ac2f6bf8afe1 --- /dev/null +++ b/solution/2500-2599/2536.Increment Submatrices by One/Solution.cs @@ -0,0 +1,40 @@ +public class Solution { + public int[][] RangeAddQueries(int n, int[][] queries) { + int[][] mat = new int[n][]; + for (int i = 0; i < n; i++) { + mat[i] = new int[n]; + } + + foreach (var q in queries) { + int x1 = q[0], y1 = q[1], x2 = q[2], y2 = q[3]; + + mat[x1][y1]++; + + if (x2 + 1 < n) { + mat[x2 + 1][y1]--; + } + if (y2 + 1 < n) { + mat[x1][y2 + 1]--; + } + if (x2 + 1 < n && y2 + 1 < n) { + mat[x2 + 1][y2 + 1]++; + } + } + + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (i > 0) { + mat[i][j] += mat[i - 1][j]; + } + if (j > 0) { + mat[i][j] += mat[i][j - 1]; + } + if (i > 0 && j > 0) { + mat[i][j] -= mat[i - 1][j - 1]; + } + } + } + + return mat; + } +}