Skip to content

two pointers 1#1861

Open
ankitakulkarnigit wants to merge 3 commits intosuper30admin:masterfrom
ankitakulkarnigit:master
Open

two pointers 1#1861
ankitakulkarnigit wants to merge 3 commits intosuper30admin:masterfrom
ankitakulkarnigit:master

Conversation

@ankitakulkarnigit
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Arrange Colors (Sample.py)

Your solution correctly sorts the colors using a counting approach. However, there are a few areas for improvement:

  • Instead of using a hashmap, you can use a fixed-size array of length 3 for counting. This would be more efficient and straightforward since the values are only 0,1,2.
  • The solution requires two passes: one to count and one to overwrite. The problem asks for a one-pass algorithm as a follow-up. Consider implementing the Dutch National Flag algorithm (using three pointers: low, mid, high) which sorts in a single pass and is more efficient.
  • In your code, the conditions in the loop are redundant. For example, you check if 0 in hashmap every time, but since you initialized the hashmap with keys 0,1,2, these keys will always be present. You can simply check if hashmap[0] > 0 without the key existence check.

Here's how you can improve:

  • Use an array count = [0,0,0] for counting.
  • Alternatively, implement the one-pass algorithm:
    Initialize three pointers: low=0, mid=0, high=len(nums)-1.
    While mid <= high:
    If nums[mid] is 0, swap with low, increment low and mid.
    If nums[mid] is 1, increment mid.
    If nums[mid] is 2, swap with high, decrement high.

This approach is more efficient and meets the follow-up requirement.

VERDICT: NEEDS_IMPROVEMENT


3 sum

Note: The student's solution is for the "3 sum" problem, but the provided code includes solutions for multiple problems. We are only evaluating the "threeSum" function in the context of the "3 sum" problem.

Let's begin.

VERDICT: NEEDS_IMPROVEMENT


Container With Most Water

Your solution for the "Container With Most Water" problem is excellent. You have implemented the optimal two-pointer approach with O(n) time complexity and O(1) space complexity. This is a significant improvement over the brute-force reference solution.

Strengths:

  • Correct and efficient algorithm.
  • Clean and readable code.
  • Good use of variable names.

Areas for improvement:

  • The comment at the beginning mentions "brute force = nested for loop", but your code does not implement brute force. It would be better to remove or update this comment to avoid confusion.
  • The condition elif height[j] < height[i] can be replaced with a simple else because if height[i] <= height[j] is false, then necessarily height[j] < height[i] is true. Using else would make the code slightly more concise.

Suggested improvement:

class Solution:
    def maxArea(self, height: List[int]) -> int:
        maxarea = 0
        i, j = 0, len(height)-1

        while i < j:
            area = min(height[i], height[j]) * (j - i)  # abs(j-i) is same as (j-i) since j>i
            maxarea = max(area, maxarea)
            if height[i] <= height[j]:
                i += 1
            else:
                j -= 1

        return maxarea

Note: Also, abs(j-i) is unnecessary because j is always greater than i in the while loop, so (j-i) is positive. You can simply use (j-i).

Overall, your solution is correct and efficient. Well done!

VERDICT: PASS

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