leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1387.cpp (741B)


0 class Solution { 1 static inline constexpr const array<int, 1001> dp = []() constexpr -> array<int, 1001> { 2 array<int, 1001> dp; 3 dp[0] = 0; 4 for (int i = 1; i <= 1000; i++) { 5 int res = 0, n = i; 6 while (n != 1) { 7 n = (n % 2) ? 3 * n + 1 : n / 2; 8 res++; 9 } 10 dp[i] = res; 11 } 12 return dp; 13 }(); 14 15 public: 16 int getKth(int lo, int hi, int k) { 17 vector<int> vec(hi - lo + 1); 18 19 iota(begin(vec), end(vec), lo); 20 nth_element(begin(vec), begin(vec) + k - 1, end(vec), 21 [](const int a, const int b) { return dp[a] == dp[b] ? a < b : dp[a] < dp[b]; }); 22 23 return vec[k - 1]; 24 } 25 };