Skip to content

Commit 63dcdfb

Browse files
committed
feat: update solutions to lc problems: NO.0626,2475
* No.0626.Exchange Seats * No.2475.Number of Unequal Triplets in Array
1 parent 5e2e374 commit 63dcdfb

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

solution/0600-0699/0626.Exchange Seats/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,16 @@ ORDER BY
9292
id;
9393
```
9494

95+
```sql
96+
# Write your MySQL query statement below
97+
select
98+
rank() over(
99+
order by
100+
(id -1) ^ 1
101+
) as id,
102+
student
103+
from
104+
seat
105+
```
106+
95107
<!-- tabs:end -->

solution/0600-0699/0626.Exchange Seats/README_EN.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,16 @@ ORDER BY
8888
id;
8989
```
9090

91+
```sql
92+
# Write your MySQL query statement below
93+
select
94+
rank() over(
95+
order by
96+
(id -1) ^ 1
97+
) as id,
98+
student
99+
from
100+
seat
101+
```
102+
91103
<!-- tabs:end -->

solution/2400-2499/2475.Number of Unequal Triplets in Array/README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,23 @@
5959

6060
我们可以直接枚举所有的三元组 $(i, j, k)$,统计所有符合条件的数量。
6161

62-
时间复杂度 $O(n^3)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
62+
时间复杂度 $O(n^3)$,其中 $n$ 为数组 $nums$ 的长度。空间复杂度 $O(1)$
6363

6464
**方法二:排序 + 枚举中间元素 + 二分查找**
6565

66-
我们可以先对数组 `nums` 进行排序。
66+
我们也可以先对数组 $nums$ 进行排序。
6767

68-
然后遍历 `nums`,枚举中间元素 $nums[j]$,在 $nums[j]$ 左侧找到最近的下标 $i$,使得 $nums[i] \lt nums[j]$ 成立;在 $nums[j]$ 右侧找到最近的下标 $k$,使得 $nums[k] \gt nums[j]$ 成立。那么以 $nums[j]$ 作为中间元素,且符合条件的三元组数量为 $(i+1) \times (n - k)$,累加到答案中。
68+
然后遍历 $nums$,枚举中间元素 $nums[j]$,利用二分查找,在 $nums[j]$ 左侧找到最近的下标 $i$,使得 $nums[i] \lt nums[j]$ 成立;在 $nums[j]$ 右侧找到最近的下标 $k$,使得 $nums[k] \gt nums[j]$ 成立。那么以 $nums[j]$ 作为中间元素,且符合条件的三元组数量为 $(i + 1) \times (n - k)$,累加到答案中。
6969

70-
时间复杂度 $O(n\times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 `nums` 的长度。
70+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $nums$ 的长度。
7171

7272
**方法三:哈希表**
7373

74-
我们还可以使用哈希表 $cnt$ 来统计数组 `nums` 中每个元素的数量。
74+
我们还可以使用哈希表 $cnt$ 来统计数组 $nums$ 中每个元素的数量。
7575

76-
然后遍历哈希表 $cnt$,枚举中间元素的个数 $b$,左侧元素个数记为 $a$,那么右侧元素个数有 $n-a-b$,此时符合条件的三元组数量为 $a \times b \times c$,累加到答案中。接着更新 $a=a+b$,继续枚举中间元素的个数 $b$。
76+
然后遍历哈希表 $cnt$,枚举中间元素的个数 $b$,左侧元素个数记为 $a$,那么右侧元素个数有 $n - a - b$,此时符合条件的三元组数量为 $a \times b \times c$,累加到答案中。接着更新 $a = a + b$,继续枚举中间元素的个数 $b$。
7777

78-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
78+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
7979

8080
<!-- tabs:start -->
8181

@@ -102,7 +102,7 @@ class Solution:
102102
ans, n = 0, len(nums)
103103
for j in range(1, n - 1):
104104
i = bisect_left(nums, nums[j], hi=j) - 1
105-
k = bisect_right(nums, nums[j], lo=j+1)
105+
k = bisect_right(nums, nums[j], lo=j + 1)
106106
ans += (i >= 0 and k < n) * (i + 1) * (n - k)
107107
return ans
108108
```
@@ -236,7 +236,9 @@ class Solution {
236236
public:
237237
int unequalTriplets(vector<int>& nums) {
238238
unordered_map<int, int> cnt;
239-
for (int& v : nums) ++cnt[v];
239+
for (int& v : nums) {
240+
++cnt[v];
241+
}
240242
int ans = 0, a = 0;
241243
int n = nums.size();
242244
for (auto& [_, b] : cnt) {

solution/2400-2499/2475.Number of Unequal Triplets in Array/README_EN.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class Solution:
7272
ans, n = 0, len(nums)
7373
for j in range(1, n - 1):
7474
i = bisect_left(nums, nums[j], hi=j) - 1
75-
k = bisect_right(nums, nums[j], lo=j+1)
75+
k = bisect_right(nums, nums[j], lo=j + 1)
7676
ans += (i >= 0 and k < n) * (i + 1) * (n - k)
7777
return ans
7878
```
@@ -204,7 +204,9 @@ class Solution {
204204
public:
205205
int unequalTriplets(vector<int>& nums) {
206206
unordered_map<int, int> cnt;
207-
for (int& v : nums) ++cnt[v];
207+
for (int& v : nums) {
208+
++cnt[v];
209+
}
208210
int ans = 0, a = 0;
209211
int n = nums.size();
210212
for (auto& [_, b] : cnt) {

solution/2400-2499/2475.Number of Unequal Triplets in Array/Solution.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ class Solution {
22
public:
33
int unequalTriplets(vector<int>& nums) {
44
unordered_map<int, int> cnt;
5-
for (int& v : nums) ++cnt[v];
5+
for (int& v : nums) {
6+
++cnt[v];
7+
}
68
int ans = 0, a = 0;
79
int n = nums.size();
810
for (auto& [_, b] : cnt) {

0 commit comments

Comments
 (0)