leetcode-215-Kth Largest Element in an Array
来源:互联网 发布:淘宝联盟分享店铺 编辑:程序博客网 时间:2024/06/11 10:01
问题
题目:[Kth Largest Element in an Array]
思路
这个是我写的第一个版本,主要是利用partition算法。
我觉得它也是最快的,因为你要找的只是第K大,不是topK。
只是定位这一个元素,所以我采用了类似遮半查找非递归的写法实现。
代码
class Solution {public: int findKthLargest(vector<int>& nums, int k) { int n = nums.size(); if(k > n) return -1; int low = 0; int high = n-1; int pivot = -1; while(low <= high){ pivot = partition(nums, low, high); if(pivot == n-k) break; else if(pivot < n-k) low = pivot + 1; else high = pivot-1; } return nums[pivot]; }private: int partition(vector<int>& arr, int low, int high){ int i = low; int j = high; while(i < j){ while(i < j && arr[i] < arr[j]) --j; if(i<j){ swap(arr[i], arr[j]);++i; } while( i < j && arr[i] < arr[j] ) ++i; if(i<j){ swap(arr[i], arr[j]);--j; } } return i; } void swap(int& a, int& b){ int t = a; a = b; b = t; }};
思路
topK的办法
代码
class Solution {public: int findKthLargest(vector<int>& nums, int k) { int n = nums.size(); if(k>n) return -1; for( int i = n/2-1; i >= 0; --i ){ max_sift( nums, i, n-1 ); } for(int i = 0; i < k; ++i){ swap( nums[0], nums[n-1-i] ); max_sift( nums, 0, n-2-i ); } return nums[n-k]; }private: void max_sift(vector<int>& arr, int i, int m){ int j = 2*i + 1; while(j <= m){ if(j+1 <= m && arr[j] < arr[j+1]) ++j; if( arr[i] > arr[j] ) break; else{ swap(arr[i], arr[j]); i = j; j = 2*i + 1; } } return; }private: void swap(int& a, int& b){ int t = a; a = b; b = t; }};
思路
还是partition的思路
代码
class Solution {public: int findKthLargest(vector<int>& nums, int k) { int low = 0; int high = nums.size() - 1; int n = nums.size(); while(1){ int pivot = partition(nums, low, high); if( pivot == n-k ) return nums[pivot]; else if( pivot < n-k ) low = pivot + 1; else high = pivot - 1; } return -1; }private: void swap( int& a, int& b ){ int t = a; a = b; b = t; } int partition( vector<int>& arr, int low, int high){ int i = low; int j = high; while( i < j ){ while( i < j && arr[i] < arr[j] ) --j; if( i < j ){ swap( arr[i], arr[j] ); ++i; } while( i < j && arr[i] < arr[j] ) ++i; if( i < j ){ swap( arr[i], arr[j] ); --j; } } return i; }};
时间复杂度
这个题目最精妙的讨论是在时间复杂度上面。
先说partition算法,每次进行partition的时间复杂度是o(n)。
回想你写的代码,是不是两根指针相遇才结束,那不就是o(n)。
这个题我要知道partition的问题在哪里?partition的问题在于每次划分可能不平均。最好的情况是两分。因为假设不平均,那么partition的时间复杂度取决与区间的长度,划分不平均,自然要长一点。所以,问题在这里。
那么,假设最好的情况。
第一次划分,平均划分,t(n) = n. 平均意味着下次partition的区间长度为n/2;
第二次划分,平均划分,t(n) = n/2。时间消耗在partition上面。
假设找了K次找到了,是按复杂度
所以,总的时间函数为
因为,无论如何 k < n,那么对上面的等式放缩,有:
对右面取极限有,T(n) = O(n).
0 0
- Leetcode Kth Largest Element in an Array
- Leetcode: Kth Largest Element in an Array
- LeetCode Kth Largest Element in an Array
- [LeetCode] Kth Largest Element in an Array
- [leetcode] Kth Largest Element in an Array
- leetcode--Kth Largest Element in an Array
- #leetcode#Kth Largest Element in an Array
- LeetCode Kth Largest Element in an Array
- 【Leetcode】Kth Largest Element in an Array
- Leetcode: Kth Largest Element in an Array
- Kth Largest Element in an Array -- leetcode
- [Leetcode]Kth Largest Element in an Array
- [LeetCode]Kth Largest Element in an Array
- *LeetCode-Kth Largest Element in an Array
- LeetCode----Kth Largest Element in an Array
- LeetCode Kth Largest Element in an Array
- leetcode-Kth Largest Element in an Array
- LeetCode -- Kth Largest Element in an Array
- 开源的跨平台移动开发利器Xamarin Studio v6.3发布丨附下载
- @Configuration、@Bean
- 量化投资策略回测框架(一):胜率的估算
- netezza 数据库几个常用的日期处理函数
- 自定义控件<一>
- leetcode-215-Kth Largest Element in an Array
- Zookeeper入门学习(二)--Zookeeper环境搭建
- PHP static局部静态变量和全局静态变量总结
- java io操作之InputStream,OutputStream
- S4 1610 smartforms取消使用word编辑器
- 大数据|Spark技术在京东智能供应链预测的应用案例深度剖析
- C++第四次作业
- 关于《抑郁症》
- Linux 终端配置