92ms cpp: try first biggest num, with index pre-process
The basic idea is to find the largest number in answer array's first position: try number from 9 to 0, firstly in nums1: if a number can be found, check if the 
The basic idea is to find the largest number in answer array's first position: try number from 9 to 0, firstly in nums1: if a number can be found, check if the left numbers (minus numbers before the found number) are sufficient for k.\n\nIf we can find a largest number in both nums1 and nums2, we should compare the two answers, and keep the bigger one.\n\nA fast pre-process for above solution: for i-th element in array nums1, lowPos1[i][num] is the smallest index of num in range [i, nums1.size()-1]. Since we have this helper, we can use O(1) time to find if num exists in nums1 or nums2 from a given position.\n\nAn important special case: when nums1.size() + nums2.size() = k, which means we have to use all elements in both arrays to produce the k-length answer. In such case, set two pointers pointing to the beginning of two arrays. Move the pointer with bigger number. Tricky part is when two pointers point to a same value, in this case we detect the values in two arrays behind the two pointers; we move the two pointers together until we find the values are different or one array is ended. The pointer with bigger value should go first, and if one pointer gets to end of array, the other pointer should go first.\n\n\n    class Solution {\n    public:\n        unordered_map<string, vector<int>> store;\n        deque<vector<int>> lowPos1, lowPos2;\n        int len1, len2;\n        \n        vector<int> get(int p1, int p2, int k, vector<int>& nums1, vector<int>& nums2) {\n            vector<int> ans;\n            if (k==0)\n                return ans;\n            \n            char buf[50];\n            sprintf(buf, "%d.%d.%d", p1, p2, k);\n            string key = buf;\n            if (store.find(key) != store.end())\n                return store[key];\n            \n            int rest = nums1.size()-p1 + nums2.size()-p2;\n            if (k==rest) {\n                while (p1<len1 || p2<len2)\n                {\n                    if (p1==len1 || (p2<len2 && nums1[p1] < nums2[p2]))\n                        ans.push_back(nums2[p2++]);\n                    else if (p2==len2 || (p1<len1 && nums1[p1] > nums2[p2]))\n                        ans.push_back(nums1[p1++]);\n                    else {\n                        int i=0;\n                        while (p1+i<len1 && p2+i<len2) {\n                            if (nums1[p1+i] != nums2[p2+i])\n                                break;\n                            i++;\n                        }\n                        if (p1+i==len1 || (p2+i<len2 && nums2[p2+i]>nums1[p1+i]))\n                            ans.push_back(nums2[p2++]);\n                        else\n                            ans.push_back(nums1[p1++]);\n                    }\n                }\n            }\n            else\n            {\n                for (int i=9; i>=0; i--) \n                {\n                    vector<int> ans1, ans2;\n                    int j = p1==len1 ? -1 : lowPos1[p1][i];\n                    if (j!=-1 && rest-(j-p1)-1 >= k-1)\n                    {\n                        ans1.push_back(i);\n                        vector<int> temp = get(j+1, p2, k-1, nums1, nums2);\n                        ans1.insert(ans1.end(), temp.begin(), temp.end());\n                    }\n                    \n                    j = p2==len2 ? -1 : lowPos2[p2][i];\n                    if (j!=-1 && rest-(j-p2)-1 >= k-1)\n                    {\n                        ans2.push_back(i);\n                        vector<int> temp = get(p1, j+1, k-1, nums1, nums2);\n                        ans2.insert(ans2.end(), temp.begin(), temp.end());\n                    }\n        \n                    if (ans1.size() + ans2.size())\n                    {\n                        if (ans1.size() < ans2.size())\n                            std::swap(ans1, ans2);\n                        bool use1=true;\n                        for (int i=0; i<min(ans1.size(), ans2.size()); i++)\n                            if (ans2[i] != ans1[i]) {\n                                use1 = ans1[i]>ans2[i];\n                                break;\n                            }\n                        ans = use1 ? ans1 : ans2;\n                        break;\n                    }\n                }\n            }\n            \n            store[key] = ans;\n            return ans;\n        }\n        \n        void preProcess(deque<vector<int>>& lowPos, const vector<int>& nums) {\n            lowPos.push_back(vector<int> (10, -1));\n            for (int i=(int)nums.size()-1; i>=0; i--) {\n                lowPos.push_front(lowPos.front());\n                lowPos.front()[nums[i]] = i;\n            }\n            lowPos.pop_back();\n        }\n        \n        vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {\n            len1=nums1.size(), len2=nums2.size();\n            preProcess(lowPos1, nums1);\n            preProcess(lowPos2, nums2);\n            \n            return get(0, 0, k, nums1, nums2);\n        }\n    };
[]