classSolution{public:voidmerge(vector<int>&nums1,intm,vector<int>&nums2,intn){// Copy elements from nums2 into the empty spots of nums1for(inti=0;i<n;i++){nums1[m+i]=nums2[i];}// Sort the nums1sort(nums1.begin(),nums1.end());}};
Optimized Code:
classSolution{public:voidmerge(vector<int>&nums1,intm,vector<int>&nums2,intn){intp1=m-1;// Pointing to last real element in nums1intp2=n-1;// Pointing to last element in nums2intp=(m+n)-1;// Pointing to last empty spot in nums1// We stop when p2 becomes less than '0'while(p2>=0){// Check first, p1 is valid or not (there are still real numbers in nums1 to compare)// And, if - nums1 p1'th element is greater than nums2 p2'th element// Then, put nums1 p1'th element at nums1 p'th empty spotif(p1>=0&&nums1[p1]>=nums2[p2]){nums1[p]=nums1[p1];p1--;// Go one step back, cause we used this number}else{// if - nums1 p1'th element is smaller than nums2 p2'th element// Then, put nums2 p2'th element at nums1 p'th empty spotnums1[p]=nums2[p2];p2--;// Go one step back, cause we used this number}p--;// In either case, we have filled one spot in nums1, so move one step back}// No extra steps needed after here, cause nums1 elements are already at corrected spots}};
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)