-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy path1387-SortIntegersByThePowerValue.cs
More file actions
35 lines (30 loc) · 1 KB
/
1387-SortIntegersByThePowerValue.cs
File metadata and controls
35 lines (30 loc) · 1 KB
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
//-----------------------------------------------------------------------------
// Runtime: 100ms
// Memory Usage: 16.2 MB
// Link: https://leetcode.com/submissions/detail/361296227/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _1387_SortIntegersByThePowerValue
{
public int GetKth(int lo, int hi, int k)
{
var list = new List<(int num, int powerValue)>();
for (int i = lo; i <= hi; i++)
list.Add((i, PowerValue(i)));
list.Sort((a, b) => a.powerValue == b.powerValue ? a.num.CompareTo(b.num) : a.powerValue.CompareTo(b.powerValue));
return list[k - 1].num;
}
private int PowerValue(int num)
{
int count = 0;
while (num != 1)
{
num = num % 2 == 0 ? num / 2 : (num * 3 + 1);
count++;
}
return count;
}
}
}