Error:
cannot solve it.
First I want to sort the num than check if the largest one is in the highest position, second largest is in the second highest position and so on. But it need O(nlogn) time.
The solution is use O(n), which is focus on the digit not the whole array:
- store the last appearance of each digit in the first pass
- check if current position is its corresponding largest number and replace it if not in the second pass.