Sorting
1 | class Solution { |
Remarks:
- Be careful of the boundaries.
- TC: $O(n\log n)$ (because we used
Array.sort
). This does not satisfy the requirement.
Swap Number
1 | class Solution { |
Remarks:
- TC: $O(n)$. Although there’s a
while
loop in eachfor
, all numbers will be only swapped at most once to be placed at their correct position.