Skip to content

Commit 57aff45

Browse files
committed
update: 添加问题“1534.统计好三元组”的代码和题解(#876)
Signed-off-by: LetMeFly666 <[email protected]>
1 parent 61b6065 commit 57aff45

File tree

6 files changed

+257
-0
lines changed

6 files changed

+257
-0
lines changed

Codes/1534-count-good-triplets.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-15 10:24:13
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-15 10:28:19
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int countGoodTriplets(vector<int>& arr, int a, int b, int c) {
14+
int ans = 0, n = arr.size();
15+
for (int i = 0; i < n; i++) {
16+
for (int j = i + 1; j < n; j++) {
17+
if (abs(arr[i] - arr[j]) > a) {
18+
continue;
19+
}
20+
for (int k = j + 1; k < n; k++) {
21+
ans += abs(arr[j] - arr[k]) <= b && abs(arr[i] - arr[k]) <= c;
22+
}
23+
}
24+
}
25+
return ans;
26+
}
27+
};

Codes/1534-count-good-triplets.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-15 10:39:01
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-15 10:43:14
6+
*/
7+
package main
8+
9+
func abs1534(a int) int {
10+
if a < 0 {
11+
return -a
12+
}
13+
return a
14+
}
15+
16+
func countGoodTriplets(arr []int, a int, b int, c int) (ans int) {
17+
for i := range arr {
18+
for j := i + 1; j < len(arr); j++ {
19+
for k := j + 1; k < len(arr); k++ {
20+
if abs1534(arr[i] - arr[j]) <= a && abs1534(arr[j] - arr[k]) <= b && abs1534(arr[i] - arr[k]) <= c {
21+
ans++
22+
}
23+
}
24+
}
25+
}
26+
return
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-15 10:35:43
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-15 10:37:19
6+
*/
7+
class Solution {
8+
public int countGoodTriplets(int[] arr, int a, int b, int c) {
9+
int ans = 0, n = arr.length;
10+
for (int i = 0; i < n; i++) {
11+
for (int j = i + 1; j < n; j++) {
12+
for (int k = j + 1; k < n; k++) {
13+
if (Math.abs(arr[i] - arr[j]) <= a && Math.abs(arr[j] - arr[k]) <= b && Math.abs(arr[i] - arr[k]) <= c) {
14+
ans++;
15+
}
16+
}
17+
}
18+
}
19+
return ans;
20+
}
21+
}

Codes/1534-count-good-triplets.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-15 10:28:55
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-15 10:34:14
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
11+
# return sum(abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[j] - arr[k]) <= c for k in range(j + 1, len(arr)) for j in range(i + 1, len(arr)) for i in range(len(arr)))
12+
return sum(abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c for i in range(len(arr)) for j in range(i + 1, len(arr)) for k in range(j + 1, len(arr)))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@
543543
|1488.避免洪水泛滥|中等|<a href="https://leetcode.cn/problems/avoid-flood-in-the-city/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/10/13/LeetCode%201488.%E9%81%BF%E5%85%8D%E6%B4%AA%E6%B0%B4%E6%B3%9B%E6%BB%A5/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133809999" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/avoid-flood-in-the-city/solutions/2480771/letmefly-1488bi-mian-hong-shui-fan-lan-h-yxx5/" target="_blank">LeetCode题解</a>|
544544
|1491.去掉最低工资和最高工资后的工资平均值|简单|<a href="https://leetcode.cn/problems/average-salary-excluding-the-minimum-and-maximum-salary/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/03/LeetCode%201491.%E5%8E%BB%E6%8E%89%E6%9C%80%E4%BD%8E%E5%B7%A5%E8%B5%84%E5%92%8C%E6%9C%80%E9%AB%98%E5%B7%A5%E8%B5%84%E5%90%8E%E7%9A%84%E5%B7%A5%E8%B5%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/138416153" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/average-salary-excluding-the-minimum-and-maximum-salary/solutions/2764238/letmefly-1491qu-diao-zui-di-gong-zi-he-z-8h25/" target="_blank">LeetCode题解</a>|
545545
|1499.满足不等式的最大值|困难|<a href="https://leetcode.cn/problems/max-value-of-equation/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/07/21/LeetCode%201499.%E6%BB%A1%E8%B6%B3%E4%B8%8D%E7%AD%89%E5%BC%8F%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131844105" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/max-value-of-equation/solutions/2352476/letmefly-1499man-zu-bu-deng-shi-de-zui-d-wlzu/" target="_blank">LeetCode题解</a>|
546+
|1534.统计好三元组|简单|<a href="https://leetcode.cn/problems/count-good-triplets/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/15/LeetCode%201534.%E7%BB%9F%E8%AE%A1%E5%A5%BD%E4%B8%89%E5%85%83%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147249275" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-good-triplets/solutions/3651795/letmefly-1534tong-ji-hao-san-yuan-zu-xi-dv8dw/" target="_blank">LeetCode题解</a>|
546547
|1535.找出数组游戏的赢家|中等|<a href="https://leetcode.cn/problems/find-the-winner-of-an-array-game/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/19/LeetCode%201535.%E6%89%BE%E5%87%BA%E6%95%B0%E7%BB%84%E6%B8%B8%E6%88%8F%E7%9A%84%E8%B5%A2%E5%AE%B6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139040126" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-winner-of-an-array-game/solutions/2782688/letmefly-1535zhao-chu-shu-zu-you-xi-de-y-ff3w/" target="_blank">LeetCode题解</a>|
547548
|1542.找出最长的超赞子字符串|困难|<a href="https://leetcode.cn/problems/find-longest-awesome-substring/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/20/LeetCode%201542.%E6%89%BE%E5%87%BA%E6%9C%80%E9%95%BF%E7%9A%84%E8%B6%85%E8%B5%9E%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139077950" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-longest-awesome-substring/solutions/2784697/letmefly-1542zhao-chu-zui-chang-de-chao-16nco/" target="_blank">LeetCode题解</a>|
548549
|1552.两球之间的磁力|中等|<a href="https://leetcode.cn/problems/magnetic-force-between-two-balls/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/02/14/LeetCode%201552.%E4%B8%A4%E7%90%83%E4%B9%8B%E9%97%B4%E7%9A%84%E7%A3%81%E5%8A%9B/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145629037" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/magnetic-force-between-two-balls/solutions/3074287/letmefly-1552liang-qiu-zhi-jian-de-ci-li-ptbu/" target="_blank">LeetCode题解</a>|
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
---
2+
title: 1534.统计好三元组:系列题之-easy等级先模拟
3+
date: 2025-04-15 13:27:51
4+
tags: [题解, LeetCode, 简单, 数组, 枚举]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】1534.统计好三元组:系列题之-easy等级先模拟
9+
10+
力扣题目链接:[https://leetcode.cn/problems/count-good-triplets/](https://leetcode.cn/problems/count-good-triplets/)
11+
12+
<p>给你一个整数数组 <code>arr</code> ,以及 <code>a</code>、<code>b</code> 、<code>c</code> 三个整数。请你统计其中好三元组的数量。</p>
13+
14+
<p>如果三元组 <code>(arr[i], arr[j], arr[k])</code> 满足下列全部条件,则认为它是一个 <strong>好三元组</strong> 。</p>
15+
16+
<ul>
17+
<li><code>0 &lt;= i &lt; j &lt; k &lt;&nbsp;arr.length</code></li>
18+
<li><code>|arr[i] - arr[j]| &lt;= a</code></li>
19+
<li><code>|arr[j] - arr[k]| &lt;= b</code></li>
20+
<li><code>|arr[i] - arr[k]| &lt;= c</code></li>
21+
</ul>
22+
23+
<p>其中 <code>|x|</code> 表示 <code>x</code> 的绝对值。</p>
24+
25+
<p>返回 <strong>好三元组的数量</strong> 。</p>
26+
27+
<p>&nbsp;</p>
28+
29+
<p><strong>示例 1:</strong></p>
30+
31+
<pre><strong>输入:</strong>arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3
32+
<strong>输出:</strong>4
33+
<strong>解释:</strong>一共有 4 个好三元组:[(3,0,1), (3,0,1), (3,1,1), (0,1,1)] 。
34+
</pre>
35+
36+
<p><strong>示例 2:</strong></p>
37+
38+
<pre><strong>输入:</strong>arr = [1,1,2,2,3], a = 0, b = 0, c = 1
39+
<strong>输出:</strong>0
40+
<strong>解释:</strong>不存在满足所有条件的三元组。
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><strong>提示:</strong></p>
46+
47+
<ul>
48+
<li><code>3 &lt;= arr.length &lt;= 100</code></li>
49+
<li><code>0 &lt;= arr[i] &lt;= 1000</code></li>
50+
<li><code>0 &lt;= a, b, c &lt;= 1000</code></li>
51+
</ul>
52+
53+
54+
55+
## 解题方法:三层循环
56+
57+
第一层用$i$从$0$到$n-1$,第二层用$j$从$i+1$到$n-1$,第三层用$k$从$j+1$到$n-1$,判断这个三元组是否为“好三元组”。
58+
59+
+ 时间复杂度$O(len(arr)^3)$
60+
+ 空间复杂度$O(1)$
61+
62+
### AC代码
63+
64+
#### C++
65+
66+
```cpp
67+
/*
68+
* @Author: LetMeFly
69+
* @Date: 2025-04-15 10:24:13
70+
* @LastEditors: LetMeFly.xyz
71+
* @LastEditTime: 2025-04-15 10:28:19
72+
*/
73+
class Solution {
74+
public:
75+
int countGoodTriplets(vector<int>& arr, int a, int b, int c) {
76+
int ans = 0, n = arr.size();
77+
for (int i = 0; i < n; i++) {
78+
for (int j = i + 1; j < n; j++) {
79+
if (abs(arr[i] - arr[j]) > a) {
80+
continue;
81+
}
82+
for (int k = j + 1; k < n; k++) {
83+
ans += abs(arr[j] - arr[k]) <= b && abs(arr[i] - arr[k]) <= c;
84+
}
85+
}
86+
}
87+
return ans;
88+
}
89+
};
90+
```
91+
92+
#### Python
93+
94+
```python
95+
'''
96+
Author: LetMeFly
97+
Date: 2025-04-15 10:28:55
98+
LastEditors: LetMeFly.xyz
99+
LastEditTime: 2025-04-15 10:34:14
100+
'''
101+
from typing import List
102+
103+
class Solution:
104+
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
105+
# return sum(abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[j] - arr[k]) <= c for k in range(j + 1, len(arr)) for j in range(i + 1, len(arr)) for i in range(len(arr)))
106+
return sum(abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c for i in range(len(arr)) for j in range(i + 1, len(arr)) for k in range(j + 1, len(arr)))
107+
```
108+
109+
#### Java
110+
111+
```java
112+
/*
113+
* @Author: LetMeFly
114+
* @Date: 2025-04-15 10:35:43
115+
* @LastEditors: LetMeFly.xyz
116+
* @LastEditTime: 2025-04-15 10:37:19
117+
*/
118+
class Solution {
119+
public int countGoodTriplets(int[] arr, int a, int b, int c) {
120+
int ans = 0, n = arr.length;
121+
for (int i = 0; i < n; i++) {
122+
for (int j = i + 1; j < n; j++) {
123+
for (int k = j + 1; k < n; k++) {
124+
if (Math.abs(arr[i] - arr[j]) <= a && Math.abs(arr[j] - arr[k]) <= b && Math.abs(arr[i] - arr[k]) <= c) {
125+
ans++;
126+
}
127+
}
128+
}
129+
}
130+
return ans;
131+
}
132+
}
133+
```
134+
135+
#### Go
136+
137+
```go
138+
/*
139+
* @Author: LetMeFly
140+
* @Date: 2025-04-15 10:39:01
141+
* @LastEditors: LetMeFly.xyz
142+
* @LastEditTime: 2025-04-15 10:43:14
143+
*/
144+
package main
145+
146+
func abs1534(a int) int {
147+
if a < 0 {
148+
return -a
149+
}
150+
return a
151+
}
152+
153+
func countGoodTriplets(arr []int, a int, b int, c int) (ans int) {
154+
for i := range arr {
155+
for j := i + 1; j < len(arr); j++ {
156+
for k := j + 1; k < len(arr); k++ {
157+
if abs1534(arr[i] - arr[j]) <= a && abs1534(arr[j] - arr[k]) <= b && abs1534(arr[i] - arr[k]) <= c {
158+
ans++
159+
}
160+
}
161+
}
162+
}
163+
return
164+
}
165+
```
166+
167+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/147249275)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/04/15/LeetCode%201534.%E7%BB%9F%E8%AE%A1%E5%A5%BD%E4%B8%89%E5%85%83%E7%BB%84/)哦~
168+
>
169+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

0 commit comments

Comments
 (0)