Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions solution/2500-2599/2527.Find Xor-Beauty of Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 。</pre>

<p><strong>示例 2:</strong></p>
Expand Down Expand Up @@ -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]$ 的异或结果。

Expand Down
14 changes: 11 additions & 3 deletions solution/2500-2599/2527.Find Xor-Beauty of Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tags:
<pre>
<strong>Input:</strong> nums = [1,4]
<strong>Output:</strong> 5
<strong>Explanation:</strong>
<strong>Explanation:</strong>
The triplets and their corresponding effective values are listed below:
- (0,0,0) with effective value ((1 | 1) &amp; 1) = 1
- (0,0,1) with effective value ((1 | 1) &amp; 4) = 0
Expand All @@ -50,7 +50,7 @@ The triplets and their corresponding effective values are listed below:
- (1,0,0) with effective value ((4 | 1) &amp; 1) = 1
- (1,0,1) with effective value ((4 | 1) &amp; 4) = 4
- (1,1,0) with effective value ((4 | 4) &amp; 1) = 0
- (1,1,1) with effective value ((4 | 4) &amp; 4) = 4
- (1,1,1) with effective value ((4 | 4) &amp; 4) = 4
Xor-beauty of array will be bitwise XOR of all beauties = 1 ^ 0 ^ 1 ^ 4 ^ 1 ^ 4 ^ 0 ^ 4 = 5.</pre>

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

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,28 +139,21 @@ 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
}

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
Expand Down Expand Up @@ -206,69 +199,4 @@ impl Solution {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### 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<int>& 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 }
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -137,28 +137,21 @@ 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
}
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
Expand Down Expand Up @@ -204,69 +197,4 @@ impl Solution {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### 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<int>& 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 }
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
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
}

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 }

This file was deleted.

This file was deleted.

This file was deleted.

Loading