Skip to content

Commit ea0faa4

Browse files
committed
leetcode.com 215. Kth Largest Element in an Array
문제 링크: https://leetcode.com/problems/kth-largest-element-in-an-array
1 parent a9ae936 commit ea0faa4

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import sys
2+
from typing import List
3+
import heapq
4+
5+
6+
class Solution:
7+
def findKthLargest(self, nums: List[int], k: int) -> int:
8+
heap = []
9+
answer = sys.maxsize
10+
11+
for n in nums:
12+
heapq.heappush(heap, -n)
13+
14+
for _ in range(k):
15+
answer = heapq.heappop(heap)
16+
17+
return -answer
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from unittest import TestCase
2+
from main import Solution
3+
4+
5+
class TestSolution(TestCase):
6+
def test1_find_kth_largest(self):
7+
sln = Solution()
8+
self.assertEqual(5, sln.findKthLargest([3, 2, 1, 5, 6, 4], 2))
9+
10+
def test2_find_kth_largest(self):
11+
sln = Solution()
12+
self.assertEqual(4, sln.findKthLargest([3, 2, 3, 1, 2, 4, 5, 5, 6], 4))
13+
14+
def test5_find_kth_largest(self):
15+
sln = Solution()
16+
self.assertEqual(99, sln.findKthLargest([99, 99], 1))

0 commit comments

Comments
 (0)