-
Notifications
You must be signed in to change notification settings - Fork 21
/
Interleaving Positive and Negative Numbers.java
executable file
·153 lines (136 loc) · 3.57 KB
/
Interleaving Positive and Negative Numbers.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
M
1525230897
tags: Two Pointers
给一串数组 有正负数. 重新排列, 让数组里面 正数 和 负数 相隔开. 原来的order无所谓
#### Two pointer
- 需要知道正负的位置, 所以排序 O(nlogN)
- 考虑: 正数多还是负数多的问题, 举栗子就看出来端倪了
- 然后Two Pointer, swap
- Time O(nlogn), space O(n)
#### extra space
- 用extra O(n) space, 把正负分成两个list
- 然后分别按照index填回去
- time O(n). space O(n)
- 但是就么有用到Two pointer了
```
/*
Given an array with positive and negative integers.
Re-range it to interleaving with positive and negative integers.
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.
Tags Expand
Two Pointers
*/
/*
Thoughts:
Sort the array and swap to correct order.
1. Negative # == positive #, swap start/end untill start == end
2. Negative # != positive #, if mid point is positive, end = end - 1; if mid point is negative, start = start + 1.
3. Then do the same swap based on start, end
O(nlogn) runtime
*/
public class Solution {
/*
* @param A: An integer array.
* @return: nothing
*/
public void rerange(int[] A) {
if (A == null || A.length <= 1) {
return;
}
Arrays.sort(A);
int start = 0;
int end = A.length - 1;
if (A.length % 2 != 0) {
if (A[(start + end) / 2] >= 0) {
end--;
} else {
start++;
}
}
while (start < end) {
int temp = A[start];
A[start] = A[end];
A[end] = temp;
start += 2;
end -= 2;
}
}
}
// Extra O(n) space, O(n) runtime
public class Solution {
/*
* @param A: An integer array.
* @return: nothing
*/
public void rerange(int[] A) {
if (A == null || A.length <= 1) {
return;
}
List<Integer> positive = new ArrayList<>();
List<Integer> negative = new ArrayList<>();
for (int num : A) {
if (num >= 0) {
positive.add(num);
} else {
negative.add(num);
}
}
int extraPositive = positive.size() > negative.size() ? 0 : 1;
for (int i = 0; i < A.length; i++) {
if (i % 2 == extraPositive) {
A[i] = positive.get(0);
positive.remove(0);
} else {
A[i] = negative.get(0);
negative.remove(0);
}
System.out.println(A[i]);
}
}
}
/*
Previous notes:
Thoughts:
Sort, so it becomes [-1,-2,-3,-4,4,5,6,7]
Two pointer start,end.
Every round, start +=2, end -= 2;
Note: have to find out how many negative/positive integers first.
*/
class Solution {
/**
* @param A: An integer array.
* @return: void
*/
public void rerange(int[] A) {
if (A == null || A.length == 0) {
return;
}
Arrays.sort(A);
int count = 0;
for (int num : A){
count += num >= 0 ? 1 : -1;
}
int start = 0;
int end = A.length - 1;
if (count < 0) {
start++;
} else if (count > 0){
end--;
}
while (start < end) {
if (A[start] < 0 && A[end] >= 0) {
int temp = A[start];
A[start] = A[end];
A[end] = temp;
}
start += 2;
end -= 2;
}
}
}
```