From 8014e9e0bfc723bdf90e5362fef582665539f851 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 22 Dec 2024 08:05:54 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1387 No.1387.Sort Integers by The Power Value --- .../README.md | 31 +++++++++++++++-- .../README_EN.md | 33 ++++++++++++++++--- .../Solution.rs | 20 +++++++++++ 3 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 solution/1300-1399/1387.Sort Integers by The Power Value/Solution.rs diff --git a/solution/1300-1399/1387.Sort Integers by The Power Value/README.md b/solution/1300-1399/1387.Sort Integers by The Power Value/README.md index a4b39d82ab3b8..d39b0459942e4 100644 --- a/solution/1300-1399/1387.Sort Integers by The Power Value/README.md +++ b/solution/1300-1399/1387.Sort Integers by The Power Value/README.md @@ -77,13 +77,13 @@ tags: ### 方法一:自定义排序 -我们先定义一个函数 $f(x)$,表示将数字 $x$ 变成 $1$ 所需要的步数,也即是数字 $x$ 的权重。 +我们先定义一个函数 $\textit{f}(x)$,表示将数字 $x$ 变成 $1$ 所需要的步数,也即是数字 $x$ 的权重。 -然后我们将区间 $[lo, hi]$ 内的所有数字按照权重升序排序,如果权重相同,按照数字自身的数值升序排序。 +然后我们将区间 $[\textit{lo}, \textit{hi}]$ 内的所有数字按照权重升序排序,如果权重相同,按照数字自身的数值升序排序。 最后返回排序后的第 $k$ 个数字。 -时间复杂度 $O(n \times \log n \times M)$,空间复杂度 $O(n)$。其中 $n$ 是区间 $[lo, hi]$ 内的数字个数,而 $M$ 是 $f(x)$ 的最大值,本题中 $M$ 最大为 $178$。 +时间复杂度 $O(n \times \log n \times M)$,空间复杂度 $O(n)$。其中 $n$ 是区间 $[\textit{lo}, \textit{hi}]$ 内的数字个数,而 $M$ 是 $f(x)$ 的最大值,本题中 $M$ 最大为 $178$。 @@ -225,6 +225,31 @@ function getKth(lo: number, hi: number, k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn get_kth(lo: i32, hi: i32, k: i32) -> i32 { + let f = |mut x: i32| -> i32 { + let mut ans = 0; + while x != 1 { + if x % 2 == 0 { + x /= 2; + } else { + x = 3 * x + 1; + } + ans += 1; + } + ans + }; + + let mut nums: Vec = (lo..=hi).collect(); + nums.sort_by(|&x, &y| f(x).cmp(&f(y))); + nums[(k - 1) as usize] + } +} +``` + diff --git a/solution/1300-1399/1387.Sort Integers by The Power Value/README_EN.md b/solution/1300-1399/1387.Sort Integers by The Power Value/README_EN.md index 94d8d2ed9d1e2..f9669642a685e 100644 --- a/solution/1300-1399/1387.Sort Integers by The Power Value/README_EN.md +++ b/solution/1300-1399/1387.Sort Integers by The Power Value/README_EN.md @@ -75,13 +75,13 @@ The fourth number in the sorted array is 7. ### Solution 1: Custom Sorting -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$. +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$. -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. +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. -Finally, we return the $k$-th number after sorting. +Finally, we return the $k$-th number in the sorted list. -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$. +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. @@ -223,6 +223,31 @@ function getKth(lo: number, hi: number, k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn get_kth(lo: i32, hi: i32, k: i32) -> i32 { + let f = |mut x: i32| -> i32 { + let mut ans = 0; + while x != 1 { + if x % 2 == 0 { + x /= 2; + } else { + x = 3 * x + 1; + } + ans += 1; + } + ans + }; + + let mut nums: Vec = (lo..=hi).collect(); + nums.sort_by(|&x, &y| f(x).cmp(&f(y))); + nums[(k - 1) as usize] + } +} +``` + diff --git a/solution/1300-1399/1387.Sort Integers by The Power Value/Solution.rs b/solution/1300-1399/1387.Sort Integers by The Power Value/Solution.rs new file mode 100644 index 0000000000000..4498b8c9c1263 --- /dev/null +++ b/solution/1300-1399/1387.Sort Integers by The Power Value/Solution.rs @@ -0,0 +1,20 @@ +impl Solution { + pub fn get_kth(lo: i32, hi: i32, k: i32) -> i32 { + let f = |mut x: i32| -> i32 { + let mut ans = 0; + while x != 1 { + if x % 2 == 0 { + x /= 2; + } else { + x = 3 * x + 1; + } + ans += 1; + } + ans + }; + + let mut nums: Vec = (lo..=hi).collect(); + nums.sort_by(|&x, &y| f(x).cmp(&f(y))); + nums[(k - 1) as usize] + } +}