作者dont (dont)
標題Re: [閒聊] 每日leetcode
時間2024-11-07 19:19:38
2275. Largest Combination With Bitwise AND Greater Than Zero
## 思路
記錄每個bit的數量, 回傳最大值
用count array記錄 space是O(N)
把for loop順序對調就O(1)了
eg. [1,3,4,5]
0001
0011
0100
0101
----
0213 ---> 3
## Code
space: O(N)
```python
class Solution:
def largestCombination(self, candidates: List[int]) -> int:
count = [0] * 30
for num in candidates:
for i in range(30):
if num & (1 << i):
count[i] += 1
return max(count)
```
space: O(1)
```python
class Solution:
def largestCombination(self, candidates: List[int]) -> int:
res = 0
for i in range(30):
curr = 0
for num in candidates:
if num & (1 << i):
curr += 1
res = max(res, curr)
return res
```
--
※ 發信站: 批踢踢實業坊(ptt-website.tw), 來自: 185.213.82.250 (臺灣)
※ 文章網址: https://ptt-website.tw/Marginalman/M.1730978381.A.3BA