Monday, January 9, 2017

Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

/*
We don't care about what we leave beyond new length , hence we can use a pointer to keep track of the position and update the array.

time: O(n), space:O(1)
*/
public class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int slow = 1;
        for (int fast = 1; fast < nums.length; fast++) {
            if (nums[fast] != nums[slow - 1]) {
                nums[slow] = nums[fast];
                slow++;
            }
        }
        return slow;
    }
}


/*
Follow up1: what if duplicates are allowed at most twice?
For example,
Given sorted array nums = [1,1,1,2,2,3],
Your function should return length = 5, with the first five elements of nums being 1122 and 3. It doesn't matter what you leave beyond the new length.
*/
//time: O(n), space:O(1)
public class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int slow = 2;
        for (int fast = 2; fast < nums.length; fast++) {
            if (nums[fast] > nums[slow - 2]) {
                nums[slow] = nums[fast];
                slow++;
            }
        }
        return slow;
    }
}


/*
Follow up2: what if duplicates are allowed at most k times(k>0)?
For example,
Given k = 3 and sorted array nums = [1,1,2,4,4,4,4,4]
Your function should return length = 6, with the first six elements of nums being 11, 2, 4, 4 and 4. It doesn't matter what you leave beyond the new length.
*/
//time: O(n), space:O(1)
public class Solution {
    public int removeDuplicates(int[] nums, int k) {
        if (nums == null || nums.length == 0 || k <= 0) {
            return 0;
        }
        int slow = k;
        for (int fast = k; fast < nums.length; fast++) {
            if (nums[fast] > nums[slow - k]) {
                nums[slow] = nums[fast];
                slow++;
            }
        }
        return slow;
    }
}


/*
Follow up3: what if duplicates are not allowed?
For example,
Given sorted array nums = [1,1,2,3,4,4]
Your function should return length = 2, with the first two elements of nums being and 3. It doesn't matter what you leave beyond the new length.
*/
//time: O(n), space:O(1)
public class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int slow = 0;
        int fast = 0;
        int ind = 0;
        for (fast = 0; fast < nums.length; fast++) {
            if (nums[fast] > nums[slow]) {
                if (fast == slow + 1) {
                    nums[ind++] = nums[slow];
                } 
                slow = fast;
            }
        }
        if (slow + 1 == fast) {
            return ind + 1;
        }
        return ind;
    }

}

No comments:

Post a Comment