Boolean array reordering in O(1) space and O(n) time

前端 未结 5 721
渐次进展
渐次进展 2020-12-29 03:36

The problem is taken from Elements of Programming Interviews:

Given an array A of n objects with Boolean-valued keys, reorder the array so that objects that have the

5条回答
  •  旧巷少年郎
    2020-12-29 04:15

    Observe that 2k for fixed k is O(1), and 2n is O(n). Construct a second array, and copy elements from the source array to the target array, adding elements with key false at one end and key true at the other. you can scan the array once to find out where the boundary must be.

提交回复
热议问题