Skip to content
LC-0454 Medium LeetCode

454. 4Sum II

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 58% Topics: Array, Hash Table
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# Time:  O(n^2)
# Space: O(n^2)

import collections


class Solution(object):
    def fourSumCount(self, A, B, C, D):
        """
        :type A: List[int]
        :type B: List[int]
        :type C: List[int]
        :type D: List[int]
        :rtype: int
        """
        A_B_sum = collections.Counter(a+b for a in A for b in B)
        return sum(A_B_sum[-c-d] for c in C for d in D)

Solution from kamyu104/LeetCode-Solutions · MIT

Similar questions