2色抖动
我有一个包含 0
和 1
混合的数组。我想重新排列数组的内容,以便数组中尽可能多的偶数位置包含 0
和奇数位置包含 1
,但受到以下约束: 0
和 1
保持不变。这意味着,如果 0
的数量超过 1
的数量,反之亦然,则重新排列的数组末尾将出现一个由 all-组成的块>0
或全1
。如何一次性完成此操作,就地修改数组?
例如:
Input: {0,1,1,0,1,0,1,0,1,1,1,0,0,1,0,1,1}
Output: {0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,1}
I have an array of containing a mixture of 0
s and 1
s. I want to rearrange the contents of the array so that as many even positions in the array contain 0
and odd positions contain 1
as possible, subject to the constraint that the number of 0
s and 1
s are unchanged. This means that if the number of 0
s exceeds the number of 1
s or vice versa then there will a block at the end of the rearranged array consisting of all-0
s or all-1
s. How can I do this in one pass, modifying the array in place?
For example:
Input: {0,1,1,0,1,0,1,0,1,1,1,0,0,1,0,1,1}
Output: {0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,1}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以为此使用标准的双色排序算法;只需编辑数组引用,将对数组前半部分的访问映射到实际数组中的偶数元素,并将对数组后半部分的访问映射到实际数组中的奇数元素(向后)。基本上,
a[i]
变为(假设size
是偶数):You can just use the standard two-color sort algorithm for this; just edit the array references to map accesses to the first half of the array to even elements in your actual array, and accesses to the second half of the array to odd elements in your actual array (backwards). Basically,
a[i]
becomes (assumingsize
is even):我不认为它可以一次性完成,除非“让它们留在原处”意味着“它们最终在哪里并不重要”。
这是我两次通过的尝试:)
I don't think it can be made in 1 pass, unless the "leave them where they are" means "it doesn't matter where they end up".
Here's my attempt with two passes :)
循环遍历数组,维护 3 个变量和数组的不变量:
pos
之前的所有内容都已排序。color
是应放置在pos
处的元素的颜色。pos
和next
之间的所有内容都具有相同的颜色。无论如何,它似乎有效。
Loop through the array, maintaining invariants for 3 variables and the array:
pos
has been sorted.color
is the color of the element that should be placed atpos
.pos
andnext
has the same color.Anyway, it seems to work.
这样就可以了。结果与建议的输出不同,但等于给定的规则(问题的文本不包含“排序”一词,只是最后你必须移动所有
0
你可以在偶数位置,1
你可以在奇数位置你不需要“压缩”它们)。 “压缩”有点复杂。我保留了一堆赔率和一堆需要移动的偶数元素,当我需要奇数/偶数时我会使用它们。这两个堆栈保留在输入数组中,因此没有额外的空间(除了两个堆栈的两个“头”,即两个额外的整数)。我认为最坏的情况是时间
O(1.5n)
(例如所有元素都是1
,一半元素被“放入”堆栈中,然后需要重置,因为没有空的空间),并且O(1)
表示空间。输出:
This will do it. The result is different from the proposed output, but is equal to the rules given (the text of the problem doesn't include the word "sort", only that at the end you have to move all the
0
you can in even positions and the1
you can in odd positions. You don't need to "compact" them). It's a little more complex to do it "compacting".I keep a stack of the odds and a stack of the even elements that need moving, and I use these when I need an odd/even number. The two stacks are keeped in the input array, so no extra space (except the two "heads" of the two stacks, that are two extra integers). I think the worst case is
O(1.5n)
for time (for example all the elements are1
, half of the elements are "put" in the stack and then need resetting because there wasn't an empty space), andO(1)
for space.Output:
这可以单次完成。
这是使用单通道的另一种解决方案。这个想法是保留两个索引
pos_0
和pos_1
,它们保存下一个0
或1
所在的位置被放入数组中。i
将用于遍历数组。This can be done in single pass.
Here's another solution that uses single pass. The idea is to keep two indexes
pos_0
andpos_1
which holds the location where the next0
or1
is to be placed in the array.i
will be used to traverse through the array.因为它只有 1 和 0,你只需计算它们数量的差异,排序就会非常容易:
很容易看出为什么它是正确的,所以我不会解释。时间复杂度:O( arr.length() ) 或 O(n)
since it's only 1 and 0 you can just count the difference of their amount and sorting will be very easy:
it's easy to see why it's correct so i won't explain. Time complexity: O( arr.length() ) or O(n)