-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq3513.py
More file actions
70 lines (56 loc) · 1.72 KB
/
Copy pathq3513.py
File metadata and controls
70 lines (56 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from typing import *
"""3513. Number of Unique XOR Triplets I
You are given an integer array nums of length n, where nums is a permutation of the numbers in the range [1, n].
A XOR triplet is defined as the XOR of three elements nums[i] XOR nums[j] XOR nums[k] where i <= j <= k.
Return the number of unique XOR triplet values from all possible triplets (i, j, k).
"""
class Solution:
def uniqueXorTriplets(self, nums: List[int]) -> int:
N = len(nums)
nset = set()
top = 0
for i in range(1, N + 1):
xn = N ^ i
top = max(xn, top)
start = 0 if N >= 3 else 1
for i in range(start, N + 1):
nset.add(i ^ top)
nset.update(nums)
res = len(nset)
if N >= 3 and 0 not in nset:
res += 1
return res
def uniqueXorTriplets(self, nums: List[int]) -> int:
n = len(nums)
if n <= 2:
return n
# 2^bit_len
return 1 << (n.bit_length())
def uniqueXorTriplets(self, nums: List[int]) -> int:
n = len(nums)
if n <= 2:
return n
s = set(nums)
for i in range(32 - 1, -1, -1):
if 1 << i in s:
return 1 << (i + 1)
def uniqueXorTriplets(self, nums: List[int]) -> int:
n = len(nums)
if n <= 2:
return n
i = 1
while i <= n:
i = i * 2
return i
def uniqueXorTriplets(self, nums: List[int]) -> int:
n = len(nums)
bits = [2**i for i in range(33)]
if n <= 2:
return n
for i in bits:
if i > n:
return i
def test_solution():
s = Solution()
if __name__ == "__main__":
test_solution()