<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p>

<p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p>

<p>Can you solve it without sorting?</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [3,2,1,5,6,4], k = 2
<strong>Output:</strong> 5
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [3,2,3,1,2,4,5,5,6], k = 4
<strong>Output:</strong> 4
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

struct Heap
{
    int* arr = new int[100000];
    int n = 0;
    Heap() {}
    void insert(int x)
    {
        arr[n] = x;
        int i = n;
        while (i > 0 && arr[(i-1)/2] < arr[i])
        {
            swap(arr[(i-1)/2], arr[i]);
            i = (i-1)/2;
        }
        ++n;
    }
    int getTop()
    {
        int res = arr[0];
        arr[0] = arr[n-1];
        int i = 0;
        while (i < n)
        {
            int maxIndex = i;
            if (2*i + 1 < n && arr[2*i + 1] > arr[maxIndex]) maxIndex = 2*i + 1;
            if (2*i + 2 < n && arr[2*i + 2] > arr[maxIndex]) maxIndex = 2*i + 2;
            if (maxIndex == i) break;
            swap(arr[i], arr[maxIndex]);
            i = maxIndex;
        }
        --n;
        return res;
    }
};
class Solution 
{
public:
    int findKthLargest(vector<int>& nums, int k) 
    {
        Heap hp;
        for(int& num : nums) hp.insert(num);
        int res = -1;
        for(int i = 0; i < k; ++i)
        {
            res = hp.getTop();
        }
        return res;
    }
};
62300