Skip to content

feat: add solutions to lc problem: No.454 #3034

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 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions solution/0400-0499/0454.4Sum II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,55 @@ func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) (ans int)

```ts
function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number {
const cnt: Map<number, number> = new Map();
const cnt: Record<number, number> = {};
for (const a of nums1) {
for (const b of nums2) {
const x = a + b;
cnt.set(x, (cnt.get(x) || 0) + 1);
cnt[x] = (cnt[x] || 0) + 1;
}
}
let ans = 0;
for (const c of nums3) {
for (const d of nums4) {
const x = c + d;
ans += cnt.get(-x) || 0;
ans += cnt[-x] || 0;
}
}
return ans;
}
```

#### Rust

```rust
use std::collections::HashMap;

impl Solution {
pub fn four_sum_count(
nums1: Vec<i32>,
nums2: Vec<i32>,
nums3: Vec<i32>,
nums4: Vec<i32>
) -> i32 {
let mut cnt = HashMap::new();
for &a in &nums1 {
for &b in &nums2 {
*cnt.entry(a + b).or_insert(0) += 1;
}
}
let mut ans = 0;
for &c in &nums3 {
for &d in &nums4 {
if let Some(&count) = cnt.get(&(0 - (c + d))) {
ans += count;
}
}
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
45 changes: 40 additions & 5 deletions solution/0400-0499/0454.4Sum II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@ The two tuples are:

<!-- solution:start -->

### Solution 1: HashMap
### Solution 1: Hash Table

Time complexity $O(n^2)$, Space complexity $O(n^2)$.
We can add the elements $a$ and $b$ in arrays $nums1$ and $nums2$ respectively, and store all possible sums in a hash table $cnt$, where the key is the sum of the two numbers, and the value is the count of the sum.

Then we iterate through the elements $c$ and $d$ in arrays $nums3$ and $nums4$, let $c+d$ be the target value, then the answer is the cumulative sum of $cnt[-(c+d)]$.

The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$, where $n$ is the length of the array.

<!-- tabs:start -->

Expand Down Expand Up @@ -146,24 +150,55 @@ func fourSumCount(nums1 []int, nums2 []int, nums3 []int, nums4 []int) (ans int)

```ts
function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number {
const cnt: Map<number, number> = new Map();
const cnt: Record<number, number> = {};
for (const a of nums1) {
for (const b of nums2) {
const x = a + b;
cnt.set(x, (cnt.get(x) || 0) + 1);
cnt[x] = (cnt[x] || 0) + 1;
}
}
let ans = 0;
for (const c of nums3) {
for (const d of nums4) {
const x = c + d;
ans += cnt.get(-x) || 0;
ans += cnt[-x] || 0;
}
}
return ans;
}
```

#### Rust

```rust
use std::collections::HashMap;

impl Solution {
pub fn four_sum_count(
nums1: Vec<i32>,
nums2: Vec<i32>,
nums3: Vec<i32>,
nums4: Vec<i32>
) -> i32 {
let mut cnt = HashMap::new();
for &a in &nums1 {
for &b in &nums2 {
*cnt.entry(a + b).or_insert(0) += 1;
}
}
let mut ans = 0;
for &c in &nums3 {
for &d in &nums4 {
if let Some(&count) = cnt.get(&(0 - (c + d))) {
ans += count;
}
}
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
26 changes: 26 additions & 0 deletions solution/0400-0499/0454.4Sum II/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::collections::HashMap;

impl Solution {
pub fn four_sum_count(
nums1: Vec<i32>,
nums2: Vec<i32>,
nums3: Vec<i32>,
nums4: Vec<i32>
) -> i32 {
let mut cnt = HashMap::new();
for &a in &nums1 {
for &b in &nums2 {
*cnt.entry(a + b).or_insert(0) += 1;
}
}
let mut ans = 0;
for &c in &nums3 {
for &d in &nums4 {
if let Some(&count) = cnt.get(&(0 - (c + d))) {
ans += count;
}
}
}
ans
}
}
6 changes: 3 additions & 3 deletions solution/0400-0499/0454.4Sum II/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number {
const cnt: Map<number, number> = new Map();
const cnt: Record<number, number> = {};
for (const a of nums1) {
for (const b of nums2) {
const x = a + b;
cnt.set(x, (cnt.get(x) || 0) + 1);
cnt[x] = (cnt[x] || 0) + 1;
}
}
let ans = 0;
for (const c of nums3) {
for (const d of nums4) {
const x = c + d;
ans += cnt.get(-x) || 0;
ans += cnt[-x] || 0;
}
}
return ans;
Expand Down
Loading