Skip to content

Instantly share code, notes, and snippets.

@1kohei1
Created May 16, 2021 18:16
Show Gist options
  • Save 1kohei1/08720147f193de0f1f0b85f53cecdaa6 to your computer and use it in GitHub Desktop.
Save 1kohei1/08720147f193de0f1f0b85f53cecdaa6 to your computer and use it in GitHub Desktop.
import heapq
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
heap = []
for n in nums:
heapq.heappush(heap, -n)
if len(heap) > k:
# list popは引数がない場合、最後の要素を削除する。その計算量はO(1)
# https://wiki.python.org/moin/TimeComplexity
heap.pop()
return heap[-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment