Skip to content

Commit 61f2326

Browse files
authored
feat: add solutions to lc problem: No.3072 (doocs#2408)
No.3072.Distribute Elements Into Two Arrays II
1 parent e7a63f6 commit 61f2326

File tree

8 files changed

+959
-4
lines changed

8 files changed

+959
-4
lines changed

solution/3000-3099/3072.Distribute Elements Into Two Arrays II/README.md

Lines changed: 324 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,345 @@
7272

7373
## 解法
7474

75-
### 方法一
75+
### 方法一:离散化 + 树状数组
76+
77+
我们可以用两个树状数组 `tree1``tree2` 分别维护 `arr1``arr2` 中小于等于某个数的元素个数。每一次,我们在树状数组中查询小于等于当前数的元素个数,那么大于当前数的元素个数就是当前数组的长度减去查询的结果。然后我们就可以根据这个差值来决定将当前数加入到哪个数组中。
78+
79+
由于题目中给出的数的范围很大,所以我们需要对这些数进行离散化。我们可以将这些数排序后去重,然后用二分查找来找到每个数在排序后的数组中的位置。
80+
81+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `nums` 的长度。
7682

7783
<!-- tabs:start -->
7884

7985
```python
86+
class BinaryIndexedTree:
87+
__slots__ = "n", "c"
88+
89+
def __init__(self, n: int):
90+
self.n = n
91+
self.c = [0] * (n + 1)
92+
93+
def update(self, x: int, delta: int) -> None:
94+
while x <= self.n:
95+
self.c[x] += delta
96+
x += x & -x
97+
98+
def query(self, x: int) -> int:
99+
s = 0
100+
while x:
101+
s += self.c[x]
102+
x -= x & -x
103+
return s
104+
105+
106+
class Solution:
107+
def resultArray(self, nums: List[int]) -> List[int]:
108+
st = sorted(set(nums))
109+
m = len(st)
110+
tree1 = BinaryIndexedTree(m + 1)
111+
tree2 = BinaryIndexedTree(m + 1)
112+
tree1.update(bisect_left(st, nums[0]) + 1, 1)
113+
tree2.update(bisect_left(st, nums[1]) + 1, 1)
114+
arr1 = [nums[0]]
115+
arr2 = [nums[1]]
116+
for x in nums[2:]:
117+
i = bisect_left(st, x) + 1
118+
a = len(arr1) - tree1.query(i)
119+
b = len(arr2) - tree2.query(i)
120+
if a > b:
121+
arr1.append(x)
122+
tree1.update(i, 1)
123+
elif a < b:
124+
arr2.append(x)
125+
tree2.update(i, 1)
126+
elif len(arr1) <= len(arr2):
127+
arr1.append(x)
128+
tree1.update(i, 1)
129+
else:
130+
arr2.append(x)
131+
tree2.update(i, 1)
132+
return arr1 + arr2
133+
```
80134

135+
```python
136+
from sortedcontainers import SortedList
137+
138+
139+
class Solution:
140+
def resultArray(self, nums: List[int]) -> List[int]:
141+
arr1 = [nums[0]]
142+
arr2 = [nums[1]]
143+
sl1 = SortedList(arr1)
144+
sl2 = SortedList(arr2)
145+
for x in nums[2:]:
146+
i = sl1.bisect_right(x)
147+
j = sl2.bisect_right(x)
148+
if len(sl1) - i > len(sl2) - j:
149+
arr1.append(x)
150+
sl1.add(x)
151+
elif len(sl1) - i < len(sl2) - j:
152+
arr2.append(x)
153+
sl2.add(x)
154+
elif len(sl1) <= len(sl2):
155+
arr1.append(x)
156+
sl1.add(x)
157+
else:
158+
arr2.append(x)
159+
sl2.add(x)
160+
return arr1 + arr2
81161
```
82162

83163
```java
84-
164+
class BinaryIndexedTree {
165+
private int n;
166+
private int[] c;
167+
168+
public BinaryIndexedTree(int n) {
169+
this.n = n;
170+
this.c = new int[n + 1];
171+
}
172+
173+
public void update(int x, int delta) {
174+
for (; x <= n; x += x & -x) {
175+
c[x] += delta;
176+
}
177+
}
178+
179+
public int query(int x) {
180+
int s = 0;
181+
for (; x > 0; x -= x & -x) {
182+
s += c[x];
183+
}
184+
return s;
185+
}
186+
}
187+
188+
class Solution {
189+
public int[] resultArray(int[] nums) {
190+
int[] st = nums.clone();
191+
Arrays.sort(st);
192+
int n = st.length;
193+
BinaryIndexedTree tree1 = new BinaryIndexedTree(n + 1);
194+
BinaryIndexedTree tree2 = new BinaryIndexedTree(n + 1);
195+
tree1.update(Arrays.binarySearch(st, nums[0]) + 1, 1);
196+
tree2.update(Arrays.binarySearch(st, nums[1]) + 1, 1);
197+
int[] arr1 = new int[n];
198+
int[] arr2 = new int[n];
199+
arr1[0] = nums[0];
200+
arr2[0] = nums[1];
201+
int i = 1, j = 1;
202+
for (int k = 2; k < n; ++k) {
203+
int x = Arrays.binarySearch(st, nums[k]) + 1;
204+
int a = i - tree1.query(x);
205+
int b = j - tree2.query(x);
206+
if (a > b) {
207+
arr1[i++] = nums[k];
208+
tree1.update(x, 1);
209+
} else if (a < b) {
210+
arr2[j++] = nums[k];
211+
tree2.update(x, 1);
212+
} else if (i <= j) {
213+
arr1[i++] = nums[k];
214+
tree1.update(x, 1);
215+
} else {
216+
arr2[j++] = nums[k];
217+
tree2.update(x, 1);
218+
}
219+
}
220+
for (int k = 0; k < j; ++k) {
221+
arr1[i++] = arr2[k];
222+
}
223+
return arr1;
224+
}
225+
}
85226
```
86227

87228
```cpp
88-
229+
class BinaryIndexedTree {
230+
private:
231+
int n;
232+
vector<int> c;
233+
234+
public:
235+
BinaryIndexedTree(int n)
236+
: n(n)
237+
, c(n + 1) {}
238+
239+
void update(int x, int delta) {
240+
for (; x <= n; x += x & -x) {
241+
c[x] += delta;
242+
}
243+
}
244+
245+
int query(int x) {
246+
int s = 0;
247+
for (; x > 0; x -= x & -x) {
248+
s += c[x];
249+
}
250+
return s;
251+
}
252+
};
253+
254+
class Solution {
255+
public:
256+
vector<int> resultArray(vector<int>& nums) {
257+
vector<int> st = nums;
258+
sort(st.begin(), st.end());
259+
int n = st.size();
260+
BinaryIndexedTree tree1(n + 1);
261+
BinaryIndexedTree tree2(n + 1);
262+
tree1.update(distance(st.begin(), lower_bound(st.begin(), st.end(), nums[0])) + 1, 1);
263+
tree2.update(distance(st.begin(), lower_bound(st.begin(), st.end(), nums[1])) + 1, 1);
264+
vector<int> arr1 = {nums[0]};
265+
vector<int> arr2 = {nums[1]};
266+
for (int k = 2; k < n; ++k) {
267+
int x = distance(st.begin(), lower_bound(st.begin(), st.end(), nums[k])) + 1;
268+
int a = arr1.size() - tree1.query(x);
269+
int b = arr2.size() - tree2.query(x);
270+
if (a > b) {
271+
arr1.push_back(nums[k]);
272+
tree1.update(x, 1);
273+
} else if (a < b) {
274+
arr2.push_back(nums[k]);
275+
tree2.update(x, 1);
276+
} else if (arr1.size() <= arr2.size()) {
277+
arr1.push_back(nums[k]);
278+
tree1.update(x, 1);
279+
} else {
280+
arr2.push_back(nums[k]);
281+
tree2.update(x, 1);
282+
}
283+
}
284+
arr1.insert(arr1.end(), arr2.begin(), arr2.end());
285+
return arr1;
286+
}
287+
};
89288
```
90289
91290
```go
291+
type BinaryIndexedTree struct {
292+
n int
293+
c []int
294+
}
295+
296+
func NewBinaryIndexedTree(n int) *BinaryIndexedTree {
297+
return &BinaryIndexedTree{n: n, c: make([]int, n+1)}
298+
}
299+
300+
func (bit *BinaryIndexedTree) update(x, delta int) {
301+
for ; x <= bit.n; x += x & -x {
302+
bit.c[x] += delta
303+
}
304+
}
305+
306+
func (bit *BinaryIndexedTree) query(x int) int {
307+
s := 0
308+
for ; x > 0; x -= x & -x {
309+
s += bit.c[x]
310+
}
311+
return s
312+
}
313+
314+
func resultArray(nums []int) []int {
315+
st := make([]int, len(nums))
316+
copy(st, nums)
317+
sort.Ints(st)
318+
n := len(st)
319+
tree1 := NewBinaryIndexedTree(n + 1)
320+
tree2 := NewBinaryIndexedTree(n + 1)
321+
tree1.update(sort.SearchInts(st, nums[0])+1, 1)
322+
tree2.update(sort.SearchInts(st, nums[1])+1, 1)
323+
arr1 := []int{nums[0]}
324+
arr2 := []int{nums[1]}
325+
for _, x := range nums[2:] {
326+
i := sort.SearchInts(st, x) + 1
327+
a := len(arr1) - tree1.query(i)
328+
b := len(arr2) - tree2.query(i)
329+
if a > b {
330+
arr1 = append(arr1, x)
331+
tree1.update(i, 1)
332+
} else if a < b {
333+
arr2 = append(arr2, x)
334+
tree2.update(i, 1)
335+
} else if len(arr1) <= len(arr2) {
336+
arr1 = append(arr1, x)
337+
tree1.update(i, 1)
338+
} else {
339+
arr2 = append(arr2, x)
340+
tree2.update(i, 1)
341+
}
342+
}
343+
arr1 = append(arr1, arr2...)
344+
return arr1
345+
}
346+
```
92347

348+
```ts
349+
class BinaryIndexedTree {
350+
private n: number;
351+
private c: number[];
352+
353+
constructor(n: number) {
354+
this.n = n;
355+
this.c = Array(n + 1).fill(0);
356+
}
357+
358+
update(x: number, delta: number): void {
359+
for (; x <= this.n; x += x & -x) {
360+
this.c[x] += delta;
361+
}
362+
}
363+
364+
query(x: number): number {
365+
let s = 0;
366+
for (; x > 0; x -= x & -x) {
367+
s += this.c[x];
368+
}
369+
return s;
370+
}
371+
}
372+
373+
function resultArray(nums: number[]): number[] {
374+
const st: number[] = nums.slice().sort((a, b) => a - b);
375+
const n: number = st.length;
376+
const search = (x: number): number => {
377+
let [l, r] = [0, n];
378+
while (l < r) {
379+
const mid = (l + r) >> 1;
380+
if (st[mid] >= x) {
381+
r = mid;
382+
} else {
383+
l = mid + 1;
384+
}
385+
}
386+
return l;
387+
};
388+
const tree1: BinaryIndexedTree = new BinaryIndexedTree(n + 1);
389+
const tree2: BinaryIndexedTree = new BinaryIndexedTree(n + 1);
390+
tree1.update(search(nums[0]) + 1, 1);
391+
tree2.update(search(nums[1]) + 1, 1);
392+
const arr1: number[] = [nums[0]];
393+
const arr2: number[] = [nums[1]];
394+
for (const x of nums.slice(2)) {
395+
const i: number = search(x) + 1;
396+
const a: number = arr1.length - tree1.query(i);
397+
const b: number = arr2.length - tree2.query(i);
398+
if (a > b) {
399+
arr1.push(x);
400+
tree1.update(i, 1);
401+
} else if (a < b) {
402+
arr2.push(x);
403+
tree2.update(i, 1);
404+
} else if (arr1.length <= arr2.length) {
405+
arr1.push(x);
406+
tree1.update(i, 1);
407+
} else {
408+
arr2.push(x);
409+
tree2.update(i, 1);
410+
}
411+
}
412+
return arr1.concat(arr2);
413+
}
93414
```
94415

95416
<!-- tabs:end -->

0 commit comments

Comments
 (0)