-
-
Notifications
You must be signed in to change notification settings - Fork 155
Added Leetcode solution 0454-4Sum-II #1968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4b62751
Create 0454-4Sum-II
UtkarshBirla28 d618b15
Update 0454-4Sum-II
UtkarshBirla28 1b1153b
Rename 0454-4Sum-II to 0454 4Sum II
UtkarshBirla28 602be30
Rename 0454 4Sum II to 0454-4Sum-II
UtkarshBirla28 98f80df
Update 0454-4Sum-II
UtkarshBirla28 3fa7b9f
Update and rename 0454-4Sum-II to 0454-4Sum-II.md
UtkarshBirla28 0e0cd55
Update 0454-4Sum-II.md
UtkarshBirla28 5fd4d39
Update 0400-0499.md
UtkarshBirla28 a76447e
Merge branch 'main' into main
ajay-dhangar a316516
Update 0454-4Sum-II.md
UtkarshBirla28 6cb99e6
Merge branch 'main' into main
UtkarshBirla28 cb21926
Merge branch 'main' into main
ajay-dhangar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
--- | ||
id: 4Sum-II | ||
title: 4Sum II (LeetCode) | ||
sidebar_label: 0454-4Sum-II | ||
tags: | ||
- Hash Table | ||
- Arrays | ||
description: "Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string." | ||
sidebar_position: 0451 | ||
--- | ||
## Problem Description | ||
|
||
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 | ||
|
||
## Examples | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replace |
||
-228 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 228 | ||
ajay-dhangar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Approach | ||
|
||
Create an empty map called "mp" to store integer keys and their corresponding counts. | ||
|
||
Iterate over each element "k" in the "nuns3" vector. | ||
|
||
For each "k", iterate over each element "l" in the "nums4" vector. | ||
|
||
Add the sum of "k" and "l" as a key in the map "mp" and increment its count by 1. | ||
|
||
Initialize a variable "count" to 0. | ||
|
||
Iterate over each element "i" in the "nums1" vector. | ||
|
||
For each "i", iterate over each element "j" in the "nums2" vector. | ||
|
||
Find the value associated with the key -(i + j) in the map "mp" and add it to the "count". | ||
|
||
Return the value of "count" as the result. | ||
|
||
### Solution Code | ||
|
||
#### C++ | ||
|
||
```c++ | ||
class Solution { | ||
public: | ||
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) { | ||
|
||
unordered_map<int,int> mp; | ||
|
||
|
||
for(int k : nums3) | ||
for(int l : nums4) | ||
mp[k + l]++; | ||
|
||
|
||
int count = 0; | ||
for(int i : nums1) | ||
for(int j : nums2) | ||
count += mp[-(i + j)]; | ||
|
||
return count; | ||
} | ||
}; | ||
``` | ||
|
||
#### java | ||
```java | ||
|
||
class Solution { | ||
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { | ||
Map<Integer, Integer> map = new HashMap<>(); | ||
for (int n1 : nums1) { | ||
for (int n2 : nums2) { | ||
map.put(n1 + n2, map.getOrDefault(n1 + n2, 0) + 1); | ||
} | ||
} | ||
|
||
int count = 0; | ||
for (int n3 : nums3) { | ||
for (int n4 : nums4) { | ||
count += map.getOrDefault(-(n3 + n4), 0); | ||
} | ||
} | ||
return count; | ||
} | ||
} | ||
|
||
``` | ||
#### Python | ||
```Python | ||
class Solution: | ||
def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int: | ||
dc1=defaultdict(lambda:0) | ||
for a in nums1: | ||
for b in nums2: | ||
dc1[a+b]+=1 | ||
ans=0 | ||
for c in nums3: | ||
for d in nums4: | ||
ans+=dc1[-c-d] | ||
return ans | ||
|
||
``` | ||
|
||
## Conclusion | ||
|
||
- 1. Time complexity:O(n^2) | ||
- 2. Space complexity:O(n^2) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.