Skip to content

Commit 29d8014

Browse files
authored
Create count-pairs-that-form-a-complete-day-i.cpp
1 parent 42344c4 commit 29d8014

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Time: O(n + 24)
2+
// Space: O(24)
3+
4+
// freq table
5+
class Solution {
6+
public:
7+
int countCompleteDayPairs(vector<int>& hours) {
8+
int result = 0;
9+
vector<int> cnt(24);
10+
for (const auto& x : hours) {
11+
result += cnt[((-x % 24) + 24) % 24];
12+
++cnt[x % 24];
13+
}
14+
return result;
15+
}
16+
};
17+
18+
// Time: O(n^2)
19+
// Space: O(1)
20+
// brute force
21+
class Solution2 {
22+
public:
23+
int countCompleteDayPairs(vector<int>& hours) {
24+
int result = 0;
25+
for (int i = 0; i + 1 < size(hours); ++i) {
26+
for (int j = i + 1; j < size(hours); ++j) {
27+
if ((hours[i] + hours[j]) % 24 == 0) {
28+
++result;
29+
}
30+
}
31+
}
32+
return result;
33+
}
34+
};

0 commit comments

Comments
 (0)