Thursday, July 2, 2015

Leetcode 15: 3 sum/ Leetcode 16: 3 sum closest

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
For example, given array S = {-1 0 1 2 -1 -4},

    A solution set is:
    (-1, 0, 1)
    (-1, -1, 2)


3 sum 题目, 首先想到的是转化为2sum的题目,时间复杂度是O(n^2),并且看要求的结果集,如果是element集合,需要先对array进行sort,在整个算法过程中,对相同的element进行deduplicate。如果是index集合,则不能进行sort,因为这样index会乱,并且不用对element deduplicate。
为了在程序运行的时候效率达到最高,需要在每一处指针移动的时候考虑到deduplicate的运算。在3sum中, 一共是3个指针在移动,i指针控制第一个元素,start 和end指针都是在2sum里面出现的指针,每次这三个指针移动的时候,都要看current element是否和下一个element相等,如果相等移动指针。
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> rst;
        //snaity check
        if (nums.size() < 3) return rst;
        sort(nums.begin(), nums.end());
        for (int i = 0; i < nums.size()-2; i++) {
            //deduplicate : 1
            if (i > 0 && nums[i] == nums[i-1]) continue;
            int target = 0 - nums[i];
            int start = i +1;
            int end = nums.size()-1;
            while (start < end) {
                vector<int> buf;
                if (nums[start] + nums[end] > target){
                    end--;
                    //deduplicate:  2
                    if (nums[end] == nums[end+1]) end--;
                }
                else if (nums[start] + nums[end] < target){
                    start++;
                    //deduplicate: 3
                    if (nums[start] == nums[start-1]) start++;
                }
                else {
                    buf.push_back(nums[i]);
                    buf.push_back(nums[start]);
                    buf.push_back(nums[end]);
                    rst.push_back(buf);
                    start++;
                    end--;
                    //deduplicate: 4
                    while (start < end && nums[start] == nums[start-1]) start++;
                    while (start < end && nums[end] == nums[end+1]) end--;
                }
            }
        }
        return rst;
    }
};
time complexity: O(n^2)
space complexity: O(n)

3Sum closest:
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
    For example, given array S = {-1 2 1 -4}, and target = 1.

    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). 
这一题和求3sum closest相似,但是在每次移动指针之前要找到此时的最近差值,minimal closet。  
class Solution {
public:
    int threeSumClosest(vector<int>& nums, int target) {
        int rst = 0;
        //sanity check
        if (nums.size() < 3) return 0;
        sort(nums.begin(), nums.end());
        int closest = INT_MAX;
        for (int i = 0; i < nums.size()-2; i++) {
            //deduplicate1
            if (i > 0 && nums[i] == nums[i-1]) continue;
            int newtarget = target-nums[i];
            int start = i+1;
            int end = nums.size()-1;
            while (start < end) {
                if (nums[start] + nums[end] == newtarget) return target;
                if (abs(nums[start] + nums[end] + nums[i] - target) < closest) {
                    closest = abs(nums[start] + nums[end] + nums[i] - target);
                    rst = nums[start] + nums[end] + nums[i];
                }
                if (nums[start] + nums[end] < newtarget) {
                    start++;
                    if (nums[start] == nums[start-1]) start++;
                } else {
                    end--;
                    if (nums[end] == nums[end+1]) end--;
                }
            }
        }
        return rst;
    }
};
time complexity: O (n^2)
space complexity: O(1)

No comments:

Post a Comment