Skip to content

Commit fba1591

Browse files
authored
feat: add solutions to lc problem: No.1552 (#4060)
No.1552.Magnetic Force Between Two Balls
1 parent 66c0854 commit fba1591

File tree

6 files changed

+77
-7
lines changed

6 files changed

+77
-7
lines changed

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

+25-2
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ tags:
6868

6969
### 方法一:枚举
7070

71-
我们可以枚举所有的 $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$,如果满足则将答案加一。
71+
我们可以枚举所有的 $i$, $j$ 和 $k$,其中 $i \lt j \lt k$,判断是否同时满足 $|\textit{arr}[i] - \textit{arr}[j]| \le a$,$|\textit{arr}[j] - \textit{arr}[k]| \le b$ 和 $|\textit{arr}[i] - \textit{arr}[k]| \le c$,如果满足则将答案加一。
7272

7373
枚举结束后,即可得到答案。
7474

75-
时间复杂度 $O(n^3)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $arr$ 的长度。
75+
时间复杂度 $O(n^3)$,其中 $n$ 为数组 $\textit{arr}$ 的长度。空间复杂度 $O(1)$
7676

7777
<!-- tabs:start -->
7878

@@ -160,6 +160,29 @@ func abs(x int) int {
160160
}
161161
```
162162

163+
#### TypeScript
164+
165+
```ts
166+
function countGoodTriplets(arr: number[], a: number, b: number, c: number): number {
167+
let n = arr.length;
168+
let ans = 0;
169+
for (let i = 0; i < n; ++i) {
170+
for (let j = i + 1; j < n; ++j) {
171+
for (let k = j + 1; k < n; ++k) {
172+
if (
173+
Math.abs(arr[i] - arr[j]) <= a &&
174+
Math.abs(arr[j] - arr[k]) <= b &&
175+
Math.abs(arr[i] - arr[k]) <= c
176+
) {
177+
++ans;
178+
}
179+
}
180+
}
181+
}
182+
return ans;
183+
}
184+
```
185+
163186
<!-- tabs:end -->
164187

165188
<!-- solution:end -->

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

+30-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,13 @@ tags:
8585

8686
<!-- solution:start -->
8787

88-
### Solution 1
88+
### Solution 1: Enumeration
89+
90+
We can enumerate all $i$, $j$, and $k$ where $i \lt j \lt k$, and check if they simultaneously satisfy $|\textit{arr}[i] - \textit{arr}[j]| \le a$, $|\textit{arr}[j] - \textit{arr}[k]| \le b$, and $|\textit{arr}[i] - \textit{arr}[k]| \le c$. If they do, we increment the answer by one.
91+
92+
After enumerating all possible triplets, we get the answer.
93+
94+
The time complexity is $O(n^3)$, where $n$ is the length of the array $\textit{arr}$. The space complexity is $O(1)$.
8995

9096
<!-- tabs:start -->
9197

@@ -173,6 +179,29 @@ func abs(x int) int {
173179
}
174180
```
175181

182+
#### TypeScript
183+
184+
```ts
185+
function countGoodTriplets(arr: number[], a: number, b: number, c: number): number {
186+
let n = arr.length;
187+
let ans = 0;
188+
for (let i = 0; i < n; ++i) {
189+
for (let j = i + 1; j < n; ++j) {
190+
for (let k = j + 1; k < n; ++k) {
191+
if (
192+
Math.abs(arr[i] - arr[j]) <= a &&
193+
Math.abs(arr[j] - arr[k]) <= b &&
194+
Math.abs(arr[i] - arr[k]) <= c
195+
) {
196+
++ans;
197+
}
198+
}
199+
}
200+
}
201+
return ans;
202+
}
203+
```
204+
176205
<!-- tabs:end -->
177206

178207
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function countGoodTriplets(arr: number[], a: number, b: number, c: number): number {
2+
let n = arr.length;
3+
let ans = 0;
4+
for (let i = 0; i < n; ++i) {
5+
for (let j = i + 1; j < n; ++j) {
6+
for (let k = j + 1; k < n; ++k) {
7+
if (
8+
Math.abs(arr[i] - arr[j]) <= a &&
9+
Math.abs(arr[j] - arr[k]) <= b &&
10+
Math.abs(arr[i] - arr[k]) <= c
11+
) {
12+
++ans;
13+
}
14+
}
15+
}
16+
}
17+
return ans;
18+
}

solution/1500-1599/1552.Magnetic Force Between Two Balls/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class Solution {
134134
class Solution {
135135
public:
136136
int maxDistance(vector<int>& position, int m) {
137-
sort(position.begin(), position.end());
137+
ranges::sort(position);
138138
int l = 1, r = position.back();
139139
auto count = [&](int f) {
140140
int prev = position[0];

solution/1500-1599/1552.Magnetic Force Between Two Balls/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class Solution {
132132
class Solution {
133133
public:
134134
int maxDistance(vector<int>& position, int m) {
135-
sort(position.begin(), position.end());
135+
ranges::sort(position);
136136
int l = 1, r = position.back();
137137
auto count = [&](int f) {
138138
int prev = position[0];

solution/1500-1599/1552.Magnetic Force Between Two Balls/Solution.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution {
22
public:
33
int maxDistance(vector<int>& position, int m) {
4-
sort(position.begin(), position.end());
4+
ranges::sort(position);
55
int l = 1, r = position.back();
66
auto count = [&](int f) {
77
int prev = position[0];
@@ -24,4 +24,4 @@ class Solution {
2424
}
2525
return l;
2626
}
27-
};
27+
};

0 commit comments

Comments
 (0)