Rearrange nums so that non-negative values (>= 0) and negative values (< 0) alternate, starting with a non-negative one. Inside each group keep the original order. If one group runs out first, put the rest of the other group at the end, in order.
Return a new list.
Example 1
Input:
rearrange_by_sign([3, 1, -2, -5, 2, -4])
Output:
[3, -2, 1, -5, 2, -4]
Non-negative: 3, 1, 2. Negative: -2, -5, -4. Interleaved: 3, -2, 1, -5, 2, -4.
Example 2
Input:
rearrange_by_sign([1, 2, 3, -1])
Output:
[1, -1, 2, 3]
After 1, -1 the negatives run out, so 2, 3 follow.
0 <= len(nums) <= 3 * 10**5-10**9 <= nums[i] <= 10**9No results yet
Run your code to check it against the examples, or submit to run every test.