Skip to content

Commit 525045b

Browse files
authored
feat: add solutions to lc problem: No.2536 (#4832)
1 parent 2b46c96 commit 525045b

File tree

11 files changed

+168
-221
lines changed

11 files changed

+168
-221
lines changed

solution/2500-2599/2527.Find Xor-Beauty of Array/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ tags:
5151
- (1,0,0) 有效值为 ((4 | 1) & 1) = 1
5252
- (1,0,1) 有效值为 ((4 | 1) & 4) = 4
5353
- (1,1,0) 有效值为 ((4 | 4) & 1) = 0
54-
- (1,1,1) 有效值为 ((4 | 4) & 4) = 4
54+
- (1,1,1) 有效值为 ((4 | 4) & 4) = 4
5555
数组的异或美丽值为所有有效值的按位异或 1 ^ 0 ^ 1 ^ 4 ^ 1 ^ 4 ^ 0 ^ 4 = 5 。</pre>
5656

5757
<p><strong>示例 2:</strong></p>
@@ -79,9 +79,9 @@ tags:
7979

8080
### 方法一:位运算
8181

82-
我们首先考虑 $i$ 与 $j$ 不相等的情况,此时 $(nums[i] | nums[j]) \& nums[k]$$(nums[j] | nums[i]) \& nums[k]$ 的结果是相同的,两者的异或结果为 $0$。
82+
我们首先考虑 $i$ 与 $j$ 不相等的情况,此时 `((nums[i] | nums[j]) & nums[k])``((nums[j] | nums[i]) & nums[k])` 的结果是相同的,两者的异或结果为 $0$。
8383

84-
因此,我们只需要考虑 $i$ 与 $j$ 相等的情况。此时 $(nums[i] | nums[j]) \& nums[k] = nums[i] \& nums[k]$,如果 $i \neq k$,那么与 $nums[k] \& nums[i]$ 的结果是相同的,这些值的异或结果为 $0$。
84+
因此,我们只需要考虑 $i$ 与 $j$ 相等的情况。此时 `((nums[i] | nums[j]) & nums[k]) = (nums[i] & nums[k])`,如果 $i \neq k$,那么与 `nums[k] & nums[i]` 的结果是相同的,这些值的异或结果为 $0$。
8585

8686
因此,我们最终只需要考虑 $i = j = k$ 的情况,那么答案就是所有 $nums[i]$ 的异或结果。
8787

solution/2500-2599/2527.Find Xor-Beauty of Array/README_EN.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ tags:
4141
<pre>
4242
<strong>Input:</strong> nums = [1,4]
4343
<strong>Output:</strong> 5
44-
<strong>Explanation:</strong>
44+
<strong>Explanation:</strong>
4545
The triplets and their corresponding effective values are listed below:
4646
- (0,0,0) with effective value ((1 | 1) &amp; 1) = 1
4747
- (0,0,1) with effective value ((1 | 1) &amp; 4) = 0
@@ -50,7 +50,7 @@ The triplets and their corresponding effective values are listed below:
5050
- (1,0,0) with effective value ((4 | 1) &amp; 1) = 1
5151
- (1,0,1) with effective value ((4 | 1) &amp; 4) = 4
5252
- (1,1,0) with effective value ((4 | 4) &amp; 1) = 0
53-
- (1,1,1) with effective value ((4 | 4) &amp; 4) = 4
53+
- (1,1,1) with effective value ((4 | 4) &amp; 4) = 4
5454
Xor-beauty of array will be bitwise XOR of all beauties = 1 ^ 0 ^ 1 ^ 4 ^ 1 ^ 4 ^ 0 ^ 4 = 5.</pre>
5555

5656
<p><strong class="example">Example 2:</strong></p>
@@ -75,7 +75,15 @@ Xor-beauty of array will be bitwise XOR of all beauties = 1 ^ 0 ^ 1 ^ 4 ^ 1 ^ 4
7575

7676
<!-- solution:start -->
7777

78-
### Solution 1
78+
### Solution 1: Bit Manipulation
79+
80+
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$.
81+
82+
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$.
83+
84+
Therefore, we ultimately only need to consider the case where $i = j = k$, and the answer is the XOR result of all $nums[i]$.
85+
86+
The time complexity is $O(n)$ and the space complexity is $O(1)$, where $n$ is the length of the array.
7987

8088
<!-- tabs:start -->
8189

solution/2500-2599/2530.Maximal Score After Applying K Operations/README.md

Lines changed: 7 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -139,28 +139,21 @@ public:
139139
140140
```go
141141
func maxKelements(nums []int, k int) (ans int64) {
142-
h := &hp{nums}
143-
heap.Init(h)
142+
h := hp{nums}
143+
heap.Init(&h)
144144
for ; k > 0; k-- {
145-
v := h.pop()
146-
ans += int64(v)
147-
h.push((v + 2) / 3)
145+
ans += int64(h.IntSlice[0])
146+
h.IntSlice[0] = (h.IntSlice[0] + 2) / 3
147+
heap.Fix(&h, 0)
148148
}
149149
return
150150
}
151151
152152
type hp struct{ sort.IntSlice }
153153
154154
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
155-
func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }
156-
func (h *hp) Pop() any {
157-
a := h.IntSlice
158-
v := a[len(a)-1]
159-
h.IntSlice = a[:len(a)-1]
160-
return v
161-
}
162-
func (h *hp) push(v int) { heap.Push(h, v) }
163-
func (h *hp) pop() int { return heap.Pop(h).(int) }
155+
func (hp) Push(any) {}
156+
func (hp) Pop() (_ any) { return }
164157
```
165158

166159
#### TypeScript
@@ -206,69 +199,4 @@ impl Solution {
206199

207200
<!-- solution:end -->
208201

209-
<!-- solution:start -->
210-
211-
### 方法二
212-
213-
<!-- tabs:start -->
214-
215-
#### Python3
216-
217-
```python
218-
class Solution:
219-
def maxKelements(self, nums: List[int], k: int) -> int:
220-
for i, v in enumerate(nums):
221-
nums[i] = -v
222-
heapify(nums)
223-
ans = 0
224-
for _ in range(k):
225-
ans -= heapreplace(nums, -ceil(-nums[0] / 3))
226-
return ans
227-
```
228-
229-
#### C++
230-
231-
```cpp
232-
class Solution {
233-
public:
234-
long long maxKelements(vector<int>& nums, int k) {
235-
make_heap(nums.begin(), nums.end());
236-
long long ans = 0;
237-
while (k--) {
238-
int v = nums[0];
239-
ans += v;
240-
pop_heap(nums.begin(), nums.end());
241-
nums.back() = (v + 2) / 3;
242-
push_heap(nums.begin(), nums.end());
243-
}
244-
return ans;
245-
}
246-
};
247-
```
248-
249-
#### Go
250-
251-
```go
252-
func maxKelements(nums []int, k int) (ans int64) {
253-
h := hp{nums}
254-
heap.Init(&h)
255-
for ; k > 0; k-- {
256-
ans += int64(h.IntSlice[0])
257-
h.IntSlice[0] = (h.IntSlice[0] + 2) / 3
258-
heap.Fix(&h, 0)
259-
}
260-
return
261-
}
262-
263-
type hp struct{ sort.IntSlice }
264-
265-
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
266-
func (hp) Push(any) {}
267-
func (hp) Pop() (_ any) { return }
268-
```
269-
270-
<!-- tabs:end -->
271-
272-
<!-- solution:end -->
273-
274202
<!-- problem:end -->

solution/2500-2599/2530.Maximal Score After Applying K Operations/README_EN.md

Lines changed: 7 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -137,28 +137,21 @@ public:
137137
138138
```go
139139
func maxKelements(nums []int, k int) (ans int64) {
140-
h := &hp{nums}
141-
heap.Init(h)
140+
h := hp{nums}
141+
heap.Init(&h)
142142
for ; k > 0; k-- {
143-
v := h.pop()
144-
ans += int64(v)
145-
h.push((v + 2) / 3)
143+
ans += int64(h.IntSlice[0])
144+
h.IntSlice[0] = (h.IntSlice[0] + 2) / 3
145+
heap.Fix(&h, 0)
146146
}
147147
return
148148
}
149149
150150
type hp struct{ sort.IntSlice }
151151
152152
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
153-
func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }
154-
func (h *hp) Pop() any {
155-
a := h.IntSlice
156-
v := a[len(a)-1]
157-
h.IntSlice = a[:len(a)-1]
158-
return v
159-
}
160-
func (h *hp) push(v int) { heap.Push(h, v) }
161-
func (h *hp) pop() int { return heap.Pop(h).(int) }
153+
func (hp) Push(any) {}
154+
func (hp) Pop() (_ any) { return }
162155
```
163156

164157
#### TypeScript
@@ -204,69 +197,4 @@ impl Solution {
204197

205198
<!-- solution:end -->
206199

207-
<!-- solution:start -->
208-
209-
### Solution 2
210-
211-
<!-- tabs:start -->
212-
213-
#### Python3
214-
215-
```python
216-
class Solution:
217-
def maxKelements(self, nums: List[int], k: int) -> int:
218-
for i, v in enumerate(nums):
219-
nums[i] = -v
220-
heapify(nums)
221-
ans = 0
222-
for _ in range(k):
223-
ans -= heapreplace(nums, -ceil(-nums[0] / 3))
224-
return ans
225-
```
226-
227-
#### C++
228-
229-
```cpp
230-
class Solution {
231-
public:
232-
long long maxKelements(vector<int>& nums, int k) {
233-
make_heap(nums.begin(), nums.end());
234-
long long ans = 0;
235-
while (k--) {
236-
int v = nums[0];
237-
ans += v;
238-
pop_heap(nums.begin(), nums.end());
239-
nums.back() = (v + 2) / 3;
240-
push_heap(nums.begin(), nums.end());
241-
}
242-
return ans;
243-
}
244-
};
245-
```
246-
247-
#### Go
248-
249-
```go
250-
func maxKelements(nums []int, k int) (ans int64) {
251-
h := hp{nums}
252-
heap.Init(&h)
253-
for ; k > 0; k-- {
254-
ans += int64(h.IntSlice[0])
255-
h.IntSlice[0] = (h.IntSlice[0] + 2) / 3
256-
heap.Fix(&h, 0)
257-
}
258-
return
259-
}
260-
261-
type hp struct{ sort.IntSlice }
262-
263-
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
264-
func (hp) Push(any) {}
265-
func (hp) Pop() (_ any) { return }
266-
```
267-
268-
<!-- tabs:end -->
269-
270-
<!-- solution:end -->
271-
272200
<!-- problem:end -->
Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
func maxKelements(nums []int, k int) (ans int64) {
2-
h := &hp{nums}
3-
heap.Init(h)
2+
h := hp{nums}
3+
heap.Init(&h)
44
for ; k > 0; k-- {
5-
v := h.pop()
6-
ans += int64(v)
7-
h.push((v + 2) / 3)
5+
ans += int64(h.IntSlice[0])
6+
h.IntSlice[0] = (h.IntSlice[0] + 2) / 3
7+
heap.Fix(&h, 0)
88
}
99
return
1010
}
1111

1212
type hp struct{ sort.IntSlice }
1313

1414
func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }
15-
func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }
16-
func (h *hp) Pop() any {
17-
a := h.IntSlice
18-
v := a[len(a)-1]
19-
h.IntSlice = a[:len(a)-1]
20-
return v
21-
}
22-
func (h *hp) push(v int) { heap.Push(h, v) }
23-
func (h *hp) pop() int { return heap.Pop(h).(int) }
15+
func (hp) Push(any) {}
16+
func (hp) Pop() (_ any) { return }

solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution2.cpp

Lines changed: 0 additions & 15 deletions
This file was deleted.

solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution2.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

solution/2500-2599/2530.Maximal Score After Applying K Operations/Solution2.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)