Skip to content

Commit e519bfe

Browse files
committed
一刷1387
1 parent eb053de commit e519bfe

File tree

5 files changed

+94
-46
lines changed

5 files changed

+94
-46
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9735,12 +9735,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
97359735
//|Medium
97369736
//|
97379737

9738-
//|{counter:codes}
9739-
//|{leetcode_base_url}/sort-integers-by-the-power-value/[1387. Sort Integers by The Power Value^]
9740-
//|{source_base_url}/_1387_SortIntegersByThePowerValue.java[Java]
9741-
//|{doc_base_url}/1387-sort-integers-by-the-power-value.adoc[题解]
9742-
//|Medium
9743-
//|
9738+
|{counter:codes}
9739+
|{leetcode_base_url}/sort-integers-by-the-power-value/[1387. Sort Integers by The Power Value^]
9740+
|{source_base_url}/_1387_SortIntegersByThePowerValue.java[Java]
9741+
|{doc_base_url}/1387-sort-integers-by-the-power-value.adoc[题解]
9742+
|Medium
9743+
|
97449744

97459745
//|{counter:codes}
97469746
//|{leetcode_base_url}/pizza-with-3n-slices/[1388. Pizza With 3n Slices^]
Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,49 @@
11
[#1387-sort-integers-by-the-power-value]
2-
= 1387. Sort Integers by The Power Value
2+
= 1387. 将整数按权重排序
33

4-
{leetcode}/problems/sort-integers-by-the-power-value/[LeetCode - 1387. Sort Integers by The Power Value ^]
4+
https://leetcode.cn/problems/sort-integers-by-the-power-value/[LeetCode - 1387. 将整数按权重排序 ^]
55

6-
The power of an integer `x` is defined as the number of steps needed to transform `x` into `1` using the following steps:
6+
我们将整数 `x` *权重* 定义为按照下述规则将 `x` 变成 `1` 所需要的步数:
77

8+
* 如果 `x` 是偶数,那么 `+x = x / 2+`
9+
* 如果 `x` 是奇数,那么 `+x = 3 * x + 1+`
810
9-
* if `x` is even then `x = x / 2`
10-
* if `x` is odd then `x = 3 * x + 1`
11+
比方说,x=3 的权重为 7 。因为 3 需要 7 步变成 1 (3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1)。
1112

13+
给你三个整数 `lo``hi``k` 。你的任务是将区间 `[lo, hi]` 之间的整数按照它们的权重 *升序排序 *,如果大于等于 2 个整数有 *相同* 的权重,那么按照数字自身的数值 *升序排序*
1214

13-
For example, the power of `x = 3` is `7` because `3` needs `7` steps to become `1` (`3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1`).
15+
请你返回区间 `[lo, hi]` 之间的整数按权重排序后的第 `k` 个数。
1416

15-
Given three integers `lo`, `hi` and `k`. The task is to sort all integers in the interval `[lo, hi]` by the power value in *ascending order*, if two or more integers have *the same* power value sort them by *ascending order*.
17+
注意,题目保证对于任意整数 `x` `+(lo <= x <= hi)+` ,它变成 `1` 所需要的步数是一个 32 位有符号整数。
1618

17-
Return the `k^th^` integer in the range `[lo, hi]` sorted by the power value.
19+
*示例 1:*
1820

19-
Notice that for any integer `x` `(lo <= x <= hi)` it is *guaranteed* that `x` will transform into `1` using these steps and that the power of `x` is will *fit* in a 32-bit signed integer.
21+
....
22+
输入:lo = 12, hi = 15, k = 2
23+
输出:13
24+
解释:12 的权重为 9(12 --> 6 --> 3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1)
25+
13 的权重为 9
26+
14 的权重为 17
27+
15 的权重为 17
28+
区间内的数按权重排序以后的结果为 [12,13,14,15] 。对于 k = 2 ,答案是第二个整数也就是 13 。
29+
注意,12 和 13 有相同的权重,所以我们按照它们本身升序排序。14 和 15 同理。
30+
....
2031

21-
22-
*Example 1:*
32+
*示例 2:*
2333

24-
[subs="verbatim,quotes"]
25-
----
26-
*Input:* lo = 12, hi = 15, k = 2
27-
*Output:* 13
28-
*Explanation:* The power of 12 is 9 (12 --> 6 --> 3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1)
29-
The power of 13 is 9
30-
The power of 14 is 17
31-
The power of 15 is 17
32-
The interval sorted by the power value [12,13,14,15]. For k = 2 answer is the second element which is 13.
33-
Notice that 12 and 13 have the same power value and we sorted them in ascending order. Same for 14 and 15.
34-
----
35-
36-
*Example 2:*
37-
38-
[subs="verbatim,quotes"]
39-
----
40-
*Input:* lo = 7, hi = 11, k = 4
41-
*Output:* 7
42-
*Explanation:* The power array corresponding to the interval [7, 8, 9, 10, 11] is [16, 3, 19, 6, 14].
43-
The interval sorted by power is [8, 10, 11, 7, 9].
44-
The fourth number in the sorted array is 7.
45-
----
34+
....
35+
输入:lo = 7, hi = 11, k = 4
36+
输出:7
37+
解释:区间内整数 [7, 8, 9, 10, 11] 对应的权重为 [16, 3, 19, 6, 14] 。
38+
按权重排序后得到的结果为 [8, 10, 11, 7, 9] 。
39+
排序后数组中第 4 个数字为 7 。
40+
....
4641

47-
48-
*Constraints:*
4942

43+
*提示:*
5044

51-
* `1 <= lo <= hi <= 1000`
52-
* `1 <= k <= hi - lo + 1`
53-
45+
* `+1 <= lo <= hi <= 1000+`
46+
* `+1 <= k <= hi - lo + 1+`
5447
5548
5649
@@ -82,4 +75,5 @@ include::{sourcedir}/_1387_SortIntegersByThePowerValue.java[tag=answer]
8275

8376
== 参考资料
8477

85-
78+
. https://leetcode.cn/problems/sort-integers-by-the-power-value/solutions/168355/jiang-zheng-shu-an-quan-zhong-pai-xu-by-leetcode-s/[1387. 将整数按权重排序 - 官方题解^]
79+
. https://leetcode.cn/problems/sort-integers-by-the-power-value/solutions/3011028/bing-bao-cai-xiang-ji-yi-hua-sou-suo-pyt-q5iz/[1387. 将整数按权重排序 - 冰雹猜想,记忆化搜索^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2857,7 +2857,7 @@ include::1349-maximum-students-taking-exam.adoc[leveloffset=+1]
28572857

28582858
// include::1386-cinema-seat-allocation.adoc[leveloffset=+1]
28592859

2860-
// include::1387-sort-integers-by-the-power-value.adoc[leveloffset=+1]
2860+
include::1387-sort-integers-by-the-power-value.adoc[leveloffset=+1]
28612861

28622862
// include::1388-pizza-with-3n-slices.adoc[leveloffset=+1]
28632863

logbook/202503.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,10 @@ endif::[]
838838
|{doc_base_url}/0915-partition-array-into-disjoint-intervals.adoc[题解]
839839
|✅ 两遍遍历。有题解可以不到一遍遍历即可解决问题。
840840

841+
|{counter:codes2503}
842+
|{leetcode_base_url}/sort-integers-by-the-power-value/[1387. Sort Integers by The Power Value^]
843+
|{doc_base_url}/1387-sort-integers-by-the-power-value.adoc[题解]
844+
|✅ 先计算每个数字的权重,再进行比较:使用集合存储下标,通过下标找到对应的权重。计算权重时,可以递归,也可以直接循环。使用递归时,可以使用备忘录把已经计算的值存储起来,防止重复计算。
841845

842846
|===
843847

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _1387_SortIntegersByThePowerValue {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-05-27 19:55:53
11+
*/
12+
public int getKth(int lo, int hi, int k) {
13+
int length = hi - lo + 1;
14+
int[] step = new int[length];
15+
for (int i = lo; i <= hi; i++) {
16+
step[i - lo] = calc(i);
17+
}
18+
List<Integer> index = new ArrayList<>(length);
19+
for (int i = 0; i < length; i++) {
20+
index.add(i);
21+
}
22+
index.sort((a, b) -> {
23+
int result = Integer.compare(step[a], step[b]);
24+
if (result == 0) {
25+
return Integer.compare(a, b);
26+
} else {
27+
return result;
28+
}
29+
});
30+
return index.get(k - 1) + lo;
31+
}
32+
33+
private int calc(int num) {
34+
int result = 0;
35+
while (num > 1) {
36+
if ((num & 1) == 0) {
37+
num /= 2;
38+
} else {
39+
num = 3 * num + 1;
40+
}
41+
result++;
42+
}
43+
return result;
44+
}
45+
// end::answer[]
46+
47+
public static void main(String[] args) {
48+
new _1387_SortIntegersByThePowerValue().getKth(7, 11, 4);
49+
}
50+
}

0 commit comments

Comments
 (0)