Merger of NearestGreaterElement and Lexicographical
Do below problems before attempt this problem\n\nhttps://leetcode.com/problems/remove-k-digits/\nhttps://leetcode.com/problems/next-greater-element-ii/\nhttps:/
**Do below problems before attempt this problem**\n\nhttps://leetcode.com/problems/remove-k-digits/\nhttps://leetcode.com/problems/next-greater-element-ii/\nhttps://leetcode.com/problems/remove-duplicate-letters/\n\n\tclass Solution {\n\t\tpublic:\n\n\n\t\t   vector<int> maxNumber(vector<int> &nums1, vector<int> &nums2, int k)\n\t\t\t{\n\t\t\t\tvector<int> ans;\n\t\t\t\tfor (int i = 0; i <=k; i++)\n\t\t\t\t{\n\t\t\t\t\tint j = k - i;\n\t\t\t\t\t\n\t\t\t\t\tif(i>nums1.size()||j>nums2.size()) continue;\n\n\t\t\t\t\tvector<int> n1 = calculateNGEType(nums1, i);\n\t\t\t\t\tvector<int> n2 = calculateNGEType(nums2, j);\n\t\t\t\t\tvector<int> t = merge(n1, n2,k);\n\t\t\t\t\tif(t>ans)ans=t;\n\n\t\t\t\t}\n\n\t\t\t\treturn ans;\n\t\t\t}\n\n\n\t\t\tbool greater(vector<int> n1, vector<int> n2,int i,int j){\n\t\t\t\tint ns1 = n1.size();\n\t\t\t\tint ns2 = n2.size();\n\t\t\t\twhile (i < ns1 || j < ns2)\n\t\t\t\t{\n\t\t\t\t\tif (i >= ns1)\n\t\t\t\t\t\treturn false;\n\t\t\t\t\telse if (j >= ns2)\n\t\t\t\t\t\treturn true;\n\t\t\t\t\telse if (n1[i] > n2[j])\n\t\t\t\t\t\treturn true;\n\t\t\t\t\telse if (n1[i] < n2[j])\n\t\t\t\t\t\treturn false;\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\ti++,j++;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\tvector<int> merge(vector<int> n1, vector<int> n2,int k)\n\t\t\t{\n\t\t\t\tint ns1 = n1.size();\n\t\t\t\tint ns2 = n2.size();\n\t\t\t\tint i = 0, j = 0, l = 0;\n\t\t\t\tvector<int> ans;\n\t\t\t\twhile ((i < ns1 || j < ns2)&&k>0 )\n\t\t\t\t{\n\t\t\t\t\tif (i >= ns1)\n\t\t\t\t\t\tans.push_back(n2[j++]);\n\t\t\t\t\telse if (j >= ns2)\n\t\t\t\t\t\tans.push_back(n1[i++]);\n\t\t\t\t\telse if (n1[i] > n2[j])\n\t\t\t\t\t\tans.push_back(n1[i++]);\n\t\t\t\t\telse if (n1[i] < n2[j])\n\t\t\t\t\t\tans.push_back(n2[j++]);\n\t\t\t\t\telse \n\t\t\t\t\t{\n\t\t\t\t\t\tif(greater(n1,n2,i,j))\n\t\t\t\t\t\t\tans.push_back(n1[i++]);\n\t\t\t\t\t\telse\n\t\t\t\t\t\t\tans.push_back(n2[j++]);\n\t\t\t\t\t}\n\t\t\t\t\tk--;\n\t\t\t\t}\n\t\t\t\treturn ans;\n\t\t\t}\n\n\t\t\tvector<int> calculateNGEType(vector<int> &nums1, int k)\n\t\t\t{\n\t\t\t\tint removal = nums1.size() - k;\n\t\t\t\tvector<int> n1;\n\n\t\t\t\tfor (int i = 0; i < nums1.size(); i++)\n\t\t\t\t{\n\t\t\t\t\twhile (n1.size() > 0 && removal > 0 && n1.back() < nums1[i])\n\t\t\t\t\t{\n\t\t\t\t\t\tremoval--;\n\t\t\t\t\t\tn1.pop_back();\n\t\t\t\t\t}\n\t\t\t\t\tn1.push_back(nums1[i]);\n\t\t\t\t}\n\n\t\t\t\treturn n1;\n\t\t\t}\n\n\n\t\t};
['Stack', 'Greedy']