Skip to content

[IMP] Two-Pointer-1#1848

Open
amitmittal117 wants to merge 1 commit intosuper30admin:masterfrom
amitmittal117:master
Open

[IMP] Two-Pointer-1#1848
amitmittal117 wants to merge 1 commit intosuper30admin:masterfrom
amitmittal117:master

Conversation

@amitmittal117
Copy link

No description provided.

@super30admin
Copy link
Owner

Your solution for the "sort-colors.py" problem is excellent. It is efficient, with optimal time and space complexity, and the code is clear and concise. Well done!

However, note that the problem you are solving is "Arrange Colors" (sort colors), but you included code for "3sum" and "container with most water" in the same response. This might be an error in your submission. In the future, ensure that you only provide the solution for the problem being evaluated.

One minor point: in the condition for nums[mid] == 0, you increment mid after swapping. This is correct because after swapping, the element at mid is now 1 (since low was pointing to a 1 or 0, but after swapping with low, which is at a position that has been processed, it is safe to move mid). However, it's important to understand why we increment mid in this case but not when we swap with high. When swapping with high, we don't know what value we get from high, so we need to check it again in the next iteration. But when swapping with low, we know that low was pointing to a 1 (because low and mid have been traversing together until they meet a 2), so we can safely increment mid.

Keep up the good work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants