<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>

class Solution {
public:
    
    void insertFromRootHeap(int current_parent, vector<int>& nums,int size){
        // cout << "now here is "<< nums[current_parent] <<endl;
        // if (nums[current_parent] == 3){
        // cout << "left is" << nums[current_parent*2 + 1] <<endl;
        // cout << "right is "<< nums[current_parent*2 + 2] <<endl;
        // }
        while(current_parent<size){
            
            int left = current_parent*2 + 1; 
            int right = current_parent*2 +2;
            int next_parent = current_parent;
            if(left<size && nums[next_parent]>nums[left]){
                next_parent = left;
            }
            if(right<size && nums[next_parent]>nums[right]){
                next_parent = right;
            } 
            if (next_parent == current_parent){
                break;
            }
            swap(nums[current_parent],nums[next_parent]);
            current_parent = next_parent;
        }
    }
    int findKthLargest(vector<int>& nums, int k) {
        
        priority_queue<int,vector<int>,greater<int>> pq;//min heap of size k
        for(int& num:nums){
            if(pq.size()<k){
                pq.push(num);
            }
            else{
                if(pq.top()<num){
                    pq.pop();
                    pq.push(num);
                }
            }
        }
        
        return pq.top();
    }
};
61400