Easy Understanding And Readable Code || Monotonic Stack || C++
Intuition & Approach\n Describe your first thoughts on how to solve this problem. \nI will find lexographically greatest subsequence from both array and then me
# Intuition & Approach\n<!-- Describe your first thoughts on how to solve this problem. -->\nI will find lexographically greatest subsequence from both array and then merge them.\nBut constraint is length=k must be satisfied. So have to find lexographically greater subsequence such that both combinedly have length k.\n\nBut how to decide how much length have to take from first array and how much from second array? --> This time go by bruteforce. So for possible {i, k-i} pair of length , we will find our answer.\n\nlet\'s divide the problem into subparts:\nsubpart-1: find out lexographically greatest subsequence from both array.\nsubpart-2: Merge them and keep taking maximum.\n\nImplemented stack logic through array. You can go through stack .\n# Complexity\n- Time complexity:O(K*N + K*N)= O(K*N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(K) => for this loop: for(int i=0;i<=k;i++) in maxNumber function.\nO(N) => for getGrtrSubseq function.\nO(K*N) => merge Function.\n- Space complexity: O(N) as all are linear vectors.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    // Function to calculate maximum number from nums of  requiredLength.\n    vector<int> getGrtrSubseq(vector<int> nums, int requiredLength)\n    {\n        vector<int> ans; // Store the resultant maximum\n        int n = nums.size();\n        // i will try to maximize initial digit as much as i can. If falling for shorter length, then i am forced to take last requiredLength-ans.size()\n        //elements left. So, before popping any element check ((requiredLength-ans.size())<(n-i)) to ensure you have requiredLength of digits.\n        for(int i=0;i<n;i++)\n        {\n            while(ans.size()>0 && ans.back()<nums[i] && ((requiredLength-ans.size())<(n-i))) // If true, then pop out the last element\n            ans.pop_back();\n            if(ans.size()<requiredLength)ans.push_back(nums[i]); \n        }\n        return ans;\n    }\n    void pop_front(std::vector<int> &v)\n    {\n        if (v.size() > 0)v.erase(v.begin());\n    }\n    vector<int> merge(vector<int> p1, vector<int>p2, int k)\n    {\n        vector<int> temp;\n        for(int j=0;j<k;j++)\n        { \n            vector<int> temp2 = max(p1,p2);\n            int fr = temp2.front();\n            if(p1>p2)\n            pop_front(p1);\n            else\n            pop_front(p2);\n            temp.push_back(fr);\n        }\n      return temp;\n    }\n    vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) \n    {\n        int n1=nums1.size(), n2=nums2.size();\n        vector<int>res;\n        for(int i=0;i<=k;i++)\n        {\n            if(i>n1 || k-i>n2) continue;\n            vector<int>grtrSubseq1=getGrtrSubseq(nums1,i);; \n            vector<int>grtrSubseq2=getGrtrSubseq(nums2,k-i); \n            vector<int>temp=merge(grtrSubseq1,grtrSubseq2,k);      \n            res = max(res, temp);\n        }\n        return res;\n    }\n};\n```\n.![download (2).jpg](https://assets.leetcode.com/users/images/8eac6c0a-55fa-4b53-abd4-45a92c342e39_1685777059.4901643.jpeg)\n\nCorrect me if i am wrong.\n
['Stack', 'Monotonic Stack', 'C++']