LeetCode-Notes
  • Introduction
  • Records of Practice
  • 关于Github 不支持密码问题
  • 面试题
    • 搜索广告
    • 多模态大模型
    • 刷题记录
  • 算法代码实现
  • Python
    • Python 笔记
  • Spark
    • PySpark
    • Spark Issues
    • Spark调优笔记
  • FeatureEngineering
    • Feature Cleaning
    • Feature Selection
    • Feature Transformation
    • Feature Crossing
  • Recommendation Algorithm
    • Recall-and-PreRank
      • Non-Negative Matrix Fatorization(NMF)
      • Fatorization Machine(FM)
      • User-base/Item-base实现
      • 多路召回实现
    • Ranking
      • NeuralFM
      • DeepFM
      • Deep&Cross network (DCN)
    • DeepLearning-Basic
      • Attention
      • Dropout
      • Batch Norm
  • Machine Learning
    • XGBoost
    • Cross Entropy Loss
    • Other models
  • Graph Neural Network
    • GNN-1-Basic
  • Big Data
    • Reservoir Sampling
  • SQL
    • SQL and PySpark functions
    • Query Film Infomation
    • Create, Insert and Alter Actor Table
    • Manage Employment Data
    • Manage Employment Data -2
  • DataStructure
    • Searching
      • Find power
      • 2 Sum All Pair II
      • Two Sum
      • Search in Rotate Array
      • Search In Shifted Sorted Array II
      • Search in 2D array
      • Three Sum with duplicated values
      • Median of Two Sorted Arrays
    • Array
      • Longest Consecutive Subarray
      • Merge Two Array in-place
      • Trapping water
      • Rotate matrix
    • Sorting
      • Merge intervals
      • 排序
      • 最小的k个数
      • Find TopK largest- QuickSelect快速选择 method
      • MergeSort Linkedlist
      • 第K大元素
    • LinkedList
      • Reverse LinkedList I
      • Reverse K-group linked list
      • Detect Start of Cycle
      • HasCycle
      • DetectCycle II
      • 链表的共同节点
      • 链表中倒数第k个节点
      • 删除链表倒数第k个节点
      • 合并两个链表
      • 在排序数组中查找元素的第一个和最后一个位置
      • 删除链表里面重复的元素-1
    • Tree
      • Find Tree height (general iteration method)
      • Check BST and Check CompleteTree
      • ZigZag Order traversal
      • Binary Tree diameter I
      • Maximum Path Sum Binary Tree
      • Maximum Path Sum Binary Tree II
      • Binary Tree Path Sum To Target III
      • Tree diameter 树的直径II
      • Tree ReConstruction
      • Check if B is Subtree of A
      • The Kth smallest in Binary Search Tree
      • 打印Tree的右视图
      • 二叉搜索树的后序遍历序列
      • 重建二叉树
      • 判断二叉树是否对称
      • Path Sum to Target in Binary Tree
      • Tree-PreOrder-InOrder-PostOrder
    • Heap&Queue
      • Top-K smallest
      • 滑动窗口最大值
      • Find the K-Largest
    • 合并k个已排序的链表
    • String
      • Reverse String
      • 最长不含重复字符的子字符串
      • 最长回文串
      • 最长回文子序列-DP
    • DFS/BFS
      • Number of island
      • Number of Provinces
      • All Permutations of Subsets without duplication
      • All Permutations of Subsets with duplication
      • Combinations Of Coins
      • All Subset I (without fixing size of subset, without order, without duplication)
      • All Subset of K size without duplication II
      • All Subset of K size III (with duplication without considering order)
      • All Permutation II (with duplication and consider order)
      • Factor Combination-质数分解
    • DynamicProgramming
      • DP-解题过程
      • Find Continuous Sequence Sum to Target
      • 1800. Maximum Ascending Subarray Sum
      • NC91 最长上升子序列
      • 查找string的编码方式个数
      • Maximum Product
      • Longest Common Substring
      • Longest Common Substring-II
      • minEditCost
      • Backpack I
      • Array Hopper I
      • Minimum distance between strings
      • 最大正方形
  • Big Data Algorithms
    • Big Data Processing Algorithms
      • Reservior Sampling
      • Shuffle
      • MapReduce
      • Bloom Filter
      • BitMap
      • Heap For Big Data
Powered by GitBook
On this page
  • 1. Link
  • 2. 题目描述
  • 输入
  • 返回值
  • 3. 思路
  • 4.Code

Was this helpful?

  1. DataStructure
  2. Sorting

Find TopK largest- QuickSelect快速选择 method

Medium; quickselect; 腾讯;

Previous最小的k个数NextMergeSort Linkedlist

Last updated 4 years ago

Was this helpful?

1. Link

2. 题目描述

有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数。

给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在。示例1

输入

复制

[1,3,5,2,2],5,3

返回值

复制

2

3. 思路

  1. 像quicksort 一样先把pivot选出来,然后partition使pivot左边的数字大于pivot,右边的小于pivot (这里是从大到小排)

  2. 把pivot的position和 K-1对比(因为index是从0开始的),如果pivot index > K-1, 那么end = pivot -1. 如果pivot index < K-1, 那么start = pivot +1.如果一样,那就返回pivot对应的element

  3. Time Complexity: worst case O(n^2), average case O(nlogn). Space: (1) 没有用recursion

4.Code

# -*- coding:utf-8 -*-

class Solution:
    def findKth(self, a, n, K):
        # write code here
        # quick select idea:
        # quick sort:
        # 1. pick pivot and move element  pivot to left to pivot, element > pivot, right ot pivot
        # 2. recursion step 1 to go throught the left subarray, right subarray
        # 3. if no longer partition, return 
        #
        # quicksort:
        # In partition part, check if K position in left or right subarray
        # then only consider that subarray include K and recursion on this subarray
        # until subarray contains one element, then that is K largest element
        #
        #base case: if K >n: return None, a == None, reutrn None
        # Time: O(n^2) if array is almost sorted and recursion becomes list. O(nlogn) if not sorted
        # SPace  O(1) work in-place without recursion
        #
        #
        if not a or n< K:
            return None
        return self.quickselect(a, n, 0, n-1,K)
    
    def quickselect(self, a, n, start, end, K):
        
        while start <= end-1:
            pivot =  self.partition(a, start, end)
            if K-1  <pivot:
                end = pivot-1
            elif K-1 >pivot:
                start = pivot+1
            else:
                return a[pivot]
        return a[end]
            
    def partition(self, a, start, end):
        if start >= end:
            return end
        pivot_idx = end
        fast_pt, stored_idx = start, start
        while fast_pt < end:
            if a[fast_pt] >= a[pivot_idx]:
                a[fast_pt], a[stored_idx] = a[stored_idx], a[fast_pt]
                stored_idx += 1
            fast_pt += 1
        a[pivot_idx] , a[stored_idx] = a[stored_idx], a[pivot_idx] 
        l_s = start
        l_e = stored_idx -1
        r_s = stored_idx +1
        r_e = end
        return stored_idx

用min-heap 找TopK largest 的方法

class Solution:
    def findKth(self, a, n, K):
        # method2: heap sort
        # 1. build a min-heap using first K element in array
        # 2. use min-heap to pop the minimum element repeatedly until
        #   the heap contain only K leement. Then the last element as top k largest
        #
        #
        #
        #
        import heapq
        if K >n and not a:
            return None
        heap = [a[i] for i in range(K)]
        heapq.heapify(heap)
        pt = K
        while pt < n:
            
            heapq.heappush(heap, a[pt])
            heapq.heappop(heap)
            pt += 1
        
        # return the minimum value in the K largest values of array
        # that is TopK largest 
        return heapq.heappop(heap)
https://www.nowcoder.com/practice/e016ad9b7f0b45048c58a9f27ba618bf?tpId=117&tqId=37791&companyId=138&rp=1&ru=%2Fcompany%2Fhome%2Fcode%2F138&qru=%2Fta%2Fjob-code-high%2Fquestion-ranking&tab=answerKey