-
Notifications
You must be signed in to change notification settings - Fork 1
/
321 Create Maximum Number .cpp
67 lines (67 loc) · 2.2 KB
/
321 Create Maximum Number .cpp
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
class Solution {
public:
vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
auto get = [&] (vector<int> &nums, int m) {
int n = nums.size();
vector<int> res;
for (int i = 0; i < n && res.size() < m; i++) {
int mx = -1;
int mxj = nums.size() - (m - res.size() - 1);
int j;
for (j = i; j < mxj; j++) {
mx = max(mx, nums[j]);
}
for (j = i; j < mxj; j++) {
if (nums[j] == mx) {
break;
}
}
res.push_back(mx);
i = j;
}
return res;
};
auto solve = [&] (vector<int> &a, vector<int> &b) {
int n = a.size(), m = b.size();
int i = 0, j = 0;
vector<int> res;
while (i < n || j < m) {
if (i < n && j < m) {
int k;
for (k = 0; i + k < n && j + k < m; k++) {
if (a[i + k] != b[j + k]) {
break;
}
}
if (i + k == n) {
res.push_back(b[j++]);
} else if (j + k == m) {
res.push_back(a[i++]);
} else {
if (a[i + k] > b[j + k]) {
res.push_back(a[i++]);
} else {
res.push_back(b[j++]);
}
}
} else if (i < n) {
res.push_back(a[i++]);
} else if (j < m) {
res.push_back(b[j++]);
}
}
return res;
};
vector<int> ans;
for (int l1 = 0; l1 <= nums1.size(); l1++) {
int l2 = k - l1;
if (0 <= l2 && l2 <= nums2.size()) {
auto v1 = get(nums1, l1);
auto v2 = get(nums2, l2);
auto res = solve(v1, v2);
ans = max(ans, res);
}
}
return ans;
}
};