Skip to content

Commit 5754059

Browse files
authored
Merge pull request #3590 from shivan2004/main
Added 3184-count-pairs-that-form-a-complete-day-I
2 parents f4becf5 + caed3d4 commit 5754059

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
id: search-in-a-binary-search-tree
3+
title: Count Pairs That Form a Complete Day I
4+
sidebar_label: 3184 - Count Pairs That Form a Complete Day I
5+
tags:
6+
- Array
7+
- Hashtable
8+
- Counting
9+
description: "This is a solution to the Search in a Binary Search Tree problem on LeetCode."
10+
---
11+
## Problem Description
12+
13+
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.
14+
15+
### Example
16+
17+
**Example 1:**
18+
19+
```
20+
Input: hours = [12, 12, 30, 24, 24]
21+
Output: 2
22+
23+
Explanation:
24+
The pairs of indices that form a complete day are (0, 1) and (3, 4).
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: hours = [72, 48, 24, 3]
31+
Output: 3
32+
33+
Explanation:
34+
The pairs of indices that form a complete day are (0, 1), (0, 2), and (1, 2).
35+
```
36+
37+
### Constraints
38+
39+
- `1 <= hours.length <= 100`
40+
- `1 <= hours[i] <= 10^9`
41+
42+
## Solution Approach
43+
44+
### Intuition
45+
46+
To efficiently find pairs of hours that sum up to a multiple of 24, we can utilize a hash map to keep track of the remainders when the hours are divided by 24. This allows us to quickly check if there exists a previous hour that complements the current hour to form a complete day.
47+
48+
### Algorithm
49+
50+
1. Initialize a hash map to keep track of remainders and their counts.
51+
2. For each hour in the array:
52+
- Compute its remainder when divided by 24.
53+
- The target remainder needed to form a complete day with the current hour is `(24 - remainder) % 24`.
54+
- Check if this target remainder exists in the hash map and count the valid pairs.
55+
- Update the hash map with the current remainder.
56+
3. Return the total count of valid pairs.
57+
58+
### Complexity
59+
60+
- Time Complexity: $O(N)$, where N is the number of hours.
61+
- Space Complexity: $O(1)$, as the hash map will have at most 24 entries.
62+
63+
## Solution Implementation
64+
65+
### Code (Python):
66+
67+
```python
68+
from collections import defaultdict
69+
70+
def countCompleteDayPairs(hours):
71+
remainder_count = defaultdict(int)
72+
complete_day_pairs = 0
73+
74+
for hour in hours:
75+
remainder = hour % 24
76+
target_remainder = (24 - remainder) % 24
77+
complete_day_pairs += remainder_count[target_remainder]
78+
remainder_count[remainder] += 1
79+
80+
return complete_day_pairs
81+
82+
```
83+
84+
### Code (C++):
85+
86+
```cpp
87+
#include <vector>
88+
#include <unordered_map>
89+
using namespace std;
90+
91+
int countCompleteDayPairs(vector<int>& hours) {
92+
unordered_map<int, int> remainder_count;
93+
int complete_day_pairs = 0;
94+
95+
for (int hour : hours) {
96+
int remainder = hour % 24;
97+
int target_remainder = (24 - remainder) % 24;
98+
complete_day_pairs += remainder_count[target_remainder];
99+
remainder_count[remainder]++;
100+
}
101+
102+
return complete_day_pairs;
103+
}
104+
105+
```
106+
107+
### Code (Java):
108+
109+
```java
110+
import java.util.HashMap;
111+
import java.util.Map;
112+
113+
public class CompleteDayPairs {
114+
public int countCompleteDayPairs(int[] hours) {
115+
Map<Integer, Integer> remainderCount = new HashMap<>();
116+
int completeDayPairs = 0;
117+
118+
for (int hour : hours) {
119+
int remainder = hour % 24;
120+
int targetRemainder = (24 - remainder) % 24;
121+
completeDayPairs += remainderCount.getOrDefault(targetRemainder, 0);
122+
remainderCount.put(remainder, remainderCount.getOrDefault(remainder, 0) + 1);
123+
}
124+
125+
return completeDayPairs;
126+
}
127+
}
128+
```
129+
130+
### Code (JavaScript):
131+
132+
```javascript
133+
function countCompleteDayPairs(hours) {
134+
const remainderCount = new Map();
135+
let completeDayPairs = 0;
136+
137+
for (const hour of hours) {
138+
const remainder = hour % 24;
139+
const targetRemainder = (24 - remainder) % 24;
140+
completeDayPairs += (remainderCount.get(targetRemainder) || 0);
141+
remainderCount.set(remainder, (remainderCount.get(remainder) || 0) + 1);
142+
}
143+
144+
return completeDayPairs;
145+
}
146+
147+
```

0 commit comments

Comments
 (0)