lintcode 中等题:kth-largest-element 第k大元素
题目
第k大元素
在数组中找到第k大的元素
给出数组[9,3,2,4,8],第三大的元素是4
给出数组 [1,2,3,4,5],第一大的元素是5,第二大的元素是4,第三大的元素是3,以此类推
你可以交换数组中的元素的位置
要求时间复杂度为O(n),空间复杂度为O(1)
解题
理论快速排序的思想,每次都减半,这个时间复杂度也是O(N),至于为什么就不知道了
class Solution {
/*
* @param k : description of k
* @param nums : array of nums
* @return: description of return
*/
public int kthLargestElement(int k, int[] nums) {
// write your code here
return quickSort(nums,0,nums.length-1,k); }
public int quickSort(int[] nums,int left,int right,int k){
int i = left;
int j = right;
int tmp = nums[i];
while(i<j){
while(i<j && tmp>=nums[j]) j--;
if(i<j){
nums[i]=nums[j];
i++;
}
while(i<j && tmp<nums[i]) i++;
if(i<j){
nums[j]=nums[i];
j--;
} }
if(i == k -1){
return tmp;
}else if(i< k-1){
return quickSort(nums,i+1,right,k);
}else{
return quickSort(nums,left,i-1,k);
}
}
};
Python
class Solution:
# @param k & A a integer and an array
# @return ans a integer
def kthLargestElement(self, k, A):
return self.quickSort(A,0,len(A)-1,k) def quickSort(self,nums,left,right,k):
i = left
j = right
tmp = nums[i]
while i<j:
while i<j and tmp>=nums[j]:
j -= 1
if i<j:
nums[i] = nums[j]
i += 1
while i<j and tmp< nums[i]:
i += 1
if i<j:
nums[j] = nums[i]
j -= 1
if i == k-1:
return tmp
elif i< k-1:
return self.quickSort(nums,i+1,right,k)
else:
return self.quickSort(nums,left,i-1,k)
Python Code
lintcode 中等题:kth-largest-element 第k大元素的更多相关文章
- Leetcode 703题数据流中的第K大元素(Kth Largest Element in a Stream)Java语言求解
题目链接 https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/ 题目内容 设计一个找到数据流中第K大元素的类(class) ...
- LeetCode算法题-Kth Largest Element in a Stream(Java实现)
这是悦乐书的第296次更新,第315篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第164题(顺位题号是703).设计一个类来查找流中第k个最大元素.请注意,它是排序顺序 ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- Lintcode: Kth Largest Element 解题报告
Kth Largest Element Find K-th largest element in an array. Note You can swap elements in the array E ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- [LeetCode] Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- [Swift]LeetCode215. 数组中的第K个最大元素 | Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- [Swift]LeetCode703. 数据流中的第K大元素 | Kth Largest Element in a Stream
Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...
- [LeetCode] Kth Largest Element in a Stream 数据流中的第K大的元素
Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...
- [leetcode]215. Kth Largest Element in an Array 数组中第k大的元素
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
随机推荐
- MongoDB如何存储数据
想要深入了解MongoDB如何存储数据之前,有一个概念必须清楚,那就是Memeory-Mapped Files. Memeory-Mapped Files 下图展示了数据库是如何跟底层系统打交道的. ...
- 【WinForm】线程中向listview添加数据
在使用listview的时候,因为处理的数据较多,为防止在处理数据时出现假死的状态出现卡的情况,我们使用委托进行处理添加数据 定义委托 private delegate void AddListVie ...
- Winform合并多个Excel文件到一个文件中(源文件.xls,实际是.xml)
1.下面两个文件.xls是给的文件,实际上是.xml文件 2.具体的代码 private void btOK_Click(object sender, EventArgs e) { //0.获取路径文 ...
- sublime text3的配置(整理)
一.代码片段 开发人员很多时候是在做一些重复的工作. 针对不同数据表的增删改查都差不多,重复来重去的.很久不写程序了,利用十一假期在家看看书,写写程序. 最近一直很喜欢使用Sublime Text,发 ...
- phpExcel导出excel的类,每步都有说明
require_once WEB_PATH . '/lib/PHPExcel/PHPExcel.php'; require_once WEB_PATH . '/lib/PHPExcel/PHPExce ...
- PHP:strpos()-返回字符串在另一个字符串中第一次出现的位置
strpos()函数返回字符串在另一个字符串中第一次出现的位置.如果没有找到该字符串,则返回false. 语法:strpos(sting, find [, start]) string ,必须,要搜索 ...
- 两个Activity之间的交互startActivityForResult的使用
代码如下: package com.zzw.teststartintentforrequest; import android.app.Activity; import android.content ...
- 测试使用Windows Live Writer
目前是在win7系统下面使用的,曾经在winxp下去配置,却失败了,难道不支持xp? Windows Live Writer好像不支持代码关键字高亮显示啊. int main(void) { prin ...
- 自改xss小平台上线
原先一直用xss.hk结果不知怎么被关的,正好手上有代码于是自己搭了一个,网上的类似的xss平台大多一样,原先的xss的chrome插件,不适合 "manifest_version" ...
- hdu 5545 The Battle of Guandu spfa最短路
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5545 题意:有N个村庄, M 个战场: $ 1 <=N,M <= 10^5 $; 其中曹 ...