g0401_0500.s0454_4sum_ii.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0401_0500.s0454_4sum_ii;
// #Medium #Top_Interview_Questions #Array #Hash_Table
// #2022_07_18_Time_133_ms_(95.19%)_Space_42.4_MB_(88.53%)
import java.util.HashMap;
import java.util.Map;
/**
* 454 - 4Sum II\.
*
* Medium
*
* Given four integer arrays `nums1`, `nums2`, `nums3`, and `nums4` all of length `n`, return the number of tuples `(i, j, k, l)` such that:
*
* * `0 <= i, j, k, l < n`
* * `nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0`
*
* **Example 1:**
*
* **Input:** nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
*
* **Output:** 2
*
* **Explanation:** The two tuples are:
*
* 1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
*
* 2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
*
* **Example 2:**
*
* **Input:** nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
*
* **Output:** 1
*
* **Constraints:**
*
* * `n == nums1.length`
* * `n == nums2.length`
* * `n == nums3.length`
* * `n == nums4.length`
* * `1 <= n <= 200`
* * -228 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 228
**/
public class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
int count = 0;
Map map = new HashMap<>();
for (int k : nums3) {
for (int i : nums4) {
int sum = k + i;
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
}
for (int k : nums1) {
for (int i : nums2) {
int m = -(k + i);
count += map.getOrDefault(m, 0);
}
}
return count;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy