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.
public class Solution { public int findKthLargest(int[] nums, int k) { if (k < 1 || k > nums.length) { return 0; } int[] kHeap = new int[k]; for (int i = 0; i < k; i++) { heapInsert(kHeap, nums[i], i); } for (int i = k; i < nums.length; i++) { if (nums[i] > kHeap[0]) { kHeap[0] = nums[i]; heapify(kHeap, 0, k); } } return kHeap[0]; } private void heapify(int[] kHeap, int index, int heapSize) { // TODO Auto-generated method stub int left = index * 2 + 1; int right = index * 2 + 2; int little = index; while (left < heapSize) { if (kHeap[left] < kHeap[index]) { little = left; } if (right < heapSize && kHeap[right] < kHeap[little]) { little = right; } if (little != index) { swap(kHeap, little, index); } else { break; } index = little; left = index * 2 + 1; right = index * 2 + 2; } } private void heapInsert(int[] kHeap, int value, int index) { // TODO Auto-generated method stub kHeap[index] = value; while (index != 0) { int parent = (index - 1) / 2; if (kHeap[parent] > kHeap[index]) { swap(kHeap, parent, index); index = parent; } else { break; } } } private void swap(int[] arr, int i, int j) { // TODO Auto-generated method stub int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } }
相关推荐
题目2:Kth Largest Element in an Array 搜索算法题目 题目1:二分查找 题目2:搜索旋转排序数组 动态规划题目 题目1:斐波那契数列 题目2:最长公共子序列 贪心算法题目 题目1:分发饼干 题目2:跳跃游戏 回溯算法...
8 Kth Largest Element in an Array 35 9 Wildcard Matching 37 10 Regular Expression Matching in Java 39 11 Merge Intervals 43 12 Insert Interval 45 13 Two Sum 47 14 Two Sum II Input array is sorted 49 ...
java入门 java_leetcode题解之215_Kth_Largest_Element_in_an_Array
10. 深入理解堆和优先队列:在问题如Kth Largest Element in an Array(数组中第K大的元素)中会用到堆和优先队列的知识。 11. 栈与单调栈问题:例如Valid Parentheses(有效的括号)、Largest Rectangle in ...
python python_leetcode题解之215_Kth_Largest_Element_in_an_Array.py
第215题是“数组中的第K个最大元素”(Find the Kth Largest Element in an Array),这是一个常见的数据结构与算法问题,考察了排序、堆和快速选择等算法知识。本题解将详细阐述如何用Python解决这个问题。 首先,...
Array(方法1:堆 方法二:快速排序(推荐)) (面试题40:最小的k个数) LeetCode 347 Top K Frequent Elements(堆排序、桶排序) LintCode 532 Reverse Pairs(归并排序的应用)(面试题51:数组中的逆序对) ...
lru缓存leetcode 力码 大批 152-最大乘积子阵列,169-多数元素,189-...215-Kth Largest element in an Array(Heap Sort), HARD: 218-The Skyline Problem(Mergesort) ) 动态规划 HARD: 174-Dungeon Game, HARD: 188
kth largest element in an array . use heap, use quick select. maximal square. Use dynamic programming. use just O(n) space. The extra space equals matrix col length. majority element ii . 使用 "摩尔...
- Kth Largest Element in an Array:在未排序的数组中找到第k大的元素。 - Binary Search:二分查找法。 - First Position of Target:在有序数组中找到目标值的第一个位置。 - Search Insert Position:在排序数组...
421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...
例如,"Kth Largest Element in an Array"可以通过优先队列在O(n log k)的时间复杂度内找到答案。 五、Java特性的运用 Java的一些高级特性如泛型、枚举、接口、匿名内部类、Lambda表达式等也会在解题中发挥重要作用...
例如,“Kth Largest Element in an Array”可以使用大顶堆找到数组中第k大的元素。 6. **回溯与剪枝**:这类问题通常要求找到所有可能的解,如“N-Queens”问题,使用回溯法可以有效地找出所有可行的皇后布局。 7...
largest element from an unsorted array in linear time. (1) If the number of elements in an array is large .Divide the array into arrays each with 5 elements. n/5 arrays. (2) Compute Median of these 5 ...
in an array 3. Find the "Kth" max and min element of an array 4. Given an array which consists of only 0, 1 and 2. Sort the array without using any sorting algo 5. Move all the negative elements to ...
in an array *Find the "Kth" max and min element of an array *Given an array which consists of only 0, 1 and 2. Sort the array with *Move all the negative elements to one side of the array *Find the ...
largest element in array 4. Improvements in Java 8 - For HashMap if hashcode collision count is more than 8 in a row , then its internal structure is changed from linked list to tree 渐近分析 big- \...
- Kth Largest Element(第k个最大元素) - First Position of Target(目标值首次出现的位置) - Search Insert Position(搜索插入位置) - Search for a Range(搜索范围) - First Bad Version(第一个错误...
7. LeetCode第703题:"Kth Largest Element in a Stream"(数据流中的第K大元素):设计一个数据结构,找到数据流中的第K个最大元素。 8. LeetCode第910题:"XOR Queries of a Subarray"(子数组异或查询):对于...