From 4b62751f1830b54ede11f95df6d40800d109de30 Mon Sep 17 00:00:00 2001 From: Utkarsh Birla <96072228+UtkarshBirla28@users.noreply.github.com> Date: Sun, 23 Jun 2024 23:15:53 +0530 Subject: [PATCH 1/9] Create 0454-4Sum-II --- dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II diff --git a/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II b/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II new file mode 100644 index 000000000..ebb806712 --- /dev/null +++ b/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II @@ -0,0 +1,12 @@ +--- +id: 4Sum-II +title: 4Sum II (LeetCode) +sidebar_label:0454-4Sum-II +tags: + - Hash Table + - Array + +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 +--- + From d618b1536dff2ebc07aaecc74691ae8b4f308951 Mon Sep 17 00:00:00 2001 From: Utkarsh Birla <96072228+UtkarshBirla28@users.noreply.github.com> Date: Sun, 23 Jun 2024 23:16:35 +0530 Subject: [PATCH 2/9] Update 0454-4Sum-II --- dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II b/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II index ebb806712..8b1378917 100644 --- a/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II +++ b/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II @@ -1,12 +1 @@ ---- -id: 4Sum-II -title: 4Sum II (LeetCode) -sidebar_label:0454-4Sum-II -tags: - - Hash Table - - Array -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 ---- - From 1b1153b6afa6b705388f09484cb2ef4233f2e25d Mon Sep 17 00:00:00 2001 From: Utkarsh Birla <96072228+UtkarshBirla28@users.noreply.github.com> Date: Sun, 23 Jun 2024 23:17:14 +0530 Subject: [PATCH 3/9] Rename 0454-4Sum-II to 0454 4Sum II --- .../lc-solutions/0400-0499/{0454-4Sum-II => 0454 4Sum II} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename dsa-solutions/lc-solutions/0400-0499/{0454-4Sum-II => 0454 4Sum II} (100%) diff --git a/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II b/dsa-solutions/lc-solutions/0400-0499/0454 4Sum II similarity index 100% rename from dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II rename to dsa-solutions/lc-solutions/0400-0499/0454 4Sum II From 602be30bcb01063a8bcfbee471d8b5e292e544e5 Mon Sep 17 00:00:00 2001 From: Utkarsh Birla <96072228+UtkarshBirla28@users.noreply.github.com> Date: Sun, 23 Jun 2024 23:19:47 +0530 Subject: [PATCH 4/9] Rename 0454 4Sum II to 0454-4Sum-II --- .../lc-solutions/0400-0499/{0454 4Sum II => 0454-4Sum-II} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename dsa-solutions/lc-solutions/0400-0499/{0454 4Sum II => 0454-4Sum-II} (100%) diff --git a/dsa-solutions/lc-solutions/0400-0499/0454 4Sum II b/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II similarity index 100% rename from dsa-solutions/lc-solutions/0400-0499/0454 4Sum II rename to dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II From 98f80dfbe6ec3acb6ac62e00ddef5cf8b3aca3b2 Mon Sep 17 00:00:00 2001 From: Utkarsh Birla <96072228+UtkarshBirla28@users.noreply.github.com> Date: Sun, 23 Jun 2024 23:21:26 +0530 Subject: [PATCH 5/9] Update 0454-4Sum-II --- .../lc-solutions/0400-0499/0454-4Sum-II | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II b/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II index 8b1378917..e429a8f7a 100644 --- a/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II +++ b/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II @@ -1 +1,25 @@ +--- +id: sort-characters-by-frequency +title: Sort Characters By Frequency (LeetCode) +sidebar_label: 0451-Sort Characters By Frequency +tags: + - Hash Table + - String + - Sorting + - Heap(Priority Queue) + - Bucket Sort + - Counting +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 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. + +Return the sorted string. If there are multiple answers, return any of them. + +Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. + +## Assign From 3fa7b9f2ef4bde7c3c3f034a78c4871e13bf00a0 Mon Sep 17 00:00:00 2001 From: Utkarsh Birla <96072228+UtkarshBirla28@users.noreply.github.com> Date: Sun, 23 Jun 2024 23:23:26 +0530 Subject: [PATCH 6/9] Update and rename 0454-4Sum-II to 0454-4Sum-II.md --- .../lc-solutions/0400-0499/{0454-4Sum-II => 0454-4Sum-II.md} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename dsa-solutions/lc-solutions/0400-0499/{0454-4Sum-II => 0454-4Sum-II.md} (98%) diff --git a/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II b/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II.md similarity index 98% rename from dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II rename to dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II.md index e429a8f7a..19d4ffb8d 100644 --- a/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II +++ b/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II.md @@ -22,4 +22,4 @@ Return the sorted string. If there are multiple answers, return any of them. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. -## Assign + From 0e0cd55d20edb64505b6a09ad55d30c926874e16 Mon Sep 17 00:00:00 2001 From: Utkarsh Birla <96072228+UtkarshBirla28@users.noreply.github.com> Date: Sun, 23 Jun 2024 23:38:11 +0530 Subject: [PATCH 7/9] Update 0454-4Sum-II.md --- .../lc-solutions/0400-0499/0454-4Sum-II.md | 134 ++++++++++++++++-- 1 file changed, 120 insertions(+), 14 deletions(-) diff --git a/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II.md b/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II.md index 19d4ffb8d..6f16eb653 100644 --- a/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II.md +++ b/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II.md @@ -1,25 +1,131 @@ - --- -id: sort-characters-by-frequency -title: Sort Characters By Frequency (LeetCode) -sidebar_label: 0451-Sort Characters By Frequency +id: 4Sum-II +title: 4Sum II (LeetCode) +sidebar_label: 0454-4Sum-II tags: - Hash Table - - String - - Sorting - - Heap(Priority Queue) - - Bucket Sort - - Counting -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. + - 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 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. +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 +-228 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 228 + +## 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& nums1, vector& nums2, vector& nums3, vector& nums4) { + + unordered_map 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 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; + } +} -Return the sorted string. If there are multiple answers, return any of them. +``` +#### 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 -Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. +``` +## Conclusion +- 1. Time complexity:O(n^2) +- 2. Space complexity:O(n^2) From 5fd4d391ea4e58bb9249531e9697304b22bc39f2 Mon Sep 17 00:00:00 2001 From: Utkarsh Birla <96072228+UtkarshBirla28@users.noreply.github.com> Date: Sun, 23 Jun 2024 23:40:17 +0530 Subject: [PATCH 8/9] Update 0400-0499.md --- dsa-problems/leetcode-problems/0400-0499.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dsa-problems/leetcode-problems/0400-0499.md b/dsa-problems/leetcode-problems/0400-0499.md index cf43992cf..a316295a9 100644 --- a/dsa-problems/leetcode-problems/0400-0499.md +++ b/dsa-problems/leetcode-problems/0400-0499.md @@ -338,7 +338,7 @@ export const problems = [ "problemName": "454. 4Sum II", "difficulty": "Medium", "leetCodeLink": "https://leetcode.com/problems/4sum-ii", - "solutionLink": "#" + "solutionLink": "/dsa-solutions/lc-solutions/0400-0499/4Sum-II" }, { "problemName": "455. Assign Cookies", @@ -619,4 +619,4 @@ export const problems = [ collectionLink="https://leetcode.com/study-plan/programming-skills" /> -Now, you can see the list of problems in a table format. You can click on the problem link to view the problem on the LeetCode website. You can also click on the solution link to view the solution of the problem. \ No newline at end of file +Now, you can see the list of problems in a table format. You can click on the problem link to view the problem on the LeetCode website. You can also click on the solution link to view the solution of the problem. From a3165162648c99e620d34d16be527b828afd3c11 Mon Sep 17 00:00:00 2001 From: Utkarsh Birla <96072228+UtkarshBirla28@users.noreply.github.com> Date: Mon, 24 Jun 2024 09:01:55 +0530 Subject: [PATCH 9/9] Update 0454-4Sum-II.md --- .../lc-solutions/0400-0499/0454-4Sum-II.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II.md b/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II.md index 6f16eb653..3e95bc229 100644 --- a/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II.md +++ b/dsa-solutions/lc-solutions/0400-0499/0454-4Sum-II.md @@ -12,7 +12,7 @@ sidebar_position: 0451 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 +`0 <= i`, `j`, `k`, `l < n` nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0 ## Examples @@ -25,6 +25,7 @@ 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] @@ -32,12 +33,12 @@ 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 +- 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` ## Approach