Skip to content

add two-pointer solutions#1789

Open
nikhylw wants to merge 1 commit into
super30admin:masterfrom
nikhylw:master
Open

add two-pointer solutions#1789
nikhylw wants to merge 1 commit into
super30admin:masterfrom
nikhylw:master

Conversation

@nikhylw

@nikhylw nikhylw commented Jun 1, 2026

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Merging of 2 arrays (remove_duplicates_sortarr.py)

The student appears to have submitted code for the wrong problem. The submitted solution addresses "Remove Duplicates from Sorted Array II" (keeping at most 2 duplicates), while the problem statement clearly asks for "Merging of 2 arrays" where two sorted arrays need to be merged into one sorted array in-place.

If the student intended to solve the merge problem, they should implement a solution that:

  • Uses three pointers: one for nums1's valid elements, one for nums2, and one for placing elements from the back
  • Merges from the end to avoid overwriting unprocessed elements
  • Handles edge cases where one array is exhausted

The code quality for the wrong problem is actually quite good - it's clean, well-structured, and efficient. However, it does not address the given problem at all.

VERDICT: NEEDS_IMPROVEMENT


Search 2D sorted matrix II (search_in_2d_matrix.py)

Strengths:

  1. Excellent optimization - by first identifying the correct row, the solution achieves O(m + log n) instead of O(m log n)
  2. Clean implementation with proper binary search implementation (using low + (high - low) // 2 to prevent overflow)
  3. Good edge case handling (when no row has last element >= target)
  4. Well-documented time and space complexity in comments

Areas for Improvement:

  1. The solution assumes the matrix is non-empty (which is valid per constraints, but could add a defensive check)
  2. Could consider the alternative "staircase" approach starting from top-right corner, which is O(m + n) worst case

Minor suggestions:

  • Consider adding type hints for better code documentation
  • The solution is already quite optimal; the approach is sound

VERDICT: PASS


Edit and Remove Duplicates in an array

For remove_duplicates_sortarr.py:

  • Strength: Good attempt at a two-pointer approach with O(1) space
  • Weakness: The comparison logic nums[slow - k] doesn't properly track duplicate counts
  • The condition should track how many times the current element has been seen, not just compare with an element k positions back
  • Consider using a counter variable like the reference solution to track occurrences

For search_in_2d_matrix.py:

  • Strength: Excellent implementation with clear logic flow
  • The row-finding + binary search combination is optimal
  • Well-structured with good variable naming
  • Handles edge cases correctly (row == -1 check)

VERDICT: NEEDS_IMPROVEMENT

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