Skip to content

Commit 0dbca56

Browse files
authored
Create count-pairs-that-form-a-complete-day-i.py
1 parent b8bb8ef commit 0dbca56

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Time: O(n + 24)
2+
# Space: O(24)
3+
4+
# freq table
5+
class Solution(object):
6+
def countCompleteDayPairs(self, hours):
7+
"""
8+
:type hours: List[int]
9+
:rtype: int
10+
"""
11+
result = 0
12+
cnt = [0]*24
13+
for x in hours:
14+
result += cnt[-x%24]
15+
cnt[x%24] += 1
16+
return result
17+
18+
19+
# Time: O(n^2)
20+
# Space: O(1)
21+
# brute force
22+
class Solution2(object):
23+
def countCompleteDayPairs(self, hours):
24+
"""
25+
:type hours: List[int]
26+
:rtype: int
27+
"""
28+
return sum((hours[i]+hours[j])%24 == 0 for i in xrange(len(hours)-1) for j in xrange(i+1, len(hours)))

0 commit comments

Comments
 (0)