Skip to content

Commit bb4ced1

Browse files
Update algorithms/heap/topklargest/__init__.py
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 5af34d3 commit bb4ced1

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

algorithms/heap/topklargest/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ def kth_largest_sorting(nums: List[int], k: int) -> int:
7676
if not nums or k <= 0 or k > len(nums):
7777
return -1
7878

79-
# Sort the list in place which incurs a time complexity cost of O(n log(n)). Space complexity is O(n) due to using
80-
# an in-memory data structure to store the elements during sorting using timsort in Python
81-
nums.sort(reverse=True)
79+
# Sort the list which incurs a time complexity cost of O(n log(n)). Space complexity is O(n) due to creating
80+
# a new sorted list
81+
sorted_nums = sorted(nums, reverse=True)
8282
# Return the kth largest element
83-
return nums[k - 1]
83+
return sorted_nums[k - 1]

0 commit comments

Comments
 (0)