321. Create Maximum Number (c++ ——> 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 digits.

Note: You should try to optimize your time and space complexity.

Example 1:

Input:
nums1 = [3, 4, 6, 5]
nums2 = [9, 1, 2, 5, 8, 3]
k = 5
Output: [9, 8, 6, 5, 3]

Example 2:

Input:
nums1 = [6, 7]
nums2 = [6, 0, 4]
k = 5
Output: [6, 7, 6, 0, 4]

Example 3:

Input:
nums1 = [3, 9]
nums2 = [8, 9]
k = 3
Output: [9, 8, 9]

Approach #1: C++. [greedy + dp]

class Solution {
public:
    vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
        int n1 = nums1.size();
        int n2 = nums2.size();
        vector<int> ans;
        for (int k1 = 0; k1 <= k; ++k1) {
            int k2 = k - k1;
            if (k1 > n1 || k2 > n2) continue;
            ans = max(ans, maxNum(maxNum(nums1, k1), maxNum(nums2, k2)));
        }
        return ans;
    }

private:
    vector<int> maxNum(const vector<int>& nums, int k) {
        if (k == 0) return {};
        vector<int> ans;
        int to_pop = nums.size() - k;
        for (auto num : nums) {
            while (!ans.empty() && num > ans.back() && to_pop-- > 0)
                ans.pop_back();
            ans.push_back(num);
        }
        ans.resize(k);
        return ans;
    }

    vector<int> maxNum(const vector<int>& nums1, const vector<int>& nums2) {
        vector<int> ans;
        auto s1 = nums1.cbegin();
        auto e1 = nums1.cend();
        auto s2 = nums2.cbegin();
        auto e2 = nums2.cend();
        int index = 0;
        while (s1 != e1 || s2 != e2)
            ans.push_back(lexicographical_compare(s1, e1, s2, e2) ? *s2++ : *s1++);
        return ans;
    }
};

  

reference:

analysis

cbegin

lexicographical_compare

原文地址:https://www.cnblogs.com/ruruozhenhao/p/10203336.html

时间: 2024-10-08 09:10:23

321. Create Maximum Number (c++ ——> lexicographical_compare)的相关文章

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

[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 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

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

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

[NACOS HTTP-GET] The maximum number of tolerable server reconnection errors has been reached

错误的意思是:已达到可容忍的服务器重连接错误的最大数目.有两个解决思路:一个将这个值设置的更大:然后是排查自己连接服务哪儿出了问题.先说在哪儿设置这个值:在拉取nacos服务的注解配置中,添加一个属性maxRetry,这个值源码中默认给的是3,可以将其设置的更大一些. 1 @Configuration 2 @EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848", names

POJ2699 The Maximum Number of Strong Kings

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2102   Accepted: 975 Description A tournament can be represented by a complete graph in which each vertex denotes a player and a directed edge is from vertex x to vertex y if player x beats

Maximum number of WAL files in the pg_xlog directory (1)

Guillaume Lelarge: Hi, As part of our monitoring work for our customers, we stumbled upon an issue with our customers' servers who have a wal_keep_segments setting higher than 0. We have a monitoring script that checks the number of WAL files in the