..

18. 4Sum

Problem Description

Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:

0 <= a, b, c, d < n
a, b, c, and d are distinct.
nums[a] + nums[b] + nums[c] + nums[d] == target

You may return the answer in any order.

Example 1:

Input: nums = [1,0,-1,0,-2,2], target = 0
Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

Example 2:

Input: nums = [2,2,2,2,2], target = 8
Output: [[2,2,2,2]]

Constraints:

1 <= nums.length <= 200
-10^9 <= nums[i] <= 10^9
-10^9 <= target <= 10^9

Solution

  • DFS backtracking
  • Two Pointers

Python


class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        self.res = []
        nums.sort()
        self.dfs(nums, target, 4, [])

        return self.res


    def dfs(self, nums, target, k, curr):
        if k == 0 and target == 0:
            self.res.append(curr[:])
            return

        if len(nums) == 0 or nums[0] * k > target or target > nums[-1] * k:
            return

        for i in range(len(nums)):
            if i == 0 or nums[i - 1] != nums[i]:
                curr.append(nums[i])
                self.dfs(nums[i + 1:], target - nums[i], k - 1, curr)
                curr.pop()

Java

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> rst = new ArrayList<List<Integer>>();
        Arrays.sort(nums);

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

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

                int left = j + 1;
                int right = nums.length - 1;
                while (left < right) {
                    int sum = nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum < target) {
                        left++;
                    } else if (sum > target) {
                        right--;
                    } else {
                        ArrayList<Integer> tmp = new ArrayList<Integer>();
                        tmp.add(nums[i]);
                        tmp.add(nums[j]);
                        tmp.add(nums[left]);
                        tmp.add(nums[right]);
                        rst.add(tmp);
                        left++;
                        right--;
                        while (left < right && nums[left] == nums[left - 1]) {
                            left++;
                        }
                        while (left < right && nums[right] == nums[right + 1]) {
                            right--;
                        }
                    }
                }
            }
        }

        return rst;
    }
}

Complexity Analysis

  • Time Complexity
    • O(n^3) - sort and two sum
  • Space Complexity
    • O(4n)