Reputation: 141
I am looking at this challenge:
Given a sequence of integers, sorted by their absolute values, return the sequence sorted by their signed value. The following rules apply:
- You can traverse the sequence in only one direction
- You cannot randomly access the elements at any position: you have to start from the first element and traverse.
- you can choose any data structure to take the input of the sequence, on the condition that the data structure does not sort the sequence for you
How can I solve this with a O(n) time complexity?
Upvotes: 0
Views: 59
Reputation: 350310
Here is a possible algorithm using a stack:
Or with a deque:
Upvotes: 4