[LeetCode] Count Pairs That Form a Complete Day I

3184. Count Pairs That Form a Complete Day I

Given an integer array hours representing times in hours, return an integer denoting the number of pairs i, j where i < j and hours[i] + hours[j] forms a complete day.

A complete day is defined as a time duration that is an exact multiple of 24 hours.

For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int countCompleteDayPairs(vector<int>& hours) {
unordered_map<int, int> freq;
int res = 0;
for(auto& h : hours) {
int t = h % 24, x = (24 - t + 24) % 24;
res += freq[x];
freq[t]++;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/06/16/PS/LeetCode/count-pairs-that-form-a-complete-day-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.