Mercury: 4Sum
harbor run -t elfsong/mercury-4sumharbor download elfsong/mercury-4sumGiven 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⁹ <= nums[i] <= 10⁹
-10⁹ <= target <= 10⁹
Implement Solution.fourSum in /app/solution.py using Python 3.12.
Keep the supplied class and method signature. The file is the only submitted artifact.
The evaluator instantiates Solution and invokes this method with the problem's inputs;
do not read standard input or write a command-line program.
class Solution(object):
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
The standard library, lctk 0.0.6 (LeetCode tree/list helpers), sortedcontainers 2.4.0, NumPy 2.4.3, SciPy 1.17.1, and six 1.17.0 are available. Standard LeetCode imports, including List, Optional, TreeNode and ListNode, are provided by the starter file.
Your solution must pass every hidden test. For a correct solution, the evaluator also measures CPU runtime over 128 repetitions per test using independent copies of the inputs, and compares it with passing reference solutions measured in the same environment. Aim for an efficient algorithm. Wrong solutions receive zero efficiency credit.