docs(leetcode): solve Remove Element with two pointers
- Add solution for LeetCode #27 (Remove Element) - Document the read/write pointer approach - Add complexity analysis and takeaways - Add tests matching LeetCode's custom judge - Add complexity analysis to #26 notes
This commit is contained in:
1 parent
ae3a42cda6
commit
7bc7cb7701
3 files changed
+170
No files matched your search
@@ -183,6 +183,12 @@ class Solution:
|
||||
```
|
||||
|
||||
|
||||
### Complexity Analysis
|
||||
|
||||
- **Time Complexity:** $O(n)$, where $n$ is the number of elements in the array `nums`. The algorithm scans the array exactly once using a single `for` loop. Each comparison and assignment operation runs in constant time $O(1)$.
|
||||
|
||||
- **Space Complexity:** $O(1)$. The operation modifies the array in place using a fixed number of scalar pointers (`w` and the loop variable `i`), requiring no auxiliary data structures or memory scaling with input size.
|
||||
|
||||
## Takeaways
|
||||
|
||||
- **Mutation and Iteration Hazards:** Mutating a collection (via `pop()` or `remove()`) while iterating over its index or iterator shifts subsequent elements unpredictably. This invalidates remaining loop bounds or skips elements unless working on a copy.
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
# [27. Remove Element](https://leetcode.com/problems/remove-element/)
|
||||
|
||||
Difficulty: Easy
|
||||
|
||||
## Problem Statement
|
||||
|
||||
Given an integer array `nums` and an integer `val`, remove all occurrences of `val` in `nums` [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm). The order of the elements may be changed. Then return _the number of elements in_ `nums` _which are not equal to_ `val`.
|
||||
|
||||
Consider the number of elements in `nums` which are not equal to `val` be `k`, to get accepted, you need to do the following things:
|
||||
|
||||
- Change the array `nums` such that the first `k` elements of `nums` contain the elements which are not equal to `val`. The remaining elements of `nums` are not important as well as the size of `nums`.
|
||||
- Return `k`.
|
||||
|
||||
**Custom Judge:**
|
||||
|
||||
The judge will test your solution with the following code:
|
||||
|
||||
int[] nums = [...]; // Input array
|
||||
int val = ...; // Value to remove
|
||||
int[] expectedNums = [...]; // The expected answer with correct length.
|
||||
// It is sorted with no values equaling val.
|
||||
|
||||
int k = removeElement(nums, val); // Calls your implementation
|
||||
|
||||
assert k == expectedNums.length;
|
||||
sort(nums, 0, k); // Sort the first k elements of nums
|
||||
for (int i = 0; i < actualLength; i++) {
|
||||
assert nums[i] == expectedNums[i];
|
||||
}
|
||||
|
||||
If all assertions pass, then your solution will be **accepted**.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
**Input:** nums = [3,2,2,3], val = 3
|
||||
**Output:** 2, nums = [2,2,_,_]
|
||||
**Explanation:** Your function should return k = 2, with the first two elements of nums being 2.
|
||||
It does not matter what you leave beyond the returned k (hence they are underscores).
|
||||
|
||||
**Example 2:**
|
||||
|
||||
**Input:** nums = [0,1,2,2,3,0,4,2], val = 2
|
||||
**Output:** 5, nums = [0,1,4,0,3,_,_,_]
|
||||
**Explanation:** Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
|
||||
Note that the five elements can be returned in any order.
|
||||
It does not matter what you leave beyond the returned k (hence they are underscores).
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `0 <= nums.length <= 100`
|
||||
- `0 <= nums[i] <= 50`
|
||||
- `0 <= val <= 100`
|
||||
|
||||
---
|
||||
## Intuition
|
||||
|
||||
Unlike the previous problem, the array is **not sorted**, so we cannot detect unwanted elements by comparing neighbors. Instead, we are given the exact value to remove.
|
||||
|
||||
The same two-pointer strategy still applies:
|
||||
|
||||
- **Read pointer (`i`)** scans every element.
|
||||
- **Write pointer (`w`)** tracks where the next valid element should be placed.
|
||||
|
||||
Whenever the read pointer encounters a value **different from `val`**, that value is copied to the write position, and both pointers advance. Otherwise, only the read pointer moves forward.
|
||||
|
||||
At the end of the scan, `w` equals the number of elements that are not equal to `val`. The first `w` positions contain the required values, while everything beyond `w - 1` can be ignored according to the problem statement.
|
||||
|
||||
## Algorithm
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
|
||||
START((removeElement))
|
||||
|
||||
input[/"Input:<br/>nums, val"/]
|
||||
|
||||
init[Initialize Read and Write pointers]
|
||||
|
||||
scan{Read pointer<br/>at end?}
|
||||
|
||||
keep{"nums[i] != val?"}
|
||||
|
||||
copy[Copy value to Write position]
|
||||
|
||||
moveRead[Advance Read]
|
||||
|
||||
moveBoth[Advance Read<br/>and Write]
|
||||
|
||||
END([Return Write])
|
||||
|
||||
START --> input --> init --> scan
|
||||
scan -- No --> keep
|
||||
keep -- Yes --> copy --> moveBoth --> scan
|
||||
keep -- No --> moveRead --> scan
|
||||
scan -- Yes --> END
|
||||
````
|
||||
|
||||
## Implementation
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def removeElement(self, nums: List[int], val: int) -> int:
|
||||
w = 0
|
||||
|
||||
for i in range(len(nums)):
|
||||
if nums[i] != val:
|
||||
nums[w] = nums[i]
|
||||
w += 1
|
||||
|
||||
return w
|
||||
```
|
||||
|
||||
## Complexity Analysis
|
||||
|
||||
- **Time Complexity:** $O(n)$, where $n$ is the number of elements in the array `nums`. The algorithm iterates through the array exactly once. Each comparison and assignment executes in constant time $O(1)$.
|
||||
|
||||
- **Space Complexity:** $O(1)$. The transformation is performed entirely in place using a fixed set of scalar variables (`w` and the loop iterator), requiring no auxiliary memory scaling with the input size.
|
||||
## Takeaways
|
||||
|
||||
- The two-pointer pattern is not limited to sorted arrays. It also works as a general **filtering** technique for in-place array transformations.
|
||||
|
||||
- The write pointer always represents both the position of the next valid element and the number of valid elements processed so far.
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
from typing import List
|
||||
|
||||
|
||||
class Solution:
|
||||
def removeElement(self, nums: List[int], val: int) -> int:
|
||||
w = 0
|
||||
|
||||
for i in range(len(nums)):
|
||||
if nums[i] != val:
|
||||
nums[w] = nums[i]
|
||||
w += 1
|
||||
|
||||
return w
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sol = Solution()
|
||||
|
||||
test_cases = [
|
||||
([3, 2, 2, 3], 3, 2, [2, 2]),
|
||||
([0, 1, 2, 2, 3, 0, 4, 2], 2, 5, [0, 1, 3, 0, 4]),
|
||||
([1], 1, 0, []),
|
||||
([1], 2, 1, [1]),
|
||||
([], 1, 0, []),
|
||||
]
|
||||
for i, (nums, val, expected_k, expected_prefix) in enumerate(test_cases):
|
||||
original_nums = nums[:]
|
||||
nums_copy = nums[:]
|
||||
k = sol.removeElement(nums_copy, val)
|
||||
|
||||
prefix = sorted(nums_copy[:k])
|
||||
expected_sorted = sorted(expected_prefix)
|
||||
|
||||
passed = k == expected_k and prefix == expected_sorted
|
||||
|
||||
if passed:
|
||||
print(f"Test {i+1} PASSED")
|
||||
else:
|
||||
print(
|
||||
f"Test {i+1} FAILED: input={original_nums}, val={val}, expected_k={expected_k}, got_k={k}, expected_prefix={expected_prefix}, got_prefix={nums_copy[:k]}"
|
||||
)
|
||||
Reference in new issue
Block a user