kth

#Leetcode# 215. Kth Largest Element in an Array

六月ゝ 毕业季﹏ 提交于 2020-03-29 18:06:27
https://leetcode.com/problems/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 sorted order, not the kth distinct element. Example 1: Input: [3,2,1,5,6,4] and k = 2 Output: 5 Example 2: Input: [3,2,3,1,2,4,5,5,6] and k = 4 Output: 4 Note: You may assume k is always valid, 1 ≤ k ≤ array's length. 代码: class Solution { public: int findKthLargest(vector<int>& nums, int k) { int n = nums.size(); int ans; sort(nums.begin(), nums.end()); ans = nums[n - k]; return ans; } };   FHFHFH 来源: https://www.cnblogs.com/zlrrrr/p

LeetCode-215. Kth Largest Element in an Array

心已入冬 提交于 2020-03-29 18:06:07
https://leetcode.com/problems/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 sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array's length. Solution # 快排 class Solution(object): def findKthLargest(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ #return sorted(nums)[-k] left = 0 right = len(nums) - 1 while True: temp = self.partition(nums,left,right) # print temp if temp == k -1: return nums

LeetCode 215 : Kth Largest Element in an Array

我只是一个虾纸丫 提交于 2020-03-27 02:41:46
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array's length. 最简单的想法:先排序,然后倒数k个元素就是 时间复杂度:O(nlogn) public class Solution { public int findKthLargest(int[] nums, int k) { Arrays.sort(nums); return nums[nums.length-k]; } } 优化:quicksort 中 partition 的思想 就像quickselect一样,时间复杂度应该是 O(n) public class Solution { public int findKthLargest(int[] nums, int k) { return helper(nums, 0, nums.length-1, k); } public

LeetCode | 0215. Kth Largest Element in an Array数组中的第K个最大元素【Python】

吃可爱长大的小学妹 提交于 2020-02-15 01:37:14
LeetCode 0215. Kth Largest Element in an Array数组中的第K个最大元素【Medium】【Python】【快排】【堆】 Problem LeetCode Find the k th largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1: Input: [3,2,1,5,6,4] and k = 2 Output: 5 Example 2: Input: [3,2,3,1,2,4,5,5,6] and k = 4 Output: 4 Note: You may assume k is always valid, 1 ≤ k ≤ array’s length. 问题 力扣 在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 示例 2: 输入: [3,2,3,1,2,4,5,5,6] 和 k = 4 输出: 4 说明: 你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度。

215. Kth Largest Element in an Array

送分小仙女□ 提交于 2020-01-20 16:36:18
https://www.jianshu.com/p/b30955885e6d Find the k th largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Approach 1: Priority Queue class Solution { public int findKthLargest ( int [ ] nums , int k ) { PriorityQueue < Integer > minQ = new PriorityQueue < > ( k ) ; for ( int i : nums ) { if ( minQ . size ( ) < k ) { minQ . add ( i ) ; continue ; } if ( i <= minQ . peek ( ) ) continue ; minQ . remove ( ) ; minQ . add ( i ) ; } return minQ . peek ( ) ; } } Runtime: 8 ms, faster than 46.78% of Java online submissions for

Leetcode 230 Kth Smallest Element in a BST

微笑、不失礼 提交于 2020-01-05 08:28:09
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up: What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine? 最先想到的思路就是中序遍历后找到结果数组中的第k-1项,为了节约时间可以每次都判断结果数组长度,达到k即返回最后一项。 def kth_smallest(root, k) stack,ans = [[false,root]], [] while not stack.empty? visit, node = stack.pop if node if visit ans << node.val return ans[-1] if ans.length == k else stack << [false,node.right]

可持久化数据结构题目泛做。

我只是一个虾纸丫 提交于 2019-12-17 18:15:18
个人理解: 每个新的线段树的一个结点保存的是1...位置 i中的数字在相应的区间上有几个。 然后我们用r-(l-1)得到的就是l...r上的中字在相应的区间中出现了几个。 题目1 POJ2104 题目大意:静态查询区间第K小值。 裸的可持久化线段树。 1 #include <cstdlib> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstdio> 5 #include <cstring> 6 7 using namespace std; 8 const int N = 100000 + 5; 9 10 struct SegTree { 11 int l, r, size; 12 }Node[N * 30]; 13 14 struct data { 15 int v, pos; 16 bool operator < (const data &k) const { 17 return v < k.v; 18 } 19 }a[N]; 20 21 int n, m, rank[N], l, r, k, root[N], tot; 22 23 void build(int &o, int l, int r) { 24 o = ++ tot; 25 Node[o].l = Node[o].r = Node[o].size

[LC] 215. Kth Largest Element in an Array

怎甘沉沦 提交于 2019-12-04 19:31:19
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1: Input: [3,2,1,5,6,4] and k = 2 Output: 5 Example 2: Input: [3,2,3,1,2,4,5,5,6] and k = 4 Output: 4 Note: You may assume k is always valid, 1 ≤ k ≤ array's length. Solution 1: Time: O(NlgN) class Solution { public int findKthLargest(int[] nums, int k) { Arrays.sort(nums); return nums[nums.length - k]; } } Solution 2: Time: O(Nlgk) class Solution { public int findKthLargest(int[] nums, int k) { if (nums == null || nums.length == 0) { return 0; }

Finding kth smallest number from n sorted arrays

匿名 (未验证) 提交于 2019-12-03 09:02:45
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: So, you have n sorted arrays (not necessarily of equal length), and you are to return the kth smallest element in the combined array (i.e the combined array formed by merging all the n sorted arrays) I have been trying it and its other variants for quite a while now, and till now I only feel comfortable in the case where there are two arrays of equal length, both sorted and one has to return the median of these two. This has logarithmic time complexity. After this I tried to generalize it to finding kth smallest among two sorted arrays. Here

Find kth smallest element in a binary search tree in Optimum way

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I need to find the kth smallest element in the binary search tree without using any static/global variable. How to achieve it efficiently? The solution that I have in my mind is doing the operation in O(n), the worst case since I am planning to do an inorder traversal of the entire tree. But deep down I feel that I am not using the BST property here. Is my assumptive solution correct or is there a better one available ? 回答1: Here's just an outline of the idea: In a BST, the left subtree of node T contains only elements smaller than the value