Leetcode 321. Create Maximum Number

Source: Internet
Author: User

Because the size of K is uncertain, so there are three possible, the first is that when K is 0 o'clock, two arrays are not counted; the second is that when K is not larger than the length of one of the arrays, it is possible to take a number from only one array, and the third case is that K is greater than the length of one of the arrays As for each array, take a few, each of which takes into account, and then each more results. For the case of taking numbers from two arrays, we need to sort the two out decimal groups into an array, with the relative order of the respective numbers in the decimal group unchanged. We also need a function to take a number of functions from the array, and the number to be removed is the largest. For example, the current array length is n, need to take out K numbers, we define a variable drop = N-k, indicating the number of numbers that need to be discarded, we iterate over the number in the array, the following loop, if the drop is an integer, and the result array length is not 0, the result array of the tail element is less than the currently traversed element The trailing element of the result array is removed, and the drop is reduced by 1, repeating the loop until any of the above conditions are satisfied, and then adding the current element to the result array, and finally we return the first k elements in the result array. For a mix of two arrays, we simply take two from the beginning of the two arrays, add the large one to the result array, and then delete the large, and then continue to take a pair of comparisons until the two arrays are empty stops. See the code for the discussion area below:

Class Solution {public:vector<int> MaxNumber (vector<int>& nums1, vector<int>& nums2, int k
    ) {int N1 = Nums1.size (), N2 = Nums2.size ();
    Vector<int> best;
                                   for (int k1=max (K-N2, 0), K1<=min (K, N1), and ++k1) best = max (MaxNumber (MaxNumber, NUMS1),
    MaxNumber (NUMS2, K-K1));
return best;
    } vector<int> MaxNumber (vector<int> nums, int k) {int drop = Nums.size ()-K;
    Vector<int> out; for (int num:nums) {while (drop && out.size () && out.back () < num) {OUT.POP_BAC
            K ();
        drop--;
    } out.push_back (num);
    } out.resize (k);
return out;
    } vector<int> MaxNumber (vector<int> nums1, vector<int> nums2) {vector<int> out;
        while (Nums1.size () + nums2.size ()) {vector<int>& now = nums1 > nums2? nums1:nums2;
   Out.push_back (Now[0]);     Now.erase (Now.begin ());
} return out; }
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.