Difficulty: Easy | Acceptance: 78.20% | Paid: No Topics: Array, Hash Table, Counting
Given an integer array hours representing hours worked by employees, return 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 period of 24 hours. In other words, hours[i] + hours[j] is a multiple of 24.
- Examples
- Constraints
- Brute Force
- Hash Map
- Counting Array
Examples
Example 1
Input: hours = [12,12,30,24,24]
Output: 2
Explanation:
- The pair (0, 1) forms a complete day: 12 + 12 = 24.
- The pair (3, 4) forms a complete day: 24 + 24 = 48.
Example 2
Input: hours = [72,48,24,3]
Output: 3
Explanation:
- The pair (0, 1) forms a complete day: 72 + 48 = 120.
- The pair (0, 2) forms a complete day: 72 + 24 = 96.
- The pair (1, 2) forms a complete day: 48 + 24 = 72.
Constraints
1 <= hours.length <= 100
1 <= hours[i] <= 10^9
Brute Force
Intuition Check all possible pairs (i, j) where i < j and count those where the sum is divisible by 24.
Steps
- Initialize count to 0
- For each index i from 0 to n-2
- For each index j from i+1 to n-1
- If (hours[i] + hours[j]) % 24 == 0, increment count
- For each index j from i+1 to n-1
- Return count
class Solution:
def countCompleteDayPairs(self, hours: List[int]) -> int:
n = len(hours)
count = 0
for i in range(n - 1):
for j in range(i + 1, n):
if (hours[i] + hours[j]) % 24 == 0:
count += 1
return count
Complexity
- Time: O(n²)
- Space: O(1)
- Notes: Simple but inefficient for large inputs
Hash Map
Intuition For each hour, we need to find how many previous hours have a remainder that complements it to 24. Use a hash map to track remainders.
Steps
- Initialize a hash map to store counts of remainders modulo 24
- Initialize result to 0
- For each hour in hours:
- Calculate remainder = hour % 24
- Calculate complement = (24 - remainder) % 24
- Add map[complement] to result
- Increment map[remainder] by 1
- Return result
class Solution:
def countCompleteDayPairs(self, hours: List[int]) -> int:
from collections import defaultdict
remainder_count = defaultdict(int)
result = 0
for hour in hours:
remainder = hour % 24
complement = (24 - remainder) % 24
result += remainder_count[complement]
remainder_count[remainder] += 1
return result
Complexity
- Time: O(n)
- Space: O(n)
- Notes: Efficient single pass solution with hash map overhead
Counting Array
Intuition Since remainders are always in range [0, 23], use a fixed-size array instead of a hash map for better performance.
Steps
- Initialize an array of size 24 with zeros
- Initialize result to 0
- For each hour in hours:
- Calculate remainder = hour % 24
- Calculate complement = (24 - remainder) % 24
- Add count[complement] to result
- Increment count[remainder] by 1
- Return result
class Solution:
def countCompleteDayPairs(self, hours: List[int]) -> int:
count = [0] * 24
result = 0
for hour in hours:
remainder = hour % 24
complement = (24 - remainder) % 24
result += count[complement]
count[remainder] += 1
return result
Complexity
- Time: O(n)
- Space: O(1)
- Notes: Most efficient approach with constant space due to fixed array size