-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolution.c
More file actions
81 lines (64 loc) · 2.43 KB
/
solution.c
File metadata and controls
81 lines (64 loc) · 2.43 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int count_le(const int* arr, int size, int value) {
int low = 0;
int high = size;
int ans_idx = 0;
while (low < high) {
int mid = low + (high - low) / 2;
if (arr[mid] <= value) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
int solve(int n, const int* a, int m, const int* b) {
int* zeros_indices_a = (int*)malloc(n * sizeof(int));
int* ones_indices_a = (int*)malloc(n * sizeof(int));
int zeros_count_a = 0;
int ones_count_a = 0;
for (int i = 0; i < n; ++i) {
if (a[i] == 0) {
zeros_indices_a[zeros_count_a++] = i;
} else {
ones_indices_a[ones_count_a++] = i;
}
}
int* zeros_indices_b = (int*)malloc(m * sizeof(int));
int* ones_indices_b = (int*)malloc(m * sizeof(int));
int zeros_count_b = 0;
int ones_count_b = 0;
for (int i = 0; i < m; ++i) {
if (b[i] == 0) {
zeros_indices_b[zeros_count_b++] = i;
} else {
ones_indices_b[ones_count_b++] = i;
}
}
int max_len = 0;
int k_zeros = 0;
int available_ones_a = ones_count_a;
int available_ones_b = ones_count_b;
int k_ones = (available_ones_a < available_ones_b) ? available_ones_a : available_ones_b; // std::min
max_len = (max_len > (k_zeros + k_ones)) ? max_len : (k_zeros + k_ones);
int max_possible_zeros = (zeros_count_a < zeros_count_b) ? zeros_count_a : zeros_count_b; // std::min
for (k_zeros = 1; k_zeros <= max_possible_zeros; ++k_zeros) {
int last_zero_idx_a = zeros_indices_a[k_zeros - 1];
int last_zero_idx_b = zeros_indices_b[k_zeros - 1];
int idx_of_first_one_after_last_zero_a = count_le(ones_indices_a, ones_count_a, last_zero_idx_a);
available_ones_a = ones_count_a - idx_of_first_one_after_last_zero_a;
int idx_of_first_one_after_last_zero_b = count_le(ones_indices_b, ones_count_b, last_zero_idx_b);
available_ones_b = ones_count_b - idx_of_first_one_after_last_zero_b;
k_ones = (available_ones_a < available_ones_b) ? available_ones_a : available_ones_b; // std::min
int current_len = k_zeros + k_ones;
max_len = (max_len > current_len) ? max_len : current_len;
}
free(zeros_indices_a);
free(ones_indices_a);
free(zeros_indices_b);
free(ones_indices_b);
return max_len;
}