Re: [闲聊] 每日leetcode

楼主: dont   2024-10-01 10:44:30
1497. Check If Array Pairs Are Divisible by k
## 思路
建k个bucket 扫阵列把 num%k 的bucket +1
检查bucket[0] 以及 bucket[i]跟bucket[k-i]的个数
## Code
```python
class Solution:
def canArrange(self, arr: List[int], k: int) -> bool:
buckets = [0] * k
for num in arr:
buckets[num % k] += 1
for i in range(1, k//2 + 1):
if buckets[i] != buckets[-i]:
return False
return buckets[0] & 1 == 0
```

Links booklink

Contact Us: admin [ a t ] ucptt.com