Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 60d86dc

Browse files
feat: add solutions to lc problem: No.1387 (doocs#3875)
No.1387.Sort Integers by The Power Value
1 parent 2071c73 commit 60d86dc

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

‎solution/1300-1399/1387.Sort Integers by The Power Value/README.md‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ tags:
7777

7878
### 方法一:自定义排序
7979

80-
我们先定义一个函数 $f(x),ドル表示将数字 $x$ 变成 1ドル$ 所需要的步数,也即是数字 $x$ 的权重。
80+
我们先定义一个函数 $\textit{f}(x),ドル表示将数字 $x$ 变成 1ドル$ 所需要的步数,也即是数字 $x$ 的权重。
8181

82-
然后我们将区间 $[lo, hi]$ 内的所有数字按照权重升序排序,如果权重相同,按照数字自身的数值升序排序。
82+
然后我们将区间 $[\textit{lo}, \textit{hi}]$ 内的所有数字按照权重升序排序,如果权重相同,按照数字自身的数值升序排序。
8383

8484
最后返回排序后的第 $k$ 个数字。
8585

86-
时间复杂度 $O(n \times \log n \times M),ドル空间复杂度 $O(n)$。其中 $n$ 是区间 $[lo, hi]$ 内的数字个数,而 $M$ 是 $f(x)$ 的最大值,本题中 $M$ 最大为 178ドル$。
86+
时间复杂度 $O(n \times \log n \times M),ドル空间复杂度 $O(n)$。其中 $n$ 是区间 $[\textit{lo}, \textit{hi}]$ 内的数字个数,而 $M$ 是 $f(x)$ 的最大值,本题中 $M$ 最大为 178ドル$。
8787

8888
<!-- tabs:start -->
8989

@@ -225,6 +225,31 @@ function getKth(lo: number, hi: number, k: number): number {
225225
}
226226
```
227227

228+
#### Rust
229+
230+
```rust
231+
impl Solution {
232+
pub fn get_kth(lo: i32, hi: i32, k: i32) -> i32 {
233+
let f = |mut x: i32| -> i32 {
234+
let mut ans = 0;
235+
while x != 1 {
236+
if x % 2 == 0 {
237+
x /= 2;
238+
} else {
239+
x = 3 * x + 1;
240+
}
241+
ans += 1;
242+
}
243+
ans
244+
};
245+
246+
let mut nums: Vec<i32> = (lo..=hi).collect();
247+
nums.sort_by(|&x, &y| f(x).cmp(&f(y)));
248+
nums[(k - 1) as usize]
249+
}
250+
}
251+
```
252+
228253
<!-- tabs:end -->
229254

230255
<!-- solution:end -->

‎solution/1300-1399/1387.Sort Integers by The Power Value/README_EN.md‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ The fourth number in the sorted array is 7.
7575

7676
### Solution 1: Custom Sorting
7777

78-
First, we define a function $f(x),ドル which represents the number of steps required to change the number $x$ to 1ドル,ドル i.e., the weight of the number $x$.
78+
First, we define a function $\textit{f}(x),ドル which represents the number of steps required to transform the number $x$ into 1ドル,ドル i.e., the power value of the number $x$.
7979

80-
Then, we sort all the numbers in the interval $[lo, hi]$ in ascending order of weight. If the weights are the same, we sort them in ascending order of the numbers themselves.
80+
Then, we sort all the numbers in the interval $[\textit{lo}, \textit{hi}]$ in ascending order based on their power values. If the power values are the same, we sort them in ascending order based on the numbers themselves.
8181

82-
Finally, we return the $k$-th number after sorting.
82+
Finally, we return the $k$-th number in the sorted list.
8383

84-
The time complexity is $O(n \times \log n \times M),ドル and the space complexity is $O(n)$. Where $n$ is the number of numbers in the interval $[lo, hi],ドル and $M$ is the maximum value of $f(x)$. In this problem, the maximum value of $M$ is 178ドル$.
84+
The time complexity is $O(n \times \log n \times M),ドル and the space complexity is $O(n)$. Here, $n$ is the number of numbers in the interval $[\textit{lo}, \textit{hi}],ドル and $M$ is the maximum value of $f(x)$, which is at most 178ドル$ in this problem.
8585

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

@@ -223,6 +223,31 @@ function getKth(lo: number, hi: number, k: number): number {
223223
}
224224
```
225225

226+
#### Rust
227+
228+
```rust
229+
impl Solution {
230+
pub fn get_kth(lo: i32, hi: i32, k: i32) -> i32 {
231+
let f = |mut x: i32| -> i32 {
232+
let mut ans = 0;
233+
while x != 1 {
234+
if x % 2 == 0 {
235+
x /= 2;
236+
} else {
237+
x = 3 * x + 1;
238+
}
239+
ans += 1;
240+
}
241+
ans
242+
};
243+
244+
let mut nums: Vec<i32> = (lo..=hi).collect();
245+
nums.sort_by(|&x, &y| f(x).cmp(&f(y)));
246+
nums[(k - 1) as usize]
247+
}
248+
}
249+
```
250+
226251
<!-- tabs:end -->
227252

228253
<!-- solution:end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn get_kth(lo: i32, hi: i32, k: i32) -> i32 {
3+
let f = |mut x: i32| -> i32 {
4+
let mut ans = 0;
5+
while x != 1 {
6+
if x % 2 == 0 {
7+
x /= 2;
8+
} else {
9+
x = 3 * x + 1;
10+
}
11+
ans += 1;
12+
}
13+
ans
14+
};
15+
16+
let mut nums: Vec<i32> = (lo..=hi).collect();
17+
nums.sort_by(|&x, &y| f(x).cmp(&f(y)));
18+
nums[(k - 1) as usize]
19+
}
20+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /