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示例 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; + } +}