4Sum

  • leetcode 18
  • Medium
  • Tags: Array, HashTable, Two Pointers
  • Similar Problems: Two Sum, 3Sum
Description

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

  • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
  • The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.

A solution set is:
(-1,  0, 0, 1)
(-2, -1, 1, 2)
(-2,  0, 0, 2)
Previous Thinking
  • similar structure used in 3Sum
  • looking for more improvement
C++ Solution
class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int>> res;
        if(nums.size() < 4) return res;
        sort(nums.begin(),nums.end());

        int n = nums.size();
        for(int i = 0; i< nums.size() - 3; i++){
            if(i > 0 && nums[i] == nums[i - 1]) continue;

            if(nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) break;
            if(nums[i] + nums[n - 3] + nums[n - 2] + nums[n - 1] < target) continue;

            for(int j = i + 1; j < nums.size() - 2; j++){
                if(j > i + 1 && nums[j] == nums[j - 1]) continue;

                if(nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) break;
                if(nums[i] + nums[j] + nums[n - 2] + nums[n - 1] < target) continue;

                int k = j + 1;
                int l = nums.size() - 1;

                while(k < l){
                    int sum = nums[i] + nums[j] + nums[k] + nums[l];
                    if(sum == target){
                        res.push_back({nums[i],nums[j],nums[k],nums[l]});
                        while( k < l && nums[k++] == nums[k]);
                        while(k < l && nums[l--] == nums[l]);
                    }else if(sum < target){
                        while(k < l && nums[k++] == nums[k]);
                    }else{
                        while(k < l && nums[l--] == nums[l]);
                    }
                }
            }
        }
        return res;
    }
};
Java Solution
public class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new ArrayList<>();
        if(nums.length < 4) return res;

        Arrays.sort(nums);

        int n = nums.length;
        for(int i = 0; i< nums.length - 3; i++){
            if(i > 0 && nums[i] == nums[i - 1]) continue;

            if(nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) break;
            if(nums[i] + nums[n - 3] + nums[n - 2] + nums[n - 1] < target) continue;

            for(int j = i + 1; j < nums.length - 2; j++){
                if(j > i + 1 && nums[j] == nums[j - 1]) continue;

                if(nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) break;
                if(nums[i] + nums[j] + nums[n - 2] + nums[n - 1] < target) continue;

                int k = j + 1;
                int l = nums.length - 1;

                while(k < l){
                    int sum = nums[i] + nums[j] + nums[k] + nums[l];
                    if(sum == target){
                        res.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l]));

                        while( k < l && nums[k++] == nums[k]);
                        while(k < l && nums[l--] == nums[l]);
                    }else if(sum < target){
                        while(k < l && nums[k++] == nums[k]);
                    }else{
                        while(k < l && nums[l--] == nums[l]);
                    }
                }
            }
        }
        return res;
    }
}
Post Thinking

There are different to ways to reduce the dimensions of this problem. HashMap is an intuitive method. However, I find the overuse of HashMap will make the solution very complex. It is because that We need to examine the order of the 4 index to make sure uniqueness of the solution.

The solution I gave there is just add a for loop. It inherit the structue of 3Sum problem. Adding some smart prune will optimize the solution dramatically, which beats the 98% solution post in Leetcode.

results matching ""

    No results matching ""