3185.构成整天的下标对数目II
链接:3185.构成整天的下标对数目II
难度:Medium
标签:数组、哈希表、计数
简介:给你一个整数数组 hours,表示以 小时 为单位的时间,返回一个整数,表示满足 i < j 且 hours[i] + hours[j] 构成 整天 的下标对 i, j 的数目。
题解 1 - python
- 编辑时间:2024-10-23
- 执行用时:75ms
- 内存消耗:60.75MB
- 编程语言:python
- 解法介绍:取模后遍历
class Solution:
    def countCompleteDayPairs(self, hours: List[int]) -> int:
        map = defaultdict(int)
        for h in hours: map[h % 24] += 1
        arr = list(map.items())
        res = 0
        for i in range(len(arr)):
            h1, c1 = arr[i]
            if h1 * 2 % 24 == 0:
                res += (1 + c1 - 1) * (c1 - 1) // 2
            else:
                for j in range(i):
                    h2, c2 = arr[j]
                    if (h1 + h2) % 24 == 0:
                        res += c1 * c2
        return res