fix(leetcode): align solution with in-place API contract

- Return the number of unique elements instead of a sliced list
- Update notes to reflect the required interface contract
- Simplify the algorithm by removing unnecessary trimming
- Rewrite tests to mirror LeetCode's custom judge
This commit is contained in:
kiskaadee committed 2026-07-31 13:17:38 -05:00
1 parent 68d7286d4d
commit f0ec70c9fd
2 files changed
+45 -32

No files matched your search

@@ -81,7 +81,7 @@ i: ^
w: ^ w: ^
``` ```
The first digit is never a duplicate so we copy this value in place and move both of our pointers. Notice that we're overriding the actual value from `nums[0]`, although the new value is equal to the original. The first element is never considered a duplicate because there is no previous value to compare against, so we copy this value in place and move both of our pointers (or simply start iterating from the next index). Notice that we're overriding the actual value from `nums[0]`, even though the new value is equal to the original.
```Plaintext ```Plaintext
# Iteration 2 # Iteration 2
@@ -124,7 +124,7 @@ nums = [1, 2, 3, 4, 4]
w = 4 w = 4
``` ```
Being `w = 4` immediately after our last unique value, we can safely discard the values after `nums[w]`. At the end of the scan, `w` equals the number of unique elements. The first `w` positions already contain the required result. Since the problem explicitly states that every element after index `w - 1` can be ignored, no additional cleanup is necessary. We simply return `w`.
## Algorithm ## Algorithm
@@ -136,48 +136,49 @@ graph TD
input[/"Input: input[/"Input:
**nums**: List[int]"/] **nums**: List[int]"/]
init[Initialize read and write pointers] init[Initialize *Read* and *Write* pointers]
scan{Read pointer<br/>at end?} scan{*Read* pointer<br/>at end?}
unique{Current value<br/>different from previous?} unique{Current value<br/>different from previous?}
copy[Copy value to write position] copy[Copy value to *Write* position]
moveRead[Advance read] moveRead[Advance *Read*]
moveBoth[Advance read<br/>and write] moveBoth[Advance *Read*<br/>and *Write*]
trim[trim values from write pointer] END([Return Write])
END([Return nums])
START --> input --> init --> scan START --> input --> init --> scan
scan -- No --> unique scan -- No --> unique
unique -- Yes --> copy --> moveBoth --> scan unique -- Yes --> copy --> moveBoth --> scan
unique -- No --> moveRead --> scan unique -- No --> moveRead --> scan
scan -- Yes --> trim --> END scan -- Yes --> END
``` ```
## Implementation ## Implementation
A Python for loop saves us from manually managing the Read pointer. From there the implementation is fairly simple Since the read pointer advances sequentially through the array, a `for` loop naturally manages it. The write pointer is the only index we update manually.
```python ```python
from typing import List from typing import List
class Solution: class Solution:
def removeDuplicates(self, nums: List[int]) -> List[int]: def removeDuplicates(self, nums: List[int]) -> int:
if not nums: if not nums:
return [] return 0
w = 1 w = 1
for i in range(1, len(nums)): for i in range(1, len(nums)):
if nums[i] != nums[i -1]: if nums[i] != nums[i - 1]:
nums[w] = nums[i] nums[w] = nums[i]
w+=1 w += 1
return nums[:w]
return w
``` ```
@@ -188,4 +189,5 @@ class Solution:
- **In-Place Transformation via Two Pointers:** When memory constraints prohibit auxiliary data structures or copies, a read-write pointer pattern allows linear $O(n)$ time and $O(1)$ space restructuring by partitioning the array into processed (unique) and unprocessed regions. - **In-Place Transformation via Two Pointers:** When memory constraints prohibit auxiliary data structures or copies, a read-write pointer pattern allows linear $O(n)$ time and $O(1)$ space restructuring by partitioning the array into processed (unique) and unprocessed regions.
- **Interface vs. Implementation Constraints:** Standard platform signatures often specify returning an integer length to validate in-place modifications without allocating a new slice. However, returning a sliced view (`nums[:w]`) satisfies functional requirements when API contracts demand returning the modified collection directly. - **Interface vs. Implementation Constraints:** Solving the algorithm is only part of the task. Competitive programming platforms often define a precise interface that the implementation must satisfy. In this problem, the array is modified in place, while the function returns only the number of valid elements (`k`).
- **The specification is part of the problem.** Verify what must be returned before reasoning about implementation details.
@@ -2,33 +2,44 @@ from typing import List
class Solution: class Solution:
def removeDuplicates(self, nums: List[int]) -> List[int]: def removeDuplicates(self, nums: List[int]) -> int:
if not nums: if not nums:
return [] return 0
w = 1 w = 1
for i in range(1, len(nums)): for i in range(1, len(nums)):
if nums[i] != nums[i-1]: if nums[i] != nums[i-1]:
nums[w] = nums[i] nums[w] = nums[i]
w+=1 w+=1
return nums[:w] return w
if __name__ == "__main__": if __name__ == "__main__":
sol = Solution() sol = Solution()
test_cases = [ test_cases = [
([1, 1, 2], [1, 2]), # Simple duplicates # input, expected_k, expected_prefix
([0, 0, 1, 1, 1, 2, 2, 3, 3, 4], [0, 1, 2, 3, 4]), # Multiple duplicates ([1, 1, 2], 2, [1, 2]),
([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]), # No duplicates ([0, 0, 1, 1, 1, 2, 2, 3, 3, 4], 5, [0, 1, 2, 3, 4]),
([2, 2, 2, 2], [2]), # All duplicates ([1, 2, 3, 4, 5], 5, [1, 2, 3, 4, 5]),
([], []), # Empty list ([2, 2, 2, 2], 1, [2]),
([1], [1]) # Single element ([], 0, []),
([1], 1, [1]),
] ]
for i, (nums, expected) in enumerate(test_cases): for i, (nums, expected_k, expected_prefix) in enumerate(test_cases, start=1):
result = sol.removeDuplicates(nums[:]) nums = nums[:]
if result == expected: k = sol.removeDuplicates(nums)
print(f"Test {i+1} PASSED")
passed = (
k == expected_k and nums[:k] == expected_prefix
)
if passed:
print(f"Test {i} PASSED")
else: else:
print(f"Test {i+1} FAILED: Expected {expected}, got {result}") print(f"Test {i} FAILED")
print(f" expected k: {expected_k}, got k={k} ")
print(f" expected prefix: {expected_prefix}, got {nums[:k]}")
print(f" array state: {nums}")