454. 4Sum II
Given four integer arrays
nums1
,nums2
,nums3
, andnums4
all of lengthn
, 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
Need Review
The question is a little bit confusing. We need to return how many times the sum of the 4 values from each array is 0.
Adding values of two values (A and B) storing them as keys and looking the sum of (C & D) in the same hashmap and finding the value associated with it.
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
int count = 0;
HashMap<Integer, Integer> map = new HashMap<>();
// sum the values from nums1 and nums2
for (int i : nums1) {
for (int j : nums2) {
// if (map.containsKey(i + j)) {
// map.put(i + j, map.get(i+j)+1);
// } else {
// map.put(i+j, 1);
// }
map.put(i + j, map.getOrDefault(i + j, 0) + 1);
}
}
for (int k : nums3) {
for (int p : nums4) {
//没找到就是0, 找到后value有几个数就可以加几次
count += map.getOrDefault(0 - k - p, 0);
}
}
return count;
}
}
TC: O(n^2)
SC: O(n)
383. Ransom Note
Given two strings
ransomNote
andmagazine
, returntrue
ifransomNote
can be constructed by using the letters frommagazine
andfalse
otherwise.Each letter in
magazine
can only be used once inransomNote
.
check whether each letter from the ransomNote string is in the magazine hash map.
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
HashMap<Character, Integer> map = new HashMap<>();
char[] charNote= ransomNote.toCharArray();
char[] charMag= magazine.toCharArray();
for (char i : charMag) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
for (char j : charNote) {
if (map.containsKey(j) && map.get(j) > 0) {
map.put(j, map.get(j) - 1);
} else if (!map.containsKey(j) || map.get(j) <= 0){
return false;
}
}
return true;
}
}
15. 3 sum
Given an integer array nums, return all the triplets
[nums[i], nums[j], nums[k]]
such thati != j
,i != k
, andj != k
, andnums[i] + nums[j] + nums[k] == 0
.Notice that the solution set must not contain duplicate triplets.
Need Review!
Three pointers point at a sorted array. Only when we test all the combinations for index 0 (left) then move left to left++.
Don't forget to deal with duplicates.
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> arr = new ArrayList<>();
// need to sort the array fist
Arrays.sort(nums);
// need to have three pointers
for (int left = 0; left < nums.length - 2; left++) {
if (nums[left] > 0) {
return arr;
}
// avoid duplicate left
if (left > 0 && nums[left] == nums[left - 1]) {
continue;
}
int mid = left + 1;
int right = nums.length - 1;
while (mid < right) {
int sum = nums[left] + nums[mid] + nums[right];
if (sum > 0) {
right --;
} else if (sum < 0) {
mid++;
} else {
arr.add(Arrays.asList(nums[left], nums[mid], nums[right]));
// avoid duplicates
while (right > mid && nums[right] == nums[right - 1]) {
right--;
}
while (mid < right && nums[mid] == nums[mid + 1]) {
mid++;
}
// still need to move right and mid out of duplicates
right --;
mid ++;
}
}
}
return arr;
}
}
TC: O(n^2)
SC: O(1)
18. 4SUM
Given an array
nums
ofn
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
, andd
are distinct.nums[a] + nums[b] + nums[c] + nums[d] == target
You may return the answer in any order.
similar solution as 3Sum
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> arr = new ArrayList<>();
Arrays.sort(nums);
for (int l = 0; l < nums.length - 3; l++) {
if (nums[l] > 0 && nums[l] >= target) {
return arr;
}
if (l > 0 && nums[l] == nums[l-1]) {
continue;
}
for (int i = l + 1; i < nums.length - 2; i++) {
if (i > l + 1 && nums[i] == nums[i-1]) {
continue;
}
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
// use long in case the space is not big enough
long sum = (long) nums[l] + nums[i] + nums[left] + nums[right];
if (sum == target) {
arr.add(Arrays.asList(nums[l], nums[i], nums[left], nums[right]));
while (right > left && nums[right] == nums[right - 1]) {
right--;
}
while (right > left && nums[left] == nums[left + 1]) {
left++;
}
right --;
left ++;
} else if (sum > target) {
right --;
} else {
left ++;
}
}
}
}
return arr;
}
}
TC: O(x^3)
SC: O(1)