作者dont (dont)
標題Re: [閒聊] 每日leetcode
時間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
```
--
https://i.imgur.com/kyBhy6o.jpeg
--
※ 發信站: 批踢踢實業坊(ptt.org.tw), 來自: 185.213.82.231 (臺灣)
※ 文章網址: https://ptt.org.tw/Marginalman/M.1727750672.A.6D4