Skip to content

two-pointers-2 - #1794

Open
joshyoon92 wants to merge 2 commits into
super30admin:masterfrom
joshyoon92:master
Open

two-pointers-2#1794
joshyoon92 wants to merge 2 commits into
super30admin:masterfrom
joshyoon92:master

Conversation

@joshyoon92

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Merge Sorted Array (MergeSorted.java)

Strengths:

  • Correctly implements the merge algorithm with proper pointer management
  • Good use of in-place merging to achieve O(1) space complexity
  • Clean and readable code structure
  • Handles edge cases properly (when m=0 or n=0)

Areas for Improvement:

  • Consider renaming variables to be more descriptive (e.g., p1 and p2 instead of left and i to match common conventions)
  • The condition while (i>=0 && left>=0) in the first loop could be simplified to just while (left >= 0) since the second while loop handles remaining nums2 elements. However, this is a minor stylistic choice.
  • Adding comments explaining the algorithm would improve readability for others reading the code

VERDICT: PASS


Search a 2D Matrix II (RemoveDup.java)

  1. Wrong Problem: The submitted solution solves a completely different problem. The code removes duplicates from a sorted array allowing at most 2 occurrences, not searching for a target in a 2D matrix.

  2. Wrong Language: The reference solution is in C++, but using Java is acceptable. However, the student should ensure the solution matches the problem.

  3. For the actual problem (Search a 2D Matrix II): The reference solution uses binary search on each row, achieving O(m log n) time. A more optimal approach would be to start from the top-right corner and eliminate either a row or column at each step, achieving O(m + n) time.

  4. If this were the RemoveDup problem: The solution is well-structured with clear variable naming (slow, fast, count). The logic correctly handles the at-most-2-duplicates requirement. However, the code could benefit from comments explaining the two-pointer technique.

VERDICT: NEEDS_IMPROVEMENT


Remove Duplicates from Sorted Array II (Search2D.java)

  1. Wrong Problem: You submitted code for "Search 2D Matrix II" instead of "Remove Duplicates from Sorted Array II". Please ensure you are working on the correct problem assignment.

  2. Syntax Error: The variable matrix is used but never defined. The method parameter is m, so you should use m[row][col] instead of matrix[row][col].

  3. Missing Solution: There is no attempt to solve the duplicate removal problem. The correct approach would use a two-pointer technique where you track the count of consecutive duplicates and only write elements that appear at most twice.

  4. Code Structure: If you were to solve the correct problem, your code should:

    • Use two pointers (slow and fast)
    • Track the count of consecutive duplicate elements
    • Only copy elements to the slow pointer when count ≤ 2
    • Return the length of the modified array

Please resubmit a solution for the correct problem "Remove Duplicates from Sorted Array II".

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