[
sort
math
buckets
divide and conquer
]
Leetcode 0912 Sort an Array
Problem statement
https://leetcode.com/problems/sort-an-array/
Solution
In this problem you just need to sort nums and it is not even forbidden to use libraries. So we can just use sorted(nums).
Complexity
It is O(n log n) for time and O(n) for space
Code
class Solution:
def sortArray(self, nums):
return sorted(nums)
Remark
There are different alternative ways how you can solve this problem, implementing some methods from scratch:
- Merge sort, using divide and conquer idea
- Quick sort, similar idea, but less memory
- Bucket sort if numbers are not too big.