Skip to content

3185. Count Pairs That Form a Complete Day II 👍

  • Time: $O(n)$
  • Space: $O(24) = O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
 public:
  // Same as 3184. Count Pairs That Form a Complete Day I
  long long countCompleteDayPairs(vector<int>& hours) {
    long ans = 0;
    vector<int> count(24);

    for (const int hour : hours) {
      ans += count[(24 - hour % 24) % 24];
      ++count[hour % 24];
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
  // Same as 3184. Count Pairs That Form a Complete Day I
  public long countCompleteDayPairs(int[] hours) {
    long ans = 0;
    int[] count = new int[24];

    for (final int hour : hours) {
      ans += count[(24 - hour % 24) % 24];
      ++count[hour % 24];
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution:
  # Same as 3184. Count Pairs That Form a Complete Day I
  def countCompleteDayPairs(self, hours: list[int]) -> int:
    ans = 0
    count = [0] * 24

    for hour in hours:
      ans += count[(24 - hour % 24) % 24]
      count[hour % 24] += 1

    return ans