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.
Example 1:
nums1 = [3, 4, 6, 5]
nums2 = [9, 1, 2, 5, 8, 3]
k = 5
return [9, 8, 6, 5, 3]
Example 2:
nums1 = [6, 7]
nums2 = [6, 0, 4]
k = 5
return [6, 7, 6, 0, 4]
Example 3:
nums1 = [3, 9]
nums2 = [8, 9]
k = 3
return [9, 8, 9]
看到题先打个标签,贪心问题,大致思路有了,剩下就是程序设计的细节。
这题还算难点的。程序设计时往往需要考虑特例情况,在这一题中就要考虑某次可以从nums1和nums2里取出的最大的数相等的情况该怎么办,这时你如果死揪着这个不放就会陷入一个黑洞——如果往下算一层还是相等怎么办?这样程序写起来就会很复杂,而且不清晰,甚至根本写不出来。这时你就要换一个思路了。程序员要做的应该是简单的事,复杂的事嘛就交给计算机好了。这话说起来容易,其实要做到挺不容易,慢慢体会吧。
回到这个问题,非要在这次就得到一个唯一的答案吗?不必的。相等的话就都留下吧,留给下一次处理,不要妄想一次就把所有问题都解决,每次解决一点,每次都往正确解靠近就行。
class Solution { public: int do_once(vector<int>&nums1,vector<int>&nums2,vector<pair<int, int>>&candi, int k,set<pair<int,int>>&hist) { vector<pair<int, int>>newcandi; int maxret = 0; for (int i = 0; i < candi.size(); i++) { int choose1(-1), choose2(-1); int ptr1(0), ptr2(0); if (nums1.size()-candi[i].first + nums2.size()-candi[i].second == k) { if (candi[i].first==nums1.size() || candi[i].second==nums2.size()) { if (candi[i].first == nums1.size()) { if (nums2[candi[i].second] >= maxret) { if (nums2[candi[i].second] > maxret) { maxret = nums2[candi[i].second]; newcandi.clear(); } int aa = candi[i].second+1; if (hist.find(pair<int, int>(candi[i].first,aa)) == hist.end()) { newcandi.push_back(pair<int, int>( candi[i].first,aa)); hist.insert(pair<int, int>(candi[i].first,aa)); } } } if (candi[i].second==nums2.size()) { if (nums1[candi[i].first] >= maxret) { if (nums1[candi[i].first] > maxret) { maxret = nums1[candi[i].first]; newcandi.clear(); } int aa = candi[i].first+1; if (hist.find(pair<int, int>(aa, candi[i].second)) == hist.end()) { newcandi.push_back(pair<int, int>(aa, candi[i].second)); hist.insert(pair<int, int>(aa, candi[i].second)); } } } } else { int gt = nums1[candi[i].first] > nums2[candi[i].second] ? nums1[candi[i].first] : nums2[candi[i].second]; if (gt >= maxret) { if (gt > maxret) { newcandi.clear(); maxret = gt; } if (nums1[candi[i].first] > nums2[candi[i].second]) { int aa = candi[i].first+1; if (hist.find(pair<int, int>(aa, candi[i].second)) == hist.end()) { newcandi.push_back(pair<int, int>(aa, candi[i].second)); hist.insert(pair<int, int>(aa, candi[i].second)); } } else if (nums1[candi[i].first] < nums2[candi[i].second]) { int aa = candi[i].second+1; if (hist.find(pair<int, int>( candi[i].first,aa)) == hist.end()) { newcandi.push_back(pair<int, int>(candi[i].first,aa)); hist.insert(pair<int, int>( candi[i].first,aa)); } } else { int aa = candi[i].second+1; if (hist.find(pair<int, int>(candi[i].first,aa)) == hist.end()) { newcandi.push_back(pair<int, int>( candi[i].first,aa)); hist.insert(pair<int, int>( candi[i].first,aa)); } int bb = candi[i].first+1; if (hist.find(pair<int, int>(bb, candi[i].second)) == hist.end()) { newcandi.push_back(pair<int, int>(bb, candi[i].second)); hist.insert(pair<int, int>(bb, candi[i].second)); } } } } } else { int j1max = -1; int a1= nums1.size()-candi[i].first; int a2= nums2.size()-candi[i].second; j1max = a1 + a2 - k <= a1 - 1 ? a1 + a2 - k + candi[i].first : a1 - 1 + candi[i].first; for (int j = candi[i].first; j <= j1max; j++) if (nums1[j] > choose1) { choose1 =nums1[j]; ptr1 = j; } int j2max = -1; j2max = a1 + a2 - k <= a2 - 1 ? a1 + a2 - k + candi[i].second : a2 - 1 + candi[i].second; for (int j = candi[i].second; j <= j2max; j++) if (nums2[j] > choose2) { choose2 = nums2[j]; ptr2 = j; } int gt = choose1 > choose2 ? choose1 : choose2; if (gt >= maxret) { if (gt > maxret) { newcandi.clear(); maxret = gt; } if (choose1 > choose2) { int aa = ptr1+1; if (hist.find(pair<int, int>(aa, candi[i].second)) == hist.end()) { newcandi.push_back(pair<int, int>(aa, candi[i].second)); hist.insert(pair<int, int>(aa, candi[i].second)); } } else if (choose1 < choose2) { int aa = ptr2+1; if (hist.find(pair<int, int>( candi[i].first,aa)) == hist.end()) { newcandi.push_back(pair<int, int>(candi[i].first,aa)); hist.insert(pair<int, int>( candi[i].first,aa)); } } else { int aa = ptr2+1; if (hist.find(pair<int, int>(candi[i].first,aa)) == hist.end()) { newcandi.push_back(pair<int, int>(candi[i].first,aa)); hist.insert(pair<int, int>(candi[i].first,aa)); } int bb = ptr1+1; if (hist.find(pair<int, int>(bb, candi[i].second)) == hist.end()) { newcandi.push_back(pair<int, int>(bb, candi[i].second)); hist.insert(pair<int, int>(bb, candi[i].second)); } } } } } candi = newcandi; return maxret; } vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) { vector<int>re; vector<pair<int, int>>candi; candi.push_back(pair<int, int>(0, 0)); set<pair<int, int>>hist; while (k > 0) { re.push_back(do_once(nums1,nums2,candi, k,hist)); k--; } return re; } };
accepted