• 321. 拼接最大数


    给定长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,表示两个自然数各位上的数字。现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个数组中取出的数字保持其在原数组中的相对顺序。

    求满足该条件的最大数。结果返回一个表示该最大数的长度为 k 的数组。

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

    示例 1:

    输入:
    nums1 = [3, 4, 6, 5]
    nums2 = [9, 1, 2, 5, 8, 3]
    k = 5
    输出:
    [9, 8, 6, 5, 3]
    示例 2:

    输入:
    nums1 = [6, 7]
    nums2 = [6, 0, 4]
    k = 5
    输出:
    [6, 7, 6, 0, 4]
    示例 3:

    输入:
    nums1 = [3, 9]
    nums2 = [8, 9]
    k = 3
    输出:
    [9, 8, 9]

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/create-maximum-number
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

     1 class Solution {
     2 public:
     3 /*
     4 4
     5 3 4 6 5
     6 6
     7 9 1 2 5 8 3
     8 5
     9 */
    10     //lexicographical order字典序
    11     //lexicographical 词典编纂的
    12     //lexicon 字典的
    13     //graphical 绘画的 计算机图形的
    14     //lexicographical_compare函数
    15 
    16     //如果[first1, last1)按字典序列小于[first2, last2),返回true,否则返回false。
    17     //bool lexicographical_compare( InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2 );
    18 
    19     //思路
    20     //1、首先要解决要从一个num中保持原有位置 取出k个数 组成最大数的算法
    21     //    可以删除的数的个数del=num.size()-k
    22     //    从头到尾依次取出数放入栈中 while栈顶元素小于当前元素 且del不为0 则pop,del--
    23     //    push当前元素
    24     //2、 接着运用上述算法从nums1中取出x个数 从nums2中取出k-x个数 
    25     //    然后运用取出来的数 组成新的最大的数
    26     //    最后从nums1 nums2取出不同数量组合的数的 组合中 找出最大的即为答案
    27     vector<int> KmaxNumber(vector<int>& nums, int k)//从一个num中保持原有位置 取出k个数 组成最大数
    28     {
    29         int nums_size = nums.size();
    30         int del = nums_size - k;
    31         vector<int> res;
    32         for (auto num : nums)
    33         {
    34             while (res.size() && res.back() < num && del)
    35             {
    36                 res.pop_back();
    37                 del--;
    38             }
    39             res.push_back(num);
    40         }
    41         while(res.size()>k)res.pop_back();
    42         return res;
    43     }
    44     vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
    45         int nums1_size = nums1.size(), nums2_size = nums2.size();
    46         vector<int> res;
    47         for (int x = max(0, k - nums2_size); x <= min(nums1_size, k); x++)
    48         {
    49             vector<int> kNum1Max = KmaxNumber(nums1, x);
    50             vector<int> Knum2Max = KmaxNumber(nums2, k - x);
    51             vector<int> temp;
    52             auto num1_iter = kNum1Max.begin();
    53             auto num2_iter = Knum2Max.begin();
    54             while (num1_iter != kNum1Max.end() || num2_iter != Knum2Max.end())
    55                 temp.push_back(lexicographical_compare
    56                     (num1_iter, kNum1Max.end(), num2_iter, Knum2Max.end()) ? 
    57                     *num2_iter++ : *num1_iter++);
    58             res = lexicographical_compare(res.begin(), res.end(), temp.begin(), temp.end()) ? temp : res;
    59         }
    60         return res;
    61     }
    62 };
  • 相关阅读:
    第1次作业
    第0次作业
    总结报告
    第14、15周作业
    第七周作业
    第六周作业
    第四周作业
    第四次作业
    第三次作业
    2018第二次作业
  • 原文地址:https://www.cnblogs.com/lancelee98/p/13294871.html
Copyright © 2020-2023  润新知