Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int QuickSortIndex(vector<int>& nums, int i, int j){
- int pivot = i;
- while(i <= j){
- while(i<nums.size() && nums[i] <= nums[pivot]){
- i++;
- }
- while(j >= 0 && nums[j] > nums[pivot]){
- j--;
- }
- if(i <j ){
- swap(nums[i], nums[j]);
- }
- }
- swap(nums[j], nums[pivot]);
- return j;
- }
- int findKthLargest(vector<int>& nums, int k) {
- int i=0, j=nums.size()-1;
- while(i<=j){
- int pivot = QuickSortIndex(nums, i, j);
- if(pivot == nums.size()- k){
- return nums[pivot];
- }
- if(pivot > nums.size() - k){
- j = pivot - 1;
- }
- else{
- i = pivot + 1;
- }
- }
- return -1;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment