We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5af34d3 commit bb4ced1Copy full SHA for bb4ced1
algorithms/heap/topklargest/__init__.py
@@ -76,8 +76,8 @@ def kth_largest_sorting(nums: List[int], k: int) -> int:
76
if not nums or k <= 0 or k > len(nums):
77
return -1
78
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)
+ # Sort the list which incurs a time complexity cost of O(n log(n)). Space complexity is O(n) due to creating
+ # a new sorted list
+ sorted_nums = sorted(nums, reverse=True)
82
# Return the kth largest element
83
- return nums[k - 1]
+ return sorted_nums[k - 1]
0 commit comments