zoukankan      html  css  js  c++  java
  • Kth Largest Element in an Array 解答

    Question

    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 1 -- PriorityQueue

    1. 将所有元素放入heap中

    2. 依次用poll()取出heap中最大值

     1 public class Solution {
     2     public int findKthLargest(int[] nums, int k) {
     3         if (nums == null || k > nums.length)
     4             return 0;
     5         int length = nums.length, index = length - k, result = 0;
     6         PriorityQueue<Integer> pq = new PriorityQueue<Integer>(length,
     7                                                             new Comparator<Integer>(){
     8                                                                 public int compare(Integer a, Integer b) {
     9                                                                     return (a - b);
    10                                                                 }
    11                                                             });
    12         for (int i = 0; i < length; i++)
    13             pq.add(nums[i]);
    14         while (index >= 0) {
    15             result = pq.poll();
    16             index--;
    17         }
    18         return result;
    19     }
    20 }

    Improvement

     1 public class Solution {
     2     public int findKthLargest(int[] nums, int k) {
     3         if (nums == null || k > nums.length)
     4             return 0;
     5         int length = nums.length, index = k, result = 0;
     6         PriorityQueue<Integer> pq = new PriorityQueue<Integer>(length, Collections.reverseOrder());
     7         for (int i = 0; i < length; i++)
     8             pq.add(nums[i]);
     9         while (index > 0) {
    10             result = pq.poll();
    11             index--;
    12         }
    13         return result;
    14     }
    15 }

    Solution 2 -- Quick Select

    Quick Sort in Java

    Average Time O(n)

     1 '''
     2 a: [3, 2, 5, 1], 2
     3 output: 2
     4 '''
     5 
     6 def swap(a, index1, index2):
     7     if index1 >= index2:
     8         return
     9     tmp = a[index1]
    10     a[index1] = a[index2]
    11     a[index2] = tmp
    12 
    13 def partition(a, start, end):
    14     ''' a[start:end]
    15         pivot: a[end]
    16         return: index of pivot
    17     '''
    18     pivot = a[end]
    19     cur_big = start - 1
    20     for i in range(start, end):
    21         if a[i] >= pivot:
    22             cur_big += 1
    23             swap(a, cur_big, i)
    24     cur_big += 1
    25     swap(a, cur_big, end)
    26     return cur_big
    27 
    28 
    29 def quick_select(a, k):
    30     k -= 1
    31     length = len(a)
    32     start = 0
    33     end = length - 1
    34     while start < end:
    35         index = partition(a, start, end)
    36         if index == k:
    37             return a[index]
    38         if index > k:
    39             # Note: end != index
    40             end = index - 1
    41         else:
    42             # Note: start != index
    43             start = index + 1
    44             k = k - index
    45     return -1
    46 
    47 a = [3, 2, 5, 1]
    48 k = 1
    49 print(quick_select(a, k))
  • 相关阅读:
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
    @RequestParam设置默认可以传空值
    python清空列表的方法
    Git分支管理的基本操作
    判断一个坐标点是否在不规则多边形内部的算法
    Flask 解析 Web 端 请求 数组
    SQL server 获取异常
    SQL server 大量数据导入和系统运行慢的问题
    SQL SERVER 跨服务器连接
    #pragma once
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/4827845.html
Copyright © 2011-2022 走看看