Skip to content

Commit 5d9a5b3

Browse files
authored
Create 1387-sort-integers-by-the-power-value.js
1 parent 05bc8c5 commit 5d9a5b3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number} lo
3+
* @param {number} hi
4+
* @param {number} k
5+
* @return {number}
6+
*/
7+
const getKth = function(lo, hi, k) {
8+
const arr = []
9+
10+
for(let i = lo; i <= hi; i++) {
11+
arr.push([helper(i), i])
12+
}
13+
14+
arr.sort((a, b) => a[0] === b[0] ? a[1] - b[1] : a[0] - b[0])
15+
16+
return arr[k - 1][1]
17+
18+
19+
function helper(num) {
20+
let res = 0
21+
while(num !== 1) {
22+
if(num % 2 === 0) num /= 2
23+
else num = num * 3 + 1
24+
res++
25+
}
26+
27+
return res
28+
}
29+
};

0 commit comments

Comments
 (0)