[LeetCode] Create Maximum Number 创建最大数

简介:

Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digits. You should try to optimize your time and space complexity.

Example 1:

nums1 = [3, 4, 6, 5]
nums2 = [9, 1, 2, 5, 8, 3]
k = 5
return [9, 8, 6, 5, 3]

Example 2:

nums1 = [6, 7]
nums2 = [6, 0, 4]
k = 5
return [6, 7, 6, 0, 4]

Example 3:

nums1 = [3, 9]
nums2 = [8, 9]
k = 3
return [9, 8, 9]

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

这道题给了我们两个数组,里面数字是无序的,又给我们一个k值为k <= m + n,然后我们从两个数组中共挑出k个数,数字之间的相对顺序不变,求能组成的最大的数。这道题的难度是Hard,博主木有想出解法,参考网上大神们的解法来做的。由于k的大小不定,所以有三种可能,第一种是当k为0时,两个数组中都不取数;第二种是当k不大于其中一个数组的长度时,有可能只从一个数组中取数;第三种情况是k大于其中一个数组的长度,则需要从两个数组中分别取数,至于每个数组中取几个,每种情况都要考虑到,然后每次更结果即可。对于分别从两个数组中取数字的情况,我们需要将两个取出的小数组混合排序成一个数组,小数组中各自的数字之间的相对顺序不变。我们还需要一个函数来从数组中取若干个数字的函数,而且取出的数要最大。比如当前数组长度为n,需要取出k个数字,我们定义一个变量drop = n - k,表示需要丢弃的数字的个数,我们遍历数组中的数字,进行下列循环,如果此时drop为整数,且结果数组长度不为0,结果数组的尾元素小于当前遍历的元素,则去掉结果数组的尾元素,此时drop自减1,重复循环直至上述任意条件不满足为止,然后把当前元素加入结果数组中,最后我们返回结果数组中的前k个元素。对于两个数组的混合,我们只要从两个数组开头每次取两个,把大的加入结果数组,然后删掉这个大的,然后继续取一对比较,直到两个数组都为空停止。参见代码如下:

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;
    }
};

本文转自博客园Grandyang的博客,原文链接:创建最大数[LeetCode] Create Maximum Number ,如需转载请自行联系原博主。

相关文章
|
6月前
Leetcode 53.Maximum Subarray
题意简单,给出一个数组,求出其中最大的子数组和。 这种简单题目背后蕴藏着很巧妙的解题方法。其实只需要遍历一次数组就可以求得解。 思路是这样的,你想想看,如果一段子数组的和是负数, 那么这一段子数组不可能是最大和数组的一部分,丢掉重新从下一个位置开始选。
27 0
|
7月前
【Leetcode -746.使用最小花费爬楼梯 -747.至少是其他数字两倍的最大数】
【Leetcode -746.使用最小花费爬楼梯 -747.至少是其他数字两倍的最大数】
47 0
|
6月前
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
65 1
|
5月前
|
算法 Java vr&ar
☆打卡算法☆LeetCode 179. 最大数 算法解析
☆打卡算法☆LeetCode 179. 最大数 算法解析
|
6月前
|
存储
Leetcode Single Number II (面试题推荐)
给你一个整数数组,每个元素出现了三次,但只有一个元素出现了一次,让你找出这个数,要求线性的时间复杂度,不使用额外空间。
21 0
LeetCode 1877. 数组中最大数对和的最小值
一个数对 (a,b) 的 数对和 等于 a + b 。最大数对和 是一个数对数组中最大的 数对和 。
91 0
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode 104. 二叉树的最大深度 Maximum Depth of Binary Tree
LeetCode 104. 二叉树的最大深度 Maximum Depth of Binary Tree
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode 136. 只出现一次的数字 Single Number
LeetCode 136. 只出现一次的数字 Single Number

热门文章

最新文章