solution.md 8.0 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
# 拼接最大数
<p>给定长度分别为&nbsp;<code>m</code>&nbsp;&nbsp;<code>n</code>&nbsp;的两个数组,其元素由&nbsp;<code>0-9</code>&nbsp;构成,表示两个自然数各位上的数字。现在从这两个数组中选出 <code>k (k &lt;= m + n)</code>&nbsp;个数字拼接成一个新的数,要求从同一个数组中取出的数字保持其在原数组中的相对顺序。</p>

<p>求满足该条件的最大数。结果返回一个表示该最大数的长度为&nbsp;<code>k</code>&nbsp;的数组。</p>

<p><strong>说明: </strong>请尽可能地优化你算法的时间和空间复杂度。</p>

<p><strong>示例&nbsp;1:</strong></p>

<pre><strong>输入:</strong>
nums1 = <code>[3, 4, 6, 5]</code>
nums2 = <code>[9, 1, 2, 5, 8, 3]</code>
k = <code>5</code>
<strong>输出:</strong>
<code>[9, 8, 6, 5, 3]</code></pre>

<p><strong>示例 2:</strong></p>

<pre><strong>输入:</strong>
nums1 = <code>[6, 7]</code>
nums2 = <code>[6, 0, 4]</code>
k = <code>5</code>
<strong>输出:</strong>
<code>[6, 7, 6, 0, 4]</code></pre>

<p><strong>示例 3:</strong></p>

<pre><strong>输入:</strong>
nums1 = <code>[3, 9]</code>
nums2 = <code>[8, 9]</code>
k = <code>3</code>
<strong>输出:</strong>
<code>[9, 8, 9]</code></pre>

<p>以下错误的选项是?</p>

## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
    Solution sol;
    vector<int> nums1 = {3, 4, 6, 5};
    vector<int> nums2 = {9, 1, 2, 5, 8, 3};
    int k = 5;
    vector<int> res = sol.maxNumber(nums1, nums2, k);
    for (auto i : res)
        cout << i << " ";

    return 0;
}
```

## 答案
```cpp
class Solution
{
public:
    struct node
    {
        int val;
        int vec;
        int pos;
    };
    static bool cmp(node a, node b)
    {
        return a.val > b.val;
    }
    vector<int> maxNumber(vector<int> &nums1, vector<int> &nums2, int k)
    {
        int n = nums1.size();
        int m = nums2.size();
        struct node all[m + n];
        vector<int> ans;
        for (int i = 0; i < n; i++)
        {
            all[i].vec = 1;
            all[i].val = nums1[i];
            all[i].pos = i;
        }
        for (int i = 0; i < m; i++)
        {
            all[i + n].vec = 2;
            all[i + n].val = nums2[i];
            all[i + n].pos = i;
        }
        sort(all, all + m + n, cmp);
        int now1 = 0;
        int now2 = 0;
        int st = 0;
        while (st != n + 1)
        {
            for (int i = 0; i < n + m; i++)
            {
                if (all[i].pos >= now1 && n - all[i].pos + m - now2 >= k - st)
                {
                    ans.push_back(all[i].val);
                    now1 = all[i].pos + 1;
                    st++;
                    break;
                }
            }
        }
        return ans;
    }
};
```
## 选项

### A
```cpp
class Solution
{
public:
    vector<int> getNumsFromVector(vector<int> &nums, int k)
    {
        int n = nums.size();
        if (n == 0)
            return {};
        vector<int> stack;
        stack.push_back(nums[0]);
        for (int i = 1; i < n; ++i)
        {

            while (!stack.empty() && nums[i] > stack.back() && (n - i > k - (int)stack.size()))
            {
                stack.pop_back();
            }
            stack.push_back(nums[i]);
        }

        if (stack.size() > k)
        {
            vector<int> res(stack.begin(), stack.begin() + k);
            return res;
        }
        return stack;
    }

    int maxNumsVector(vector<int> &nums1, int index1, vector<int> &nums2, int index2)
    {
        int m = nums1.size(), n = nums2.size();
        if (m == 0)
            return 2;
        if (n == 0)
            return 1;
        int p1 = index1, p2 = index2;
        for (; p1 < m && p2 < n; ++p1, ++p2)
        {
            if (nums1[p1] < nums2[p2])
            {
                return 2;
            }
            if (nums1[p1] > nums2[p2])
            {
                return 1;
            }
        }
        if (p1 < m)
        {
            return 1;
        }
        else
        {
            return 2;
        }
    }
    vector<int> merge(vector<int> &nums1, vector<int> &nums2)
    {
        int m = nums1.size(), n = nums2.size();
        if (m == 0)
            return nums2;
        if (n == 0)
            return nums1;
        vector<int> res(m + n, 0);
        int p1 = 0, p2 = 0;
        int i = 0;
        while (p1 < m && p2 < n)
        {
            if (nums1[p1] < nums2[p2])
            {
                res[i++] = nums2[p2++];
            }
            else if (nums1[p1] == nums2[p2])
            {
                if (maxNumsVector(nums1, p1, nums2, p2) == 1)
                {
                    res[i++] = nums1[p1++];
                }
                else
                {
                    res[i++] = nums2[p2++];
                }
            }
            else
            {
                res[i++] = nums1[p1++];
            }
        }
        while (p1 < m)
        {
            res[i++] = nums1[p1++];
        }
        while (p2 < n)
        {
            res[i++] = nums2[p2++];
        }
        return res;
    }
    vector<int> maxNumber(vector<int> &nums1, vector<int> &nums2, int k)
    {
        int m = nums1.size();
        int n = nums2.size();

        int begin = max(0, k - n), end = min(k, m);
        vector<int> ansNums(k, 0);
        for (int i = begin; i <= end; ++i)
        {
            vector<int> tempNums1 = getNumsFromVector(nums1, i);
            vector<int> tempNums2 = getNumsFromVector(nums2, k - i);
            vector<int> mergeRes = merge(tempNums1, tempNums2);
            if (maxNumsVector(ansNums, 0, mergeRes, 0) == 2)
            {
                ansNums = mergeRes;
            }
        }
        return ansNums;
    }
};
```

### B
```cpp
class Solution
{
public:
    vector<int> find_kMax(vector<int> &num, int k)
    {
        vector<int> res;
        int i = 0;
        while (k > 0)
        {
            int idx = i;

            for (; i < num.size() - k + 1; ++i)
            {
                if (num[i] > num[idx])
                    idx = i;
            }
            res.push_back(num[idx]);
            i = idx + 1;
            --k;
        }
        return res;
    }

    vector<int> merge(vector<int> &nums1, vector<int> &nums2)
    {
        vector<int> res;

        for (auto iter1 = nums1.begin(), iter2 = nums2.begin(); iter1 != nums1.end() || iter2 != nums2.end();)
        {
            if (lexicographical_compare(iter1, nums1.end(), iter2, nums2.end()))
                res.push_back(*iter2++);
            else
                res.push_back(*iter1++);
        }
        return res;
    }
    vector<int> maxNumber(vector<int> &nums1, vector<int> &nums2, int k)
    {
        vector<int> res;
        for (int i = 0; i <= k; ++i)
        {
            int j = k - i;
            if (i > nums1.size() || j > nums2.size())
                continue;
            auto tmp1 = find_kMax(nums1, i);
            auto tmp2 = find_kMax(nums2, j);
            res = max(res, merge(tmp1, tmp2));
        }
        return res;
    }
};
```

### C
```cpp
class Solution
{
public:
    vector<int> maxNumber(vector<int> &nums1, vector<int> &nums2, int k)
    {
        int m = nums1.size(), n = nums2.size();
        vector<int> res;
        for (int i = max(0, k - n); i <= min(k, m); i++)
            res = max(res, mergeVector(maxVector(nums1, i), maxVector(nums2, k - i)));
        return res;
    }
    vector<int> maxVector(vector<int> nums, int k)
    {
        int drop = nums.size() - k;
        vector<int> res;
        for (int num : nums)
        {
            while (drop && res.size() && res.back() < num)
            {
                res.pop_back();
                --drop;
            }
            res.push_back(num);
        }
        res.resize(k);
        return res;
    }
    vector<int> mergeVector(vector<int> nums1, vector<int> nums2)
    {
        vector<int> res;
        while (nums1.size() + nums2.size())
        {
            vector<int> &tmp = nums1 > nums2 ? nums1 : nums2;
            res.push_back(tmp[0]);
            tmp.erase(tmp.begin());
        }
        return res;
    }
};
```