A Tip for C++ programmer
The basic idea is sorts of like brute force, since we just pick 1 number from nums1, k-1 from nums2, \nnext we pick 2 from nums1, k-2 from nums2 , and so on....
The basic idea is sorts of like brute force, since we just pick 1 number from nums1, k-1 from nums2, \nnext we pick 2 from nums1, k-2 from nums2 , and so on........\nThanks to post "share my greedy solution"(ranking number 1 under this topic), I write a similiar one, but in C++.\nThe only thing I\'d like to share is, under such situation, it seems that we need to always copy a vector to another. And this will cost a lot. \nBut actually it shouldn\'t. Unlike Java, if you do something like`int a[] , int b[], a = b`, in java, the just a copy of **reference** , but in c++, it will automatically call the **copy** method, which indeed costs a lot.\nHowever we can still use assignment in term of reference by calling std::move. And if you do not know the method or the difference between rvalue and lvalue, please look up on C++ reference......\n```\npublic:\n    vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {\n        int n = nums1.size();\n        int m = nums2.size();\n        vector<int> ans(k,0);\n        for (int i=std::max(0,k-m);i<=k && i<=n;i++){\n            vector<int> candidate = std::move(merge(maxArray(nums1,i),maxArray(nums2,k-i)));\n            if (greater(candidate,0,ans,0)) ans = std::move(candidate);\n        }\n        return ans;\n    }\nprivate:\n    bool greater(vector<int>& nums1, int i, vector<int>& nums2, int j){\n        while (i<nums1.size() && j< nums2.size() && nums1[i] == nums2[j]){i++;j++;}\n        return (j == nums2.size()) || (i<nums1.size() && nums1[i] > nums2[j]);\n    }\n    vector<int> merge(vector<int> && nums1, vector<int> && nums2){\n        int k = nums1.size() + nums2.size();\n        vector<int> ans(k);\n        for (int r=0, i = 0 , j = 0; r < k ;r++)\n             ans[r] = greater(nums1,i,nums2,j) ? nums1[i++] : nums2[j++];\n        return ans;\n    }\n    vector<int> maxArray(vector<int> & nums, int k) {\n        int n = nums.size();\n        vector<int> ans(k);\n        for (int i=0,j=0;i<n;i++){\n            while (n-i+j > k && j > 0 && ans[j-1] < nums[i]) j--;\n            if (j<k) ans[j++] = nums[i];\n        }\n        return ans;\n    }\n```\nHope it helps.
[]