Given an int array of positive and negative numbers, rearrange it in O(1) extra space such that all positive numbers are on the left and all negative numbers are on the right, and the relative order of positive numbers, and the relative order of the negative numbers are the same as in the input. Can it be done in O(n)?
【在 l***i 的大作中提到】 : Given an int array of positive and negative numbers, rearrange it in O(1) : extra space such that all positive numbers are on the left and all negative : numbers are on the right, and the relative order of positive numbers, and : the relative order of the negative numbers are the same as in the input. : Can it be done in O(n)?
【在 l***i 的大作中提到】 : Given an int array of positive and negative numbers, rearrange it in O(1) : extra space such that all positive numbers are on the left and all negative : numbers are on the right, and the relative order of positive numbers, and : the relative order of the negative numbers are the same as in the input. : Can it be done in O(n)?
【在 l***i 的大作中提到】 : Divide and conquer can give you nlogn. But I don't know if O(n) is possible.
y*o
24 楼
狮子会有的,莉香也会有的
【在 S*****i 的大作中提到】 : : 我是因为东京爱情故事
l*i
25 楼
split into two halves, solve each half recursively, then do some swaps to get the positive subarray from right to left and the negative subarray from right to left.
【在 l***i 的大作中提到】 : split into two halves, solve each half recursively, then do some swaps to : get the positive subarray from right to left and the negative subarray from : right to left.
【在 r**u 的大作中提到】 : 我觉得不对,in-place的话,每个element移动到对应的位置都是要O(n)步,无论用啥 : 法子。所以overall复杂度还是O(n^2)。 : : from
a*u
34 楼
听着象母爱啊
【在 y********o 的大作中提到】 : 狮子女都是光芒万丈,母仪天下,百鸟朝凤!
c*y
35 楼
好像不行,你试一个例子。
to from
【在 l***i 的大作中提到】 : split into two halves, solve each half recursively, then do some swaps to : get the positive subarray from right to left and the negative subarray from : right to left.