-
Notifications
You must be signed in to change notification settings - Fork 90
/
Interleaving Positive and Negative Numbers.py
48 lines (38 loc) · 1.32 KB
/
Interleaving Positive and Negative Numbers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers.
Have you met this question in a real interview? Yes
Example
Given [-1, -2, -3, 4, 5, 6], after re-range, it will be [-1, 5, -2, 4, -3, 6] or any other reasonable answer.
Note
You are not necessary to keep the original order of positive integers or negative integers.
Challenge
Do it in-place and without extra memory.
"""
__author__ = 'Daniel'
class Solution(object):
def rerange(self, A):
"""
Algorithm: Two Pointers
:type A: List[int]
:rtype: None, in-place
"""
n = len(A)
pos_cnt = len(filter(lambda x: x > 0, A))
# expecting positive
pos_expt = True if pos_cnt*2 > n else False
neg = 0 # next negative
pos = 0 # next positive
for i in xrange(n):
while neg < n and A[neg] > 0: neg += 1
while pos < n and A[pos] < 0: pos += 1
if pos_expt:
A[i], A[pos] = A[pos], A[i]
else:
A[i], A[neg] = A[neg], A[i]
if i == neg: neg += 1
if i == pos: pos += 1
pos_expt = not pos_expt
if __name__ == "__main__":
A = [-33, -19, 30, 26, 21, -9]
Solution().rerange(A)
assert A == [-33, 30, -19, 26, -9, 21]