Skip to content

Solved two pointer-2#1779

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

Solved two pointer-2#1779
pratikb0501 wants to merge 1 commit into
super30admin:masterfrom
pratikb0501:master

Conversation

@pratikb0501
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Merging of 2 arrays (Problem_2.py)

Your solution has the right idea of merging from the end to avoid overwriting elements. However, you forgot to handle the case when there are remaining elements in nums2 after the first loop. In the first loop, you process until either i or j becomes negative. If j (pointer for nums2) is still >=0 after the first loop, it means there are elements in nums2 that need to be copied to the front of nums1. Your code currently does not do this.

To fix this, you should add a second loop after the first to copy any remaining nums2 elements. For example:
while j >= 0:
nums1[ptr] = nums2[j]
j -= 1
ptr -= 1

Also, test your code with cases where nums2 has elements smaller than all in nums1 (like the example above: nums1=[4,5,6], nums2=[1,2,3]) and cases where m=0.

Otherwise, the logic inside the first loop is correct: you are comparing the current elements from the end of both arrays and placing the larger one at the current pointer. This is efficient and avoids extra space.

VERDICT: NEEDS_IMPROVEMENT


Search 2D sorted matrix II (Problem_1.py)

It appears there has been a mix-up in the problem you are solving. The code you provided is for "Remove Duplicates from Sorted Array II", but the problem you are supposed to solve is "Search 2D sorted matrix II".

For the correct problem, you need to design an algorithm that efficiently searches for a target value in a 2D matrix where each row is sorted and each column is sorted. A common efficient approach is to start from the top-right corner (or bottom-left) and move based on comparisons with the target. Alternatively, you can perform binary search on each row, which is what the reference solution does.

Here are some suggestions for the correct problem:

  1. Understand the problem: The matrix has both rows and columns sorted. This allows you to eliminate entire rows or columns with each comparison.
  2. Consider starting from the top-right corner. If the target is greater than the current element, move down (since all elements to the left are smaller). If the target is smaller, move left (since all elements below are larger). This approach runs in O(m + n) time.
  3. Alternatively, you can use binary search on each row (as in the reference solution), which is O(m log n). This is efficient enough given the constraints (m, n up to 300).

Please revisit the problem statement and implement a solution for the correct problem. Your current solution does not address the problem at all.

VERDICT: NEEDS_IMPROVEMENT


Edit and Remove Duplicates in an array (Problem_3.py)

It seems there has been a misunderstanding. The problem you were asked to solve is about removing duplicates from a sorted array, allowing at most two duplicates per element. However, your solution is for a different problem (Search a 2D Matrix II). Please review the problem statement again carefully.

For the correct problem, you need to implement a function that takes an integer array nums and modifies it in-place so that each unique element appears at most twice. You should return the length of the new array. The reference solution uses a two-pointer technique (slow and fast pointers) to traverse the array, counting occurrences of each element and only keeping up to two duplicates.

Here are some steps to guide you:

  1. Initialize a slow pointer (to place valid elements) and a fast pointer (to traverse the array).
  2. Use a counter to track how many times the current element has appeared consecutively.
  3. For each element, if it's the same as the previous, increment the counter; otherwise reset the counter to 1.
  4. If the counter is <= 2, copy the element to the slow pointer position and increment the slow pointer.
  5. Finally, return the slow pointer as the new length.

Please ensure you are solving the correct problem and double-check the problem title and description before coding.

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