-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path1387.Sort-Integers-by-The-Power-Value.java
60 lines (48 loc) · 1.33 KB
/
1387.Sort-Integers-by-The-Power-Value.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// https://leetcode.com/problems/sort-integers-by-the-power-value/
// algorithms
// Medium (72.72%)
// Total Accepted: 5,983
// Total Submissions: 8,227
class Solution {
public int getKth(int lo, int hi, int k) {
ArrayList<Element> l = new ArrayList<Element>(hi - lo + 1);
for (int i = lo; i <= hi; i++) {
l.add(new Element(i));
}
Collections.sort(l);
return l.get(k - 1).getNum();
}
class Element implements Comparable<Element> {
private int num;
private int power;
Element(int num) {
this.num = num;
computePower();
}
private void computePower() {
int power = 0;
int curN = num;
while (curN != 1) {
if ((curN & 1) == 0) {
curN >>>= 1;
} else {
curN = curN * 3 + 1;
}
power++;
}
this.power = power;
}
@Override
public int compareTo(Element other) {
if (this.power < other.power) {
return -1;
} else if (this.power > other.power) {
return 1;
}
return this.num - other.num;
}
public int getNum() {
return num;
}
}
}