From fba15918e3cb01587d06383297abd6d107178ad4 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 14 Feb 2025 09:17:23 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1552 (#4060) No.1552.Magnetic Force Between Two Balls --- .../1534.Count Good Triplets/README.md | 27 ++++++++++++++-- .../1534.Count Good Triplets/README_EN.md | 31 ++++++++++++++++++- .../1534.Count Good Triplets/Solution.ts | 18 +++++++++++ .../README.md | 2 +- .../README_EN.md | 2 +- .../Solution.cpp | 4 +-- 6 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 solution/1500-1599/1534.Count Good Triplets/Solution.ts diff --git a/solution/1500-1599/1534.Count Good Triplets/README.md b/solution/1500-1599/1534.Count Good Triplets/README.md index 8c86246489ee7..1b8c85b232947 100644 --- a/solution/1500-1599/1534.Count Good Triplets/README.md +++ b/solution/1500-1599/1534.Count Good Triplets/README.md @@ -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)$。 @@ -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; +} +``` + diff --git a/solution/1500-1599/1534.Count Good Triplets/README_EN.md b/solution/1500-1599/1534.Count Good Triplets/README_EN.md index 2c122dfa156b1..1a417e60574bf 100644 --- a/solution/1500-1599/1534.Count Good Triplets/README_EN.md +++ b/solution/1500-1599/1534.Count Good Triplets/README_EN.md @@ -85,7 +85,13 @@ tags: -### 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)$. @@ -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; +} +``` + diff --git a/solution/1500-1599/1534.Count Good Triplets/Solution.ts b/solution/1500-1599/1534.Count Good Triplets/Solution.ts new file mode 100644 index 0000000000000..d6d6c378fbacc --- /dev/null +++ b/solution/1500-1599/1534.Count Good Triplets/Solution.ts @@ -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; +} diff --git a/solution/1500-1599/1552.Magnetic Force Between Two Balls/README.md b/solution/1500-1599/1552.Magnetic Force Between Two Balls/README.md index 6aa40c35c2a7b..57bda6eb8109f 100644 --- a/solution/1500-1599/1552.Magnetic Force Between Two Balls/README.md +++ b/solution/1500-1599/1552.Magnetic Force Between Two Balls/README.md @@ -134,7 +134,7 @@ class Solution { class Solution { public: int maxDistance(vector& 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]; diff --git a/solution/1500-1599/1552.Magnetic Force Between Two Balls/README_EN.md b/solution/1500-1599/1552.Magnetic Force Between Two Balls/README_EN.md index 79b3601a09f74..a71436794dfba 100644 --- a/solution/1500-1599/1552.Magnetic Force Between Two Balls/README_EN.md +++ b/solution/1500-1599/1552.Magnetic Force Between Two Balls/README_EN.md @@ -132,7 +132,7 @@ class Solution { class Solution { public: int maxDistance(vector& 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]; diff --git a/solution/1500-1599/1552.Magnetic Force Between Two Balls/Solution.cpp b/solution/1500-1599/1552.Magnetic Force Between Two Balls/Solution.cpp index ceb1c1e02df21..2bab6cb44af0b 100644 --- a/solution/1500-1599/1552.Magnetic Force Between Two Balls/Solution.cpp +++ b/solution/1500-1599/1552.Magnetic Force Between Two Balls/Solution.cpp @@ -1,7 +1,7 @@ class Solution { public: int maxDistance(vector& 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]; @@ -24,4 +24,4 @@ class Solution { } return l; } -}; \ No newline at end of file +};