-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathremove-duplicates-from-sorted-array-ii.rs
54 lines (52 loc) · 1.78 KB
/
remove-duplicates-from-sorted-array-ii.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 80. Remove Duplicates from Sorted Array II
// 🟠 Medium
//
// https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/h
//
// Tags: Array - Two Pointers
struct Solution;
impl Solution {
// Generic solution that leaves a maximum of n instances of the same element
// in a given sorted input array. The solution uses two pointers, one to read
// and one to write, to iterate over the input, when the element over the read
// pointer is the same as the one n indexes back from the write pointer, it
// skips it, otherwise, we know that we have less than n instances of that
// element in the current output array, and we can add the current one.
//
// Time complexity: O(n) - We visit each value in the input once.
// Space complexity: O(1) - We only use pointers.
//
// Runtime 0 ms Beats 100%
// Memory 2.1 MB Beats 84.38%
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
// The maximum number of duplicates allowed.
const MAX_DUPLICATES: usize = 2;
// Nothing to do.
if nums.len() <= MAX_DUPLICATES {
return nums.len() as i32;
}
// A write pointer.
let mut w = MAX_DUPLICATES;
for r in MAX_DUPLICATES..nums.len() {
// Check if the value two positions back is the same.
if nums[r] != nums[w - MAX_DUPLICATES] {
nums[w] = nums[r];
w += 1;
}
}
w as i32
}
}
// Tests.
fn main() {
let tests = [
(vec![1], 1),
(vec![1, 1], 2),
(vec![1, 1, 1, 2, 2, 3], 5),
(vec![0, 0, 1, 1, 1, 1, 2, 3, 3], 7),
];
for test in tests {
assert_eq!(Solution::remove_duplicates(&mut Vec::from(test.0)), test.1);
}
println!("All tests passed!")
}