Skip to content

Commit 517c423

Browse files
committed
Add solution #1387
1 parent 76e65d9 commit 517c423

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,271 LeetCode solutions in JavaScript
1+
# 1,272 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1057,6 +1057,7 @@
10571057
1383|[Maximum Performance of a Team](./solutions/1383-maximum-performance-of-a-team.js)|Hard|
10581058
1385|[Find the Distance Value Between Two Arrays](./solutions/1385-find-the-distance-value-between-two-arrays.js)|Easy|
10591059
1386|[Cinema Seat Allocation](./solutions/1386-cinema-seat-allocation.js)|Medium|
1060+
1387|[Sort Integers by The Power Value](./solutions/1387-sort-integers-by-the-power-value.js)|Medium|
10601061
1389|[Create Target Array in the Given Order](./solutions/1389-create-target-array-in-the-given-order.js)|Easy|
10611062
1400|[Construct K Palindrome Strings](./solutions/1400-construct-k-palindrome-strings.js)|Medium|
10621063
1402|[Reducing Dishes](./solutions/1402-reducing-dishes.js)|Hard|
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* 1387. Sort Integers by The Power Value
3+
* https://leetcode.com/problems/sort-integers-by-the-power-value/
4+
* Difficulty: Medium
5+
*
6+
* The power of an integer x is defined as the number of steps needed to transform x into
7+
* 1 using the following steps:
8+
* - if x is even then x = x / 2
9+
* - if x is odd then x = 3 * x + 1
10+
*
11+
* For example, the power of x = 3 is 7 because 3 needs 7 steps to become 1
12+
* (3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1).
13+
*
14+
* Given three integers lo, hi and k. The task is to sort all integers in the interval
15+
* [lo, hi] by the power value in ascending order, if two or more integers have the same
16+
* power value sort them by ascending order.
17+
*
18+
* Return the kth integer in the range [lo, hi] sorted by the power value.
19+
*
20+
* Notice that for any integer x (lo <= x <= hi) it is guaranteed that x will transform
21+
* into 1 using these steps and that the power of x is will fit in a 32-bit signed integer.
22+
*/
23+
24+
/**
25+
* @param {number} lo
26+
* @param {number} hi
27+
* @param {number} k
28+
* @return {number}
29+
*/
30+
var getKth = function(lo, hi, k) {
31+
const powerValues = new Map();
32+
const numbers = Array.from({ length: hi - lo + 1 }, (_, i) => lo + i);
33+
34+
numbers.sort((a, b) => {
35+
const powerA = getPower(a);
36+
const powerB = getPower(b);
37+
return powerA === powerB ? a - b : powerA - powerB;
38+
});
39+
40+
return numbers[k - 1];
41+
42+
function calculatePower(num) {
43+
let steps = 0;
44+
while (num !== 1) {
45+
if (num % 2 === 0) {
46+
num = num / 2;
47+
} else {
48+
num = 3 * num + 1;
49+
}
50+
steps++;
51+
}
52+
return steps;
53+
}
54+
55+
function getPower(num) {
56+
if (!powerValues.has(num)) {
57+
powerValues.set(num, calculatePower(num));
58+
}
59+
return powerValues.get(num);
60+
}
61+
};

0 commit comments

Comments
 (0)