[LintCode] 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.
Have you met this question in a real interview?
Example

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

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

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

LeetCode上的原题,请参见我之前的博客Create Maximum Number

class Solution {
public:
    /**
     * @param nums1 an integer array of length m with digits 0-9
     * @param nums2 an integer array of length n with digits 0-9
     * @param k an integer and k <= m + n
     * @return an integer array
     */
    vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
        vector<int> res;
        int m = nums1.size(), n = nums2.size();
        for (int i = max(0, k - n); i <= min(k, m); ++i) {
            res = max(res, mergeVec(maxVec(nums1, i), maxVec(nums2, k - i)));
        }
        return res;
    }
    vector<int> maxVec(vector<int> nums, int k) {
        if (k == 0) return {};
        vector<int> res;
        int drop = nums.size() - k;
        for (auto a : nums) {
            while (drop && res.size() && res.back() < a) {
                res.pop_back();
                --drop;
            }
            res.push_back(a);
        }
        res.resize(k);
        return res;
    }
    vector<int> mergeVec(vector<int> nums1, vector<int> nums2) {
        vector<int> res;
        while (nums1.size() + nums2.size()) {
            vector<int> &t = nums1 > nums2 ? nums1 : nums2;
            res.push_back(t[0]);
            t.erase(t.begin());
        }
        return res;
    }
};
时间: 2024-10-04 23:24:24

[LintCode] Create Maximum Number 创建最大数的相关文章

Leetcode 321: 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 + nfrom digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digits

321. Create Maximum Number (c++ ——&gt; lexicographical_compare)

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 digit

Create Maximum Number

1 public class Solution { 2 public int[] maxNumber(int[] nums1, int[] nums2, int k) { 3 int[] result = new int[k]; 4 5 for (int i = Math.max(0, k - nums2.length); i <= k && i <= nums1.length; i++) { 6 int[] candidate = merge(getMaxArray(nums

[LintCode] 585 Maximum Number in Mountain Sequence

4/23/2017 算法班 注意返回的是值,而不是index 1 public int mountainSequence(int[] nums) { 2 if (nums == null || nums.length == 0) return -1; 3 int start = 0, end = nums.length - 1; 4 int ret; 5 6 while (start + 1 < end) { 7 int mid = start + (end - start) / 2; 8 9

leetcode_321 Create Maximum Number

题目分析: 给定两个长度分别为m和n的数组,数组元素为0-9,每个数组元素代表一个数字.从这两个数组中选出一些数字,组成一个数组,是这个数组中的数尽可能大,其长度k <= m + n.要求数组中选出的元素的相对顺序与原数组保持一致.最终返回一个包含k个数字的数组. 解题思路: 1)分别从nums1(长度为m)和nums2(长度为n)中挑选出i(max(0, k - n) <= i <= min(m, k) 和k-i个数,在保持挑选数组的元素相对顺序不变的情况下,使选出的子数组最大化,主要

The maximum number of cell styles was exceeded. You can define up to 4000 styles

POI操作Excel中,导出的数据不是很大时,则不会有问题,而数据很多或者比较多时, 就会报以下的错误,是由于cell styles太多create造成,故一般可以把cellstyle设置放到循环外面 报错如下: Caused by: java.lang.IllegalStateException: The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook

414. Third Maximum Number

Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n). 给定一个非空数组的整数,返回该数组中的第三个最大数. 如果不存在,返回最大数量. 时间复杂度必须在O(n)中. Example 1: Input: [3, 2,

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 digit

[CC-ANUGCD]Maximum number, GCD condition

[CC-ANUGCD]Maximum number, GCD condition 题目大意: 一个长度为\(n(n\le10^5)\)的数列\(A(A_i\le10^5)\),\(m(m\le10^5)\)次询问,每次询问\(l\sim r\)中不与\(g\)互质的数中的最大数以及最大数的个数. 思路: 对于每个质数维护一棵线段树,记录区间内包含这个质数的数的和.询问时将\(g\)分解质因数,在线段树上寻找最大值. 统计个数时将所有数以数值为第一关键字.下标为第二关键字排序后二分即可. 源代码: