GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/215.Kth Largest Element in an Array at master · geemo/leetcode

Expand file tree

/

215.Kth Largest Element in an Array

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

215.Kth Largest Element in an Array

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
README.md
README.md
 
 

README.md

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:

func findKthLargest(nums []int, k int) int {
  topK := make([]int, k)
  copy(topK, nums[:k])

  for i := k / 2 - 1; i >= 0; i-- {
    heapify(topK, i)
  }

  for i := k; i < len(nums); i++ {
    if topK[0] < nums[i] {
      topK[0] = nums[i]
      heapify(topK, 0)
    }
  }

  return topK[0]
}

func heapify(arr []int, root int) {
  l, r := root*2+1, root*2+2
  min := root
  size := len(arr)
  if l < size && arr[l] < arr[min] {
    min = l
  }

  if r < size && arr[r] < arr[min] {
    min = r
  }

  if min != root {
    arr[root], arr[min] = arr[min], arr[root]
    heapify(arr, min)
  }
}
You are visitor 01402622 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.