|
1 |
| - |
2 | 1 | ---
|
3 |
| -id: sort-characters-by-frequency |
4 |
| -title: Sort Characters By Frequency (LeetCode) |
5 |
| -sidebar_label: 0451-Sort Characters By Frequency |
| 2 | +id: 4Sum-II |
| 3 | +title: 4Sum II (LeetCode) |
| 4 | +sidebar_label: 0454-4Sum-II |
6 | 5 | tags:
|
7 | 6 | - Hash Table
|
8 |
| - - String |
9 |
| - - Sorting |
10 |
| - - Heap(Priority Queue) |
11 |
| - - Bucket Sort |
12 |
| - - Counting |
13 |
| -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. |
| 7 | + - Arrays |
| 8 | +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." |
14 | 9 | sidebar_position: 0451
|
15 | 10 | ---
|
16 |
| - |
17 | 11 | ## Problem Description
|
18 | 12 |
|
19 |
| -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. |
| 13 | +Given four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that: |
| 14 | + |
| 15 | +0 <= i, j, k, l < n |
| 16 | +nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0 |
| 17 | + |
| 18 | +## Examples |
| 19 | + |
| 20 | +Example 1: |
| 21 | + |
| 22 | +Input: nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] |
| 23 | +Output: 2 |
| 24 | +Explanation: |
| 25 | +The two tuples are: |
| 26 | +1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 |
| 27 | +2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0 |
| 28 | +Example 2: |
| 29 | + |
| 30 | +Input: nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] |
| 31 | +Output: 1 |
| 32 | + |
| 33 | +## Constraints |
| 34 | + |
| 35 | +n == nums1.length |
| 36 | +n == nums2.length |
| 37 | +n == nums3.length |
| 38 | +n == nums4.length |
| 39 | +1 <= n <= 200 |
| 40 | +-228 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 228 |
| 41 | + |
| 42 | +## Approach |
| 43 | + |
| 44 | +Create an empty map called "mp" to store integer keys and their corresponding counts. |
| 45 | + |
| 46 | +Iterate over each element "k" in the "nuns3" vector. |
| 47 | + |
| 48 | +For each "k", iterate over each element "l" in the "nums4" vector. |
| 49 | + |
| 50 | +Add the sum of "k" and "l" as a key in the map "mp" and increment its count by 1. |
| 51 | + |
| 52 | +Initialize a variable "count" to 0. |
| 53 | + |
| 54 | +Iterate over each element "i" in the "nums1" vector. |
| 55 | + |
| 56 | +For each "i", iterate over each element "j" in the "nums2" vector. |
| 57 | + |
| 58 | +Find the value associated with the key -(i + j) in the map "mp" and add it to the "count". |
| 59 | + |
| 60 | +Return the value of "count" as the result. |
| 61 | + |
| 62 | +### Solution Code |
| 63 | + |
| 64 | +#### C++ |
| 65 | + |
| 66 | +```c++ |
| 67 | +class Solution { |
| 68 | +public: |
| 69 | + int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) { |
| 70 | + |
| 71 | + unordered_map<int,int> mp; |
| 72 | + |
| 73 | + |
| 74 | + for(int k : nums3) |
| 75 | + for(int l : nums4) |
| 76 | + mp[k + l]++; |
| 77 | + |
| 78 | + |
| 79 | + int count = 0; |
| 80 | + for(int i : nums1) |
| 81 | + for(int j : nums2) |
| 82 | + count += mp[-(i + j)]; |
| 83 | + |
| 84 | + return count; |
| 85 | + } |
| 86 | +}; |
| 87 | +``` |
| 88 | + |
| 89 | +#### java |
| 90 | +```java |
| 91 | + |
| 92 | +class Solution { |
| 93 | + public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { |
| 94 | + Map<Integer, Integer> map = new HashMap<>(); |
| 95 | + for (int n1 : nums1) { |
| 96 | + for (int n2 : nums2) { |
| 97 | + map.put(n1 + n2, map.getOrDefault(n1 + n2, 0) + 1); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + int count = 0; |
| 102 | + for (int n3 : nums3) { |
| 103 | + for (int n4 : nums4) { |
| 104 | + count += map.getOrDefault(-(n3 + n4), 0); |
| 105 | + } |
| 106 | + } |
| 107 | + return count; |
| 108 | + } |
| 109 | +} |
20 | 110 |
|
21 |
| -Return the sorted string. If there are multiple answers, return any of them. |
| 111 | +``` |
| 112 | +#### Python |
| 113 | +```Python |
| 114 | +class Solution: |
| 115 | + def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int: |
| 116 | + dc1=defaultdict(lambda:0) |
| 117 | + for a in nums1: |
| 118 | + for b in nums2: |
| 119 | + dc1[a+b]+=1 |
| 120 | + ans=0 |
| 121 | + for c in nums3: |
| 122 | + for d in nums4: |
| 123 | + ans+=dc1[-c-d] |
| 124 | + return ans |
22 | 125 |
|
23 |
| -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. |
| 126 | +``` |
24 | 127 |
|
| 128 | +## Conclusion |
25 | 129 |
|
| 130 | +- 1. Time complexity:O(n^2) |
| 131 | +- 2. Space complexity:O(n^2) |
0 commit comments