forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1387.java
31 lines (28 loc) · 877 Bytes
/
_1387.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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class _1387 {
public static class Solution1 {
public int getKth(int lo, int hi, int k) {
List<int[]> power = new ArrayList<>();
for (int i = lo; i <= hi; i++) {
power.add(new int[]{getSteps(i), i});
}
Collections.sort(power, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);
return power.get(k - 1)[1];
}
private int getSteps(int number) {
int steps = 0;
while (number != 1) {
if (number % 2 == 0) {
number /= 2;
} else {
number = 3 * number + 1;
}
steps++;
}
return steps;
}
}
}