Created
May 16, 2021 18:16
-
-
Save 1kohei1/08720147f193de0f1f0b85f53cecdaa6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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