3184. Count Pairs That Form a Complete Day I
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 77% Topics: Array, Hash Table, Counting
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n + 24)
# Space: O(24)
# freq table
class Solution(object):
def countCompleteDayPairs(self, hours):
"""
:type hours: List[int]
:rtype: int
"""
result = 0
cnt = [0]*24
for x in hours:
result += cnt[-x%24]
cnt[x%24] += 1
return result
# Time: O(n^2)
# Space: O(1)
# brute force
class Solution2(object):
def countCompleteDayPairs(self, hours):
"""
:type hours: List[int]
:rtype: int
"""
return sum((hours[i]+hours[j])%24 == 0 for i in xrange(len(hours)-1) for j in xrange(i+1, len(hours)))
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions