Skip to content

[pull] main from doocs:main #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions solution/1500-1599/1534.Count Good Triplets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ tags:

### 方法一:枚举

我们可以枚举所有的 $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$,如果满足则将答案加一。
我们可以枚举所有的 $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$,如果满足则将答案加一。

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -160,6 +160,29 @@ func abs(x int) int {
}
```

#### TypeScript

```ts
function countGoodTriplets(arr: number[], a: number, b: number, c: number): number {
let n = arr.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
for (let j = i + 1; j < n; ++j) {
for (let k = j + 1; k < n; ++k) {
if (
Math.abs(arr[i] - arr[j]) <= a &&
Math.abs(arr[j] - arr[k]) <= b &&
Math.abs(arr[i] - arr[k]) <= c
) {
++ans;
}
}
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
31 changes: 30 additions & 1 deletion solution/1500-1599/1534.Count Good Triplets/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Enumeration

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.

After enumerating all possible triplets, we get the answer.

The time complexity is $O(n^3)$, where $n$ is the length of the array $\textit{arr}$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -173,6 +179,29 @@ func abs(x int) int {
}
```

#### TypeScript

```ts
function countGoodTriplets(arr: number[], a: number, b: number, c: number): number {
let n = arr.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
for (let j = i + 1; j < n; ++j) {
for (let k = j + 1; k < n; ++k) {
if (
Math.abs(arr[i] - arr[j]) <= a &&
Math.abs(arr[j] - arr[k]) <= b &&
Math.abs(arr[i] - arr[k]) <= c
) {
++ans;
}
}
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
18 changes: 18 additions & 0 deletions solution/1500-1599/1534.Count Good Triplets/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function countGoodTriplets(arr: number[], a: number, b: number, c: number): number {
let n = arr.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
for (let j = i + 1; j < n; ++j) {
for (let k = j + 1; k < n; ++k) {
if (
Math.abs(arr[i] - arr[j]) <= a &&
Math.abs(arr[j] - arr[k]) <= b &&
Math.abs(arr[i] - arr[k]) <= c
) {
++ans;
}
}
}
}
return ans;
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class Solution {
class Solution {
public:
int maxDistance(vector<int>& position, int m) {
sort(position.begin(), position.end());
ranges::sort(position);
int l = 1, r = position.back();
auto count = [&](int f) {
int prev = position[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class Solution {
class Solution {
public:
int maxDistance(vector<int>& position, int m) {
sort(position.begin(), position.end());
ranges::sort(position);
int l = 1, r = position.back();
auto count = [&](int f) {
int prev = position[0];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Solution {
public:
int maxDistance(vector<int>& position, int m) {
sort(position.begin(), position.end());
ranges::sort(position);
int l = 1, r = position.back();
auto count = [&](int f) {
int prev = position[0];
Expand All @@ -24,4 +24,4 @@ class Solution {
}
return l;
}
};
};