I recently solved the Merge Sorted Array question on leetcode
Here is the part of the code I am having doubts on :
while (curr >= 0 && p1 >= 0 && p2 >= 0) {
// more TC if we use the condition >= , not sure of the reason
if (nums1[p1] > nums2[p2]) {
nums1[curr] = nums1[p1];
nums1[p1] = 0; // replacing 0 and greater number in nums1
p1--;
} else {
nums1[curr] = nums2[p2];
p2--;
}
curr--;
}
I noticed that when I use >= in the while condition (instead of >), the time complexity seems to increase or the solution behaves slower. Why ?
>=are you talking about? Please update your answer.>=instead of>you'd have 1 extra case in all scenarios to compute for: the==case. So you've already got inherently more work out the gate, computational performance aside.