Skip to content

Commit 09ac186

Browse files
committed
feat: add solutions to lc problem: No.1534
No.1534.Count Good Triplets
1 parent 6554458 commit 09ac186

File tree

8 files changed

+37
-41
lines changed

8 files changed

+37
-41
lines changed

solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767

6868
接下来,我们维护当前的前缀和 $s$,初始时 $s = 0$。
6969

70-
遍历数组 $arr$,对于遍历到的每个元素 $x$,我们将 $s$ 的值加上 $x$,然后根据 $s$ 的奇偶性,将 $cnt[s \& 1 \oplus 1]$ 的值累加到答案中,然后我们将 $cnt[s \& 1]$ 的值加 $1$。
70+
遍历数组 $arr$,对于遍历到的每个元素 $x$,我们将 $s$ 的值加上 $x$,然后根据 $s$ 的奇偶性,将 $cnt[s \mod 2 \oplus 1]$ 的值累加到答案中,然后我们将 $cnt[s \mod 2]$ 的值加 $1$。
7171

7272
遍历结束后,我们即可得到答案。注意答案的取模运算。
7373

solution/1500-1599/1528.Shuffle String/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ public:
8888
string restoreString(string s, vector<int>& indices) {
8989
int n = s.size();
9090
string ans(n, 0);
91-
for (int i = 0; i < n; ++i) ans[indices[i]] = s[i];
91+
for (int i = 0; i < n; ++i) {
92+
ans[indices[i]] = s[i];
93+
}
9294
return ans;
9395
}
9496
};

solution/1500-1599/1528.Shuffle String/README_EN.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public:
7474
string restoreString(string s, vector<int>& indices) {
7575
int n = s.size();
7676
string ans(n, 0);
77-
for (int i = 0; i < n; ++i) ans[indices[i]] = s[i];
77+
for (int i = 0; i < n; ++i) {
78+
ans[indices[i]] = s[i];
79+
}
7880
return ans;
7981
}
8082
};

solution/1500-1599/1528.Shuffle String/Solution.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ class Solution {
33
string restoreString(string s, vector<int>& indices) {
44
int n = s.size();
55
string ans(n, 0);
6-
for (int i = 0; i < n; ++i) ans[indices[i]] = s[i];
6+
for (int i = 0; i < n; ++i) {
7+
ans[indices[i]] = s[i];
8+
}
79
return ans;
810
}
911
};

solution/1500-1599/1534.Count Good Triplets/README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
**方法一:枚举**
55+
56+
我们可以枚举所有的 $i$, $j$ 和 $k$,其中 $i \lt j \lt k$,判断是否同时满足 $|arr[i] - arr[j]| \le a$,$|arr[j] - arr[k]| \le b$ 和 $|arr[i] - arr[k]| \le c$,如果满足则将答案加一。
57+
58+
枚举结束后,即可得到答案。
59+
60+
时间复杂度 $O(n^3)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $arr$ 的长度。
61+
5462
<!-- tabs:start -->
5563

5664
### **Python3**
@@ -60,18 +68,14 @@
6068
```python
6169
class Solution:
6270
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
63-
n = len(arr)
64-
ans = 0
71+
ans, n = 0, len(arr)
6572
for i in range(n):
6673
for j in range(i + 1, n):
6774
for k in range(j + 1, n):
68-
if (
69-
abs(arr[i] - arr[j]) <= a
70-
and abs(arr[j] - arr[k]) <= b
71-
and abs(arr[i] - arr[k]) <= c
72-
):
73-
ans += 1
75+
ans += abs(arr[i] - arr[j]) <= a \
76+
and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c
7477
return ans
78+
7579
```
7680

7781
### **Java**
@@ -121,8 +125,8 @@ public:
121125
### **Go**
122126
123127
```go
124-
func countGoodTriplets(arr []int, a int, b int, c int) int {
125-
n, ans := len(arr), 0
128+
func countGoodTriplets(arr []int, a int, b int, c int) (ans int) {
129+
n := len(arr)
126130
for i := 0; i < n; i++ {
127131
for j := i + 1; j < n; j++ {
128132
for k := j + 1; k < n; k++ {
@@ -132,7 +136,7 @@ func countGoodTriplets(arr []int, a int, b int, c int) int {
132136
}
133137
}
134138
}
135-
return ans
139+
return
136140
}
137141
138142
func abs(x int) int {

solution/1500-1599/1534.Count Good Triplets/README_EN.md

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,9 @@
5555
<p><strong>Constraints:</strong></p>
5656

5757
<ul>
58-
5958
<li><code>3 &lt;= arr.length &lt;= 100</code></li>
60-
6159
<li><code>0 &lt;= arr[i] &lt;= 1000</code></li>
62-
6360
<li><code>0 &lt;= a, b, c &lt;= 1000</code></li>
64-
6561
</ul>
6662

6763
## Solutions
@@ -73,17 +69,12 @@
7369
```python
7470
class Solution:
7571
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
76-
n = len(arr)
77-
ans = 0
72+
ans, n = 0, len(arr)
7873
for i in range(n):
7974
for j in range(i + 1, n):
8075
for k in range(j + 1, n):
81-
if (
82-
abs(arr[i] - arr[j]) <= a
83-
and abs(arr[j] - arr[k]) <= b
84-
and abs(arr[i] - arr[k]) <= c
85-
):
86-
ans += 1
76+
ans += abs(arr[i] - arr[j]) <= a \
77+
and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c
8778
return ans
8879
```
8980

@@ -132,8 +123,8 @@ public:
132123
### **Go**
133124
134125
```go
135-
func countGoodTriplets(arr []int, a int, b int, c int) int {
136-
n, ans := len(arr), 0
126+
func countGoodTriplets(arr []int, a int, b int, c int) (ans int) {
127+
n := len(arr)
137128
for i := 0; i < n; i++ {
138129
for j := i + 1; j < n; j++ {
139130
for k := j + 1; k < n; k++ {
@@ -143,7 +134,7 @@ func countGoodTriplets(arr []int, a int, b int, c int) int {
143134
}
144135
}
145136
}
146-
return ans
137+
return
147138
}
148139
149140
func abs(x int) int {

solution/1500-1599/1534.Count Good Triplets/Solution.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
func countGoodTriplets(arr []int, a int, b int, c int) int {
2-
n, ans := len(arr), 0
1+
func countGoodTriplets(arr []int, a int, b int, c int) (ans int) {
2+
n := len(arr)
33
for i := 0; i < n; i++ {
44
for j := i + 1; j < n; j++ {
55
for k := j + 1; k < n; k++ {
@@ -9,7 +9,7 @@ func countGoodTriplets(arr []int, a int, b int, c int) int {
99
}
1010
}
1111
}
12-
return ans
12+
return
1313
}
1414

1515
func abs(x int) int {
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
class Solution:
22
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
3-
n = len(arr)
4-
ans = 0
3+
ans, n = 0, len(arr)
54
for i in range(n):
65
for j in range(i + 1, n):
76
for k in range(j + 1, n):
8-
if (
9-
abs(arr[i] - arr[j]) <= a
10-
and abs(arr[j] - arr[k]) <= b
11-
and abs(arr[i] - arr[k]) <= c
12-
):
13-
ans += 1
7+
ans += abs(arr[i] - arr[j]) <= a \
8+
and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c
149
return ans

0 commit comments

Comments
 (0)