leetcode 215. Kth Largest Element in an Array | Java最短代码实现

本文介绍了一种使用快速排序思想的算法来找到未排序数组中第K大的元素,时间复杂度为O(n*logn)。通过实例演示算法实现,并提供了测试用例和运行结果。

Total Accepted: 46230 Total Submissions: 143069 Difficulty: Medium

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.

【抛砖】

该实现是按照快排的思想进行实现,时间复杂度为O(n*logn):

    public int findKthLargest(int[] nums, int k) {
        int leftBoundary = 0;
        int rightBouondary = nums.length - 1;
        while (leftBoundary < rightBouondary) {
            int temp = nums[leftBoundary];
            int left = leftBoundary;
            int right = rightBouondary;
            while (left < right) {
                while (right > left && nums[right] <= temp)
                    right--;
                nums[left] = nums[right];
                while (left < right && nums[left] > temp)
                    left++;
                nums[right] = nums[left];
            }
            nums[left] = temp;
            if (left == k - 1) return nums[left];
            else if (left < k - 1) leftBoundary = left + 1;
            else rightBouondary = left - 1;
        }
        return nums[leftBoundary];
    }
31 / 31 test cases passed. Runtime: 55 ms  Your runtime beats 27.71% of javasubmissions.
欢迎优化!
### LeetCode 215 - Kth Largest Element in an Array LeetCode215 题的目标是从一个未排序的数组中找到第 k 大的元素。可以采用多种方法来解决问题,其中常见的是基于堆的方法以及快速选择算法。 #### 方法一:小堆 (Min Heap) 通过维护大小为 `k` 的小堆,可以在遍历整个数组的过程中动态调整堆结构以保留当前大的前 `k` 个元素。终,堆顶即为所求的第 `k` 大元素[^1]。 以下是实现代码: ```python import heapq def findKthLargest(nums, k): min_heap = [] for num in nums: if len(min_heap) < k: heapq.heappush(min_heap, num) elif num > min_heap[0]: heapq.heapreplace(min_heap, num) return min_heap[0] ``` 这种方法的时间复杂度主要由堆操作决定,整体时间复杂度为 \( O(n \log k) \),空间复杂度为 \( O(k) \)[^1]。 #### 方法二:快速选择 (Quickselect Algorithm) 快速选择是一种类似于快速排序的选择算法,其核心思想是利用分治策略将目标范围逐步缩小至所需位置。具体来说,在每次划分后可以根据索引定位所需的第 `k` 大元素所在区域并继续递归处理。 下面是具体的 Python 实现: ```python import random def partition(nums, left, right, pivot_index): pivot_value = nums[pivot_index] nums[right], nums[pivot_index] = nums[pivot_index], nums[right] store_index = left for i in range(left, right): if nums[i] > pivot_value: nums[store_index], nums[i] = nums[i], nums[store_index] store_index += 1 nums[right], nums[store_index] = nums[store_index], nums[right] return store_index def select(nums, left, right, k_smallest): if left == right: return nums[left] pivot_index = random.randint(left, right) pivot_index = partition(nums, left, right, pivot_index) if k_smallest == pivot_index: return nums[k_smallest] elif k_smallest < pivot_index: return select(nums, left, pivot_index - 1, k_smallest) else: return select(nums, pivot_index + 1, right, k_smallest) def findKthLargest(nums, k): n = len(nums) target_idx = n - k return select(nums, 0, n - 1, target_idx) ``` 此方法平均情况下具有线性时间复杂度 \( O(n) \),但在坏情况下的时间复杂度可能退化到 \( O(n^2) \)。 --- ### 总结 对于本题而言,推荐优先考虑 **小堆** 或者 **快速选择** 算法作为解决方案。前者更加稳定且易于理解;后者则在理论上具备更优的时间性能表现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值