> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/1387.-sort-integers-by-the-power-value.md).

# 1387. Sort Integers by The Power Value

## Memo + Sort

TC: O(nlogn + kn), k is getPower TC

S: O(n)

```java
class Solution {
    Map<Integer, Integer> memo = new HashMap<>(); //use common Map to save, same number's count
    
    public int getKth(int lo, int hi, int k) {
        int[][] result = new int[hi-lo+1][];
        for (int i = lo; i <= hi; i++) {
            result[i-lo] = new int[]{i, getPower(i)};
        }
        Arrays.sort(result, (a, b) -> a[1] - b[1]);
        return result[k-1][0];
    }
    
    private int getPower(int x) {
        if (x == 1) { // base case
            return 0;
        }
        if (memo.containsKey(x)) {
            return memo.get(x);
        }
        
        memo.put(x, (x % 2 == 0) ? getPower(x/2)+1 : getPower(3*x + 1)+1);
        return memo.get(x);
    }
}

/*
ex lo = 12, hi = 18

12 --> 6 --> 3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1

so 16 will cal again in the future (larger than frist one: 12)
so use a common map to memo

at last sort result
O(nk+ nlogn), k is the getPower's TC

a better one is using PQ ()

*/
```

## Memo + PQ

<pre><code><strong>O(nx+ nlogk)
</strong></code></pre>

```java
class Solution {
    Map<Integer, Integer> memo = new HashMap<>(); //use common Map to save, same number's count
    
    public int getKth(int lo, int hi, int k) {
        // count same: poll larger index, or poll larger count
        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> (a[1] == b[1]) ? b[0] - a[0] : b[1] - a[1]); 
        for (int i = lo; i <= hi; i++) {
            pq.offer(new int[]{i, getPower(i)});
            if (pq.size() > k) {
                pq.poll();
            }
        }
        return pq.poll()[0];
    }
    
    private int getPower(int x) {
        if (x == 1) { // base case
            return 0;
        }
        if (memo.containsKey(x)) {
            return memo.get(x);
        }
        
        memo.put(x, (x % 2 == 0) ? getPower(x/2)+1 : getPower(3*x + 1)+1);
        return memo.get(x);
    }
}

/*
ex lo = 12, hi = 18

12 --> 6 --> 3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1

so 16 will cal again in the future (larger than frist one: 12)
so use a common map to memo

at last sort result
O(nx+ nlogn), x is the getPower's TC

a better one is using PQ 
O(nx+ nlogk)

*/
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/1387.-sort-integers-by-the-power-value.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
