class Solution { public: int findKthLargest(vector<int>& nums, int k) { int left = 0, right = nums.size() - 1; while (true) { int p = partition(nums, left, right); if (p == k-1) return nums[p]; if (p > k-1) right = p - 1; else left = p + 1; } return -1; } int partition(vector<int>& nums, int left, int right) { int p = nums[left]; int i = left + 1, j = right; while (i <= j) { if (nums[i] < p && nums[j] > p) { swap(nums[i], nums[j]); i++; j--; } else if (nums[i] >= p) i++; else if (nums[j] <= p) j--; } swap(nums[left], nums[j]); return j; } };