Skip to content

Commit 1206f97

Browse files
authored
feat: add weekly contest 402 (doocs#3113)
1 parent 1c9b2ab commit 1206f97

File tree

28 files changed

+1614
-1
lines changed

28 files changed

+1614
-1
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3184.Count%20Pairs%20That%20Form%20a%20Complete%20Day%20I/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3184. 构成整天的下标对数目 I](https://leetcode.cn/problems/count-pairs-that-form-a-complete-day-i)
10+
11+
[English Version](/solution/3100-3199/3184.Count%20Pairs%20That%20Form%20a%20Complete%20Day%20I/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个整数数组 <code>hours</code>,表示以 <strong>小时 </strong>为单位的时间,返回一个整数,表示满足 <code>i &lt; j</code> 且 <code>hours[i] + hours[j]</code> 构成 <strong>整天 </strong>的下标对&nbsp;<code>i</code>, <code>j</code> 的数目。</p>
18+
19+
<p><strong>整天 </strong>定义为时间持续时间是 24 小时的 <strong>整数倍 </strong>。</p>
20+
21+
<p>例如,1 天是 24 小时,2 天是 48 小时,3 天是 72 小时,以此类推。</p>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong class="example">示例 1:</strong></p>
26+
27+
<div class="example-block">
28+
<p><strong>输入:</strong> <span class="example-io">hours = [12,12,30,24,24]</span></p>
29+
30+
<p><strong>输出:</strong> <span class="example-io">2</span></p>
31+
32+
<p><strong>解释:</strong></p>
33+
34+
<p>构成整天的下标对分别是 <code>(0, 1)</code> 和 <code>(3, 4)</code>。</p>
35+
</div>
36+
37+
<p><strong class="example">示例 2:</strong></p>
38+
39+
<div class="example-block">
40+
<p><strong>输入:</strong> <span class="example-io">hours = [72,48,24,3]</span></p>
41+
42+
<p><strong>输出:</strong> <span class="example-io">3</span></p>
43+
44+
<p><strong>解释:</strong></p>
45+
46+
<p>构成整天的下标对分别是 <code>(0, 1)</code>、<code>(0, 2)</code> 和 <code>(1, 2)</code>。</p>
47+
</div>
48+
49+
<p>&nbsp;</p>
50+
51+
<p><strong>提示:</strong></p>
52+
53+
<ul>
54+
<li><code>1 &lt;= hours.length &lt;= 100</code></li>
55+
<li><code>1 &lt;= hours[i] &lt;= 10<sup>9</sup></code></li>
56+
</ul>
57+
58+
<!-- description:end -->
59+
60+
## 解法
61+
62+
<!-- solution:start -->
63+
64+
### 方法一:计数
65+
66+
我们可以用一个哈希表或者一个长度为 $24$ 的数组 $\text{cnt}$ 来记录每个小时数模 $24$ 的出现次数。
67+
68+
遍历数组 $\text{hours}$,对于每个小时数 $x$,我们可以得出与 $x$ 相加为 $24$ 的倍数,且模 $24$ 之后的数为 $(24 - x \bmod 24) \bmod 24$。累加这个数在哈希表或者数组中的出现次数即可。然后我们将 $x$ 的模 $24$ 的出现次数加一。
69+
70+
遍历完数组 $\text{hours}$ 后,我们就可以得到满足题意的下标对数目。
71+
72+
时间复杂度 $O(n)$,其中 $n$ 为数组 $\text{hours}$ 的长度。空间复杂度 $O(C)$,其中 $C=24$。
73+
74+
<!-- tabs:start -->
75+
76+
#### Python3
77+
78+
```python
79+
class Solution:
80+
def countCompleteDayPairs(self, hours: List[int]) -> int:
81+
cnt = Counter()
82+
ans = 0
83+
for x in hours:
84+
ans += cnt[(24 - (x % 24)) % 24]
85+
cnt[x % 24] += 1
86+
return ans
87+
```
88+
89+
#### Java
90+
91+
```java
92+
class Solution {
93+
public int countCompleteDayPairs(int[] hours) {
94+
int[] cnt = new int[24];
95+
int ans = 0;
96+
for (int x : hours) {
97+
ans += cnt[(24 - x % 24) % 24];
98+
++cnt[x % 24];
99+
}
100+
return ans;
101+
}
102+
}
103+
```
104+
105+
#### C++
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
int countCompleteDayPairs(vector<int>& hours) {
111+
int cnt[24]{};
112+
int ans = 0;
113+
for (int x : hours) {
114+
ans += cnt[(24 - x % 24) % 24];
115+
++cnt[x % 24];
116+
}
117+
return ans;
118+
}
119+
};
120+
```
121+
122+
#### Go
123+
124+
```go
125+
func countCompleteDayPairs(hours []int) (ans int) {
126+
cnt := [24]int{}
127+
for _, x := range hours {
128+
ans += cnt[(24-x%24)%24]
129+
cnt[x%24]++
130+
}
131+
return
132+
}
133+
```
134+
135+
#### TypeScript
136+
137+
```ts
138+
function countCompleteDayPairs(hours: number[]): number {
139+
const cnt: number[] = Array(24).fill(0);
140+
let ans: number = 0;
141+
for (const x of hours) {
142+
ans += cnt[(24 - (x % 24)) % 24];
143+
++cnt[x % 24];
144+
}
145+
return ans;
146+
}
147+
```
148+
149+
<!-- tabs:end -->
150+
151+
<!-- solution:end -->
152+
153+
<!-- problem:end -->
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3184.Count%20Pairs%20That%20Form%20a%20Complete%20Day%20I/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3184. Count Pairs That Form a Complete Day I](https://leetcode.com/problems/count-pairs-that-form-a-complete-day-i)
10+
11+
[中文文档](/solution/3100-3199/3184.Count%20Pairs%20That%20Form%20a%20Complete%20Day%20I/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>Given an integer array <code>hours</code> representing times in <strong>hours</strong>, return an integer denoting the number of pairs <code>i</code>, <code>j</code> where <code>i &lt; j</code> and <code>hours[i] + hours[j]</code> forms a <strong>complete day</strong>.</p>
18+
19+
<p>A <strong>complete day</strong> is defined as a time duration that is an <strong>exact</strong> <strong>multiple</strong> of 24 hours.</p>
20+
21+
<p>For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.</p>
22+
23+
<p>&nbsp;</p>
24+
<p><strong class="example">Example 1:</strong></p>
25+
26+
<div class="example-block">
27+
<p><strong>Input:</strong> <span class="example-io">hours = [12,12,30,24,24]</span></p>
28+
29+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
30+
31+
<p><strong>Explanation:</strong></p>
32+
33+
<p>The pairs of indices that form a complete day are <code>(0, 1)</code> and <code>(3, 4)</code>.</p>
34+
</div>
35+
36+
<p><strong class="example">Example 2:</strong></p>
37+
38+
<div class="example-block">
39+
<p><strong>Input:</strong> <span class="example-io">hours = [72,48,24,3]</span></p>
40+
41+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
42+
43+
<p><strong>Explanation:</strong></p>
44+
45+
<p>The pairs of indices that form a complete day are <code>(0, 1)</code>, <code>(0, 2)</code>, and <code>(1, 2)</code>.</p>
46+
</div>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= hours.length &lt;= 100</code></li>
53+
<li><code>1 &lt;= hours[i] &lt;= 10<sup>9</sup></code></li>
54+
</ul>
55+
56+
<!-- description:end -->
57+
58+
## Solutions
59+
60+
<!-- solution:start -->
61+
62+
### Solution 1: Counting
63+
64+
We can use a hash table or an array $\text{cnt}$ of length $24$ to record the occurrence count of each hour modulo $24$.
65+
66+
Iterate through the array $\text{hours}$. For each hour $x$, we can find the number that, when added to $x$, results in a multiple of $24$, and after modulo $24$, this number is $(24 - x \bmod 24) \bmod 24$. We then accumulate the occurrence count of this number from the hash table or array. After that, we increment the occurrence count of $x$ modulo $24$ by one.
67+
68+
After iterating through the array $\text{hours}$, we can obtain the number of index pairs that meet the problem requirements.
69+
70+
The time complexity is $O(n)$, where $n$ is the length of the array $\text{hours}$. The space complexity is $O(C)$, where $C=24$.
71+
72+
<!-- tabs:start -->
73+
74+
#### Python3
75+
76+
```python
77+
class Solution:
78+
def countCompleteDayPairs(self, hours: List[int]) -> int:
79+
cnt = Counter()
80+
ans = 0
81+
for x in hours:
82+
ans += cnt[(24 - (x % 24)) % 24]
83+
cnt[x % 24] += 1
84+
return ans
85+
```
86+
87+
#### Java
88+
89+
```java
90+
class Solution {
91+
public int countCompleteDayPairs(int[] hours) {
92+
int[] cnt = new int[24];
93+
int ans = 0;
94+
for (int x : hours) {
95+
ans += cnt[(24 - x % 24) % 24];
96+
++cnt[x % 24];
97+
}
98+
return ans;
99+
}
100+
}
101+
```
102+
103+
#### C++
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
int countCompleteDayPairs(vector<int>& hours) {
109+
int cnt[24]{};
110+
int ans = 0;
111+
for (int x : hours) {
112+
ans += cnt[(24 - x % 24) % 24];
113+
++cnt[x % 24];
114+
}
115+
return ans;
116+
}
117+
};
118+
```
119+
120+
#### Go
121+
122+
```go
123+
func countCompleteDayPairs(hours []int) (ans int) {
124+
cnt := [24]int{}
125+
for _, x := range hours {
126+
ans += cnt[(24-x%24)%24]
127+
cnt[x%24]++
128+
}
129+
return
130+
}
131+
```
132+
133+
#### TypeScript
134+
135+
```ts
136+
function countCompleteDayPairs(hours: number[]): number {
137+
const cnt: number[] = Array(24).fill(0);
138+
let ans: number = 0;
139+
for (const x of hours) {
140+
ans += cnt[(24 - (x % 24)) % 24];
141+
++cnt[x % 24];
142+
}
143+
return ans;
144+
}
145+
```
146+
147+
<!-- tabs:end -->
148+
149+
<!-- solution:end -->
150+
151+
<!-- problem:end -->
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int countCompleteDayPairs(vector<int>& hours) {
4+
int cnt[24]{};
5+
int ans = 0;
6+
for (int x : hours) {
7+
ans += cnt[(24 - x % 24) % 24];
8+
++cnt[x % 24];
9+
}
10+
return ans;
11+
}
12+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func countCompleteDayPairs(hours []int) (ans int) {
2+
cnt := [24]int{}
3+
for _, x := range hours {
4+
ans += cnt[(24-x%24)%24]
5+
cnt[x%24]++
6+
}
7+
return
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int countCompleteDayPairs(int[] hours) {
3+
int[] cnt = new int[24];
4+
int ans = 0;
5+
for (int x : hours) {
6+
ans += cnt[(24 - x % 24) % 24];
7+
++cnt[x % 24];
8+
}
9+
return ans;
10+
}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def countCompleteDayPairs(self, hours: List[int]) -> int:
3+
cnt = Counter()
4+
ans = 0
5+
for x in hours:
6+
ans += cnt[(24 - (x % 24)) % 24]
7+
cnt[x % 24] += 1
8+
return ans
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function countCompleteDayPairs(hours: number[]): number {
2+
const cnt: number[] = Array(24).fill(0);
3+
let ans: number = 0;
4+
for (const x of hours) {
5+
ans += cnt[(24 - (x % 24)) % 24];
6+
++cnt[x % 24];
7+
}
8+
return ans;
9+
}

0 commit comments

Comments
 (0)