nums1
and create a sorted copy of nums2
.assigned
to store the elements of nums1
that can beat elements of nums2
.remaining
to store the elements of nums1
that cannot beat any element in nums2
.nums1
and one for the sorted nums2
, to iterate through both arrays.nums2
, if the current element in nums1
can beat it, assign it to assigned
and move both pointers. Otherwise, add the element in nums1
to remaining
and move only the pointer for nums1
.assigned
and remaining
to reconstruct the final answer array.nums2
, if there is an assigned element in assigned
, use it. Otherwise, use the next element from remaining
.