<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:
    def findKthLargest(self, nums: List[int], k: int) -> int:
        heap = nums

        def swap(i1,i2):
            temp = heap[i1]
            heap[i1] = heap[i2]
            heap[i2] = temp
        
        def heapify(i, size):
            largest = i
            left = 2*i + 1
            right = 2*i + 2
            if left < size and heap[largest] < heap[left]:
                largest = left
            if right < size and heap[largest] < heap[right]:
                largest = right
            if i != largest:
                swap(i, largest)
                heapify(largest, size)
            
        def maxHeap():
            size = len(heap)
            for i in range(size//2-1, -1, -1):
                heapify(i, size)
        
        def delete():
            heap[0] = heap.pop()
            size = len(heap)
            heapify(0, size)
        
        
        maxHeap()
        for i in range(k-1):
            delete()
        
        return heap[0]



31687