-
Notifications
You must be signed in to change notification settings - Fork 8
/
power.java
36 lines (34 loc) · 1.01 KB
/
power.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
class Solution {
static Map<Integer, Integer> powers = new HashMap<>();
static {
powers.put(1, 0);
}
public int getKth(int lo, int hi, int k) {
List<Pair<Integer, Integer>> ans = new ArrayList<>();
for (int i = lo; i <= hi; i++) {
int curPower = getPower(i);
ans.add(new Pair<>(i, curPower));
}
ans.sort((p1, p2) -> {
if ((int)p1.getValue() != p2.getValue()) {
return p1.getValue().compareTo(p2.getValue());
}
return p1.getKey().compareTo(p2.getKey());
});
return ans.get(k - 1).getKey();
}
private int getPower(int num) {
Integer pow = powers.get(num);
if (pow != null) {
return pow;
}
if (num % 2 == 0) {
int power = getPower(num / 2) + 1;
powers.put(num, power);
return power;
}
int power = getPower(3 * num + 1) + 1;
powers.put(num, power);
return power;
}
}