Skip to content

feat: add solutions to lc problems: No.0349,2285 #3171

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 28, 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
77 changes: 21 additions & 56 deletions solution/0300-0399/0349.Intersection of Two Arrays/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ func intersection(nums1 []int, nums2 []int) (ans []int) {
}
```

#### TypeScript

```ts
function intersection(nums1: number[], nums2: number[]): number[] {
const s = new Set(nums1);
return [...new Set(nums2.filter(x => s.has(x)))];
}
```

#### JavaScript

```js
Expand All @@ -143,18 +152,8 @@ func intersection(nums1 []int, nums2 []int) (ans []int) {
* @return {number[]}
*/
var intersection = function (nums1, nums2) {
const s = Array(1001).fill(false);
for (const x of nums1) {
s[x] = true;
}
const ans = [];
for (const x of nums2) {
if (s[x]) {
ans.push(x);
s[x] = false;
}
}
return ans;
const s = new Set(nums1);
return [...new Set(nums2.filter(x => s.has(x)))];
};
```

Expand All @@ -163,15 +162,12 @@ var intersection = function (nums1, nums2) {
```cs
public class Solution {
public int[] Intersection(int[] nums1, int[] nums2) {
List<int> result = new List<int>();
HashSet<int> arr1 = new(nums1);
HashSet<int> arr2 = new(nums2);
foreach (int x in arr1) {
if (arr2.Contains(x)) {
result.Add(x);
}
}
return result.ToArray();
HashSet<int> s1 = new HashSet<int>(nums1);
HashSet<int> s2 = new HashSet<int>(nums2);
s1.IntersectWith(s2);
int[] ans = new int[s1.Count];
s1.CopyTo(ans);
return ans;
}
}
```
Expand All @@ -186,18 +182,10 @@ class Solution {
* @return Integer[]
*/
function intersection($nums1, $nums2) {
$rs = [];
$set1 = array_values(array_unique($nums1));
$set2 = array_values(array_unique($nums2));
for ($i = 0; $i < count($set1); $i++) {
$hashmap[$set1[$i]] = 1;
}
for ($j = 0; $j < count($set2); $j++) {
if ($hashmap[$set2[$j]]) {
array_push($rs, $set2[$j]);
}
}
return $rs;
$s1 = array_unique($nums1);
$s2 = array_unique($nums2);
$ans = array_intersect($s1, $s2);
return array_values($ans);
}
}
```
Expand All @@ -206,27 +194,4 @@ class Solution {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### JavaScript

```js
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
*/
var intersection = function (nums1, nums2) {
return Array.from(new Set(nums1)).filter(num => new Set(nums2).has(num));
};
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
85 changes: 28 additions & 57 deletions solution/0300-0399/0349.Intersection of Two Arrays/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table or Array

First, we use a hash table or an array $s$ of length $1001$ to record the elements that appear in the array $nums1$. Then, we iterate through each element in the array $nums2$. If an element $x$ is in $s$, we add $x$ to the answer and remove $x$ from $s$.

After the iteration is finished, we return the answer array.

The time complexity is $O(n+m)$, and the space complexity is $O(n)$. Here, $n$ and $m$ are the lengths of the arrays $nums1$ and $nums2$, respectively.

<!-- tabs:start -->

Expand Down Expand Up @@ -126,6 +132,15 @@ func intersection(nums1 []int, nums2 []int) (ans []int) {
}
```

#### TypeScript

```ts
function intersection(nums1: number[], nums2: number[]): number[] {
const s = new Set(nums1);
return [...new Set(nums2.filter(x => s.has(x)))];
}
```

#### JavaScript

```js
Expand All @@ -135,18 +150,8 @@ func intersection(nums1 []int, nums2 []int) (ans []int) {
* @return {number[]}
*/
var intersection = function (nums1, nums2) {
const s = Array(1001).fill(false);
for (const x of nums1) {
s[x] = true;
}
const ans = [];
for (const x of nums2) {
if (s[x]) {
ans.push(x);
s[x] = false;
}
}
return ans;
const s = new Set(nums1);
return [...new Set(nums2.filter(x => s.has(x)))];
};
```

Expand All @@ -155,15 +160,12 @@ var intersection = function (nums1, nums2) {
```cs
public class Solution {
public int[] Intersection(int[] nums1, int[] nums2) {
List<int> result = new List<int>();
HashSet<int> arr1 = new(nums1);
HashSet<int> arr2 = new(nums2);
foreach (int x in arr1) {
if (arr2.Contains(x)) {
result.Add(x);
}
}
return result.ToArray();
HashSet<int> s1 = new HashSet<int>(nums1);
HashSet<int> s2 = new HashSet<int>(nums2);
s1.IntersectWith(s2);
int[] ans = new int[s1.Count];
s1.CopyTo(ans);
return ans;
}
}
```
Expand All @@ -178,18 +180,10 @@ class Solution {
* @return Integer[]
*/
function intersection($nums1, $nums2) {
$rs = [];
$set1 = array_values(array_unique($nums1));
$set2 = array_values(array_unique($nums2));
for ($i = 0; $i < count($set1); $i++) {
$hashmap[$set1[$i]] = 1;
}
for ($j = 0; $j < count($set2); $j++) {
if ($hashmap[$set2[$j]]) {
array_push($rs, $set2[$j]);
}
}
return $rs;
$s1 = array_unique($nums1);
$s2 = array_unique($nums2);
$ans = array_intersect($s1, $s2);
return array_values($ans);
}
}
```
Expand All @@ -198,27 +192,4 @@ class Solution {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### JavaScript

```js
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
*/
var intersection = function (nums1, nums2) {
return Array.from(new Set(nums1)).filter(num => new Set(nums2).has(num));
};
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
14 changes: 2 additions & 12 deletions solution/0300-0399/0349.Intersection of Two Arrays/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@
* @return {number[]}
*/
var intersection = function (nums1, nums2) {
const s = Array(1001).fill(false);
for (const x of nums1) {
s[x] = true;
}
const ans = [];
for (const x of nums2) {
if (s[x]) {
ans.push(x);
s[x] = false;
}
}
return ans;
const s = new Set(nums1);
return [...new Set(nums2.filter(x => s.has(x)))];
};
18 changes: 5 additions & 13 deletions solution/0300-0399/0349.Intersection of Two Arrays/Solution.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,9 @@ class Solution {
* @return Integer[]
*/
function intersection($nums1, $nums2) {
$rs = [];
$set1 = array_values(array_unique($nums1));
$set2 = array_values(array_unique($nums2));
for ($i = 0; $i < count($set1); $i++) {
$hashmap[$set1[$i]] = 1;
}
for ($j = 0; $j < count($set2); $j++) {
if ($hashmap[$set2[$j]]) {
array_push($rs, $set2[$j]);
}
}
return $rs;
$s1 = array_unique($nums1);
$s2 = array_unique($nums2);
$ans = array_intersect($s1, $s2);
return array_values($ans);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function intersection(nums1: number[], nums2: number[]): number[] {
const s = new Set(nums1);
return [...new Set(nums2.filter(x => s.has(x)))];
}

This file was deleted.

17 changes: 7 additions & 10 deletions solution/0300-0399/0349.Intersection of Two Arrays/solution.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
public class Solution {
public int[] Intersection(int[] nums1, int[] nums2) {
List<int> result = new List<int>();
HashSet<int> arr1 = new(nums1);
HashSet<int> arr2 = new(nums2);
foreach (int x in arr1) {
if (arr2.Contains(x)) {
result.Add(x);
}
}
return result.ToArray();
HashSet<int> s1 = new HashSet<int>(nums1);
HashSet<int> s2 = new HashSet<int>(nums2);
s1.IntersectWith(s2);
int[] ans = new int[s1.Count];
s1.CopyTo(ans);
return ans;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ tags:

### 方法一:贪心 + 排序

考虑每个城市对所有道路的总重要性的贡献度,按贡献度从小到大排序,为城市依次分配 $[1, 2, ..., n]$。
我们考虑每个城市对所有道路的总重要性的贡献度,记录在数组 $\text{deg}$ 中。然后将 $\text{deg}$ 按贡献度从小到大排序,为城市依次分配 $[1, 2, ..., n]$。

时间复杂度 $O(n \tiems \log n)$,其中 $n$ 表示城市数目
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$

<!-- tabs:start -->

Expand Down Expand Up @@ -135,7 +135,9 @@ public:
}
sort(deg.begin(), deg.end());
long long ans = 0;
for (int i = 0; i < n; ++i) ans += 1ll * (i + 1) * deg[i];
for (int i = 0; i < n; ++i) {
ans += (i + 1LL) * deg[i];
}
return ans;
}
};
Expand All @@ -144,18 +146,31 @@ public:
#### Go

```go
func maximumImportance(n int, roads [][]int) int64 {
func maximumImportance(n int, roads [][]int) (ans int64) {
deg := make([]int, n)
for _, r := range roads {
deg[r[0]]++
deg[r[1]]++
}
sort.Ints(deg)
var ans int64
for i := 0; i < n; i++ {
ans += int64((i + 1) * deg[i])
for i, x := range deg {
ans += int64(x) * int64(i+1)
}
return ans
return
}
```

#### TypeScript

```ts
function maximumImportance(n: number, roads: number[][]): number {
const deg: number[] = Array(n).fill(0);
for (const [a, b] of roads) {
++deg[a];
++deg[b];
}
deg.sort((a, b) => a - b);
return deg.reduce((acc, cur, idx) => acc + (idx + 1) * cur, 0);
}
```

Expand Down
Loading