Skip to content

Commit 175fba4

Browse files
committed
feat: add solutions to lc problems: No.0349,2285
* No.0349.Intersection of Two Arrays * No.2285.Maximum Total Importance of Roads
1 parent c7f9776 commit 175fba4

File tree

12 files changed

+132
-177
lines changed

12 files changed

+132
-177
lines changed

solution/0300-0399/0349.Intersection of Two Arrays/README.md

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ func intersection(nums1 []int, nums2 []int) (ans []int) {
134134
}
135135
```
136136

137+
#### TypeScript
138+
139+
```ts
140+
function intersection(nums1: number[], nums2: number[]): number[] {
141+
const s = new Set(nums1);
142+
return [...new Set(nums2.filter(x => s.has(x)))];
143+
}
144+
```
145+
137146
#### JavaScript
138147

139148
```js
@@ -143,18 +152,8 @@ func intersection(nums1 []int, nums2 []int) (ans []int) {
143152
* @return {number[]}
144153
*/
145154
var intersection = function (nums1, nums2) {
146-
const s = Array(1001).fill(false);
147-
for (const x of nums1) {
148-
s[x] = true;
149-
}
150-
const ans = [];
151-
for (const x of nums2) {
152-
if (s[x]) {
153-
ans.push(x);
154-
s[x] = false;
155-
}
156-
}
157-
return ans;
155+
const s = new Set(nums1);
156+
return [...new Set(nums2.filter(x => s.has(x)))];
158157
};
159158
```
160159

@@ -163,15 +162,12 @@ var intersection = function (nums1, nums2) {
163162
```cs
164163
public class Solution {
165164
public int[] Intersection(int[] nums1, int[] nums2) {
166-
List<int> result = new List<int>();
167-
HashSet<int> arr1 = new(nums1);
168-
HashSet<int> arr2 = new(nums2);
169-
foreach (int x in arr1) {
170-
if (arr2.Contains(x)) {
171-
result.Add(x);
172-
}
173-
}
174-
return result.ToArray();
165+
HashSet<int> s1 = new HashSet<int>(nums1);
166+
HashSet<int> s2 = new HashSet<int>(nums2);
167+
s1.IntersectWith(s2);
168+
int[] ans = new int[s1.Count];
169+
s1.CopyTo(ans);
170+
return ans;
175171
}
176172
}
177173
```
@@ -186,18 +182,10 @@ class Solution {
186182
* @return Integer[]
187183
*/
188184
function intersection($nums1, $nums2) {
189-
$rs = [];
190-
$set1 = array_values(array_unique($nums1));
191-
$set2 = array_values(array_unique($nums2));
192-
for ($i = 0; $i < count($set1); $i++) {
193-
$hashmap[$set1[$i]] = 1;
194-
}
195-
for ($j = 0; $j < count($set2); $j++) {
196-
if ($hashmap[$set2[$j]]) {
197-
array_push($rs, $set2[$j]);
198-
}
199-
}
200-
return $rs;
185+
$s1 = array_unique($nums1);
186+
$s2 = array_unique($nums2);
187+
$ans = array_intersect($s1, $s2);
188+
return array_values($ans);
201189
}
202190
}
203191
```
@@ -206,27 +194,4 @@ class Solution {
206194

207195
<!-- solution:end -->
208196

209-
<!-- solution:start -->
210-
211-
### 方法二
212-
213-
<!-- tabs:start -->
214-
215-
#### JavaScript
216-
217-
```js
218-
/**
219-
* @param {number[]} nums1
220-
* @param {number[]} nums2
221-
* @return {number[]}
222-
*/
223-
var intersection = function (nums1, nums2) {
224-
return Array.from(new Set(nums1)).filter(num => new Set(nums2).has(num));
225-
};
226-
```
227-
228-
<!-- tabs:end -->
229-
230-
<!-- solution:end -->
231-
232197
<!-- problem:end -->

solution/0300-0399/0349.Intersection of Two Arrays/README_EN.md

Lines changed: 28 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ tags:
5252

5353
<!-- solution:start -->
5454

55-
### Solution 1
55+
### Solution 1: Hash Table or Array
56+
57+
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$.
58+
59+
After the iteration is finished, we return the answer array.
60+
61+
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.
5662

5763
<!-- tabs:start -->
5864

@@ -126,6 +132,15 @@ func intersection(nums1 []int, nums2 []int) (ans []int) {
126132
}
127133
```
128134

135+
#### TypeScript
136+
137+
```ts
138+
function intersection(nums1: number[], nums2: number[]): number[] {
139+
const s = new Set(nums1);
140+
return [...new Set(nums2.filter(x => s.has(x)))];
141+
}
142+
```
143+
129144
#### JavaScript
130145

131146
```js
@@ -135,18 +150,8 @@ func intersection(nums1 []int, nums2 []int) (ans []int) {
135150
* @return {number[]}
136151
*/
137152
var intersection = function (nums1, nums2) {
138-
const s = Array(1001).fill(false);
139-
for (const x of nums1) {
140-
s[x] = true;
141-
}
142-
const ans = [];
143-
for (const x of nums2) {
144-
if (s[x]) {
145-
ans.push(x);
146-
s[x] = false;
147-
}
148-
}
149-
return ans;
153+
const s = new Set(nums1);
154+
return [...new Set(nums2.filter(x => s.has(x)))];
150155
};
151156
```
152157

@@ -155,15 +160,12 @@ var intersection = function (nums1, nums2) {
155160
```cs
156161
public class Solution {
157162
public int[] Intersection(int[] nums1, int[] nums2) {
158-
List<int> result = new List<int>();
159-
HashSet<int> arr1 = new(nums1);
160-
HashSet<int> arr2 = new(nums2);
161-
foreach (int x in arr1) {
162-
if (arr2.Contains(x)) {
163-
result.Add(x);
164-
}
165-
}
166-
return result.ToArray();
163+
HashSet<int> s1 = new HashSet<int>(nums1);
164+
HashSet<int> s2 = new HashSet<int>(nums2);
165+
s1.IntersectWith(s2);
166+
int[] ans = new int[s1.Count];
167+
s1.CopyTo(ans);
168+
return ans;
167169
}
168170
}
169171
```
@@ -178,18 +180,10 @@ class Solution {
178180
* @return Integer[]
179181
*/
180182
function intersection($nums1, $nums2) {
181-
$rs = [];
182-
$set1 = array_values(array_unique($nums1));
183-
$set2 = array_values(array_unique($nums2));
184-
for ($i = 0; $i < count($set1); $i++) {
185-
$hashmap[$set1[$i]] = 1;
186-
}
187-
for ($j = 0; $j < count($set2); $j++) {
188-
if ($hashmap[$set2[$j]]) {
189-
array_push($rs, $set2[$j]);
190-
}
191-
}
192-
return $rs;
183+
$s1 = array_unique($nums1);
184+
$s2 = array_unique($nums2);
185+
$ans = array_intersect($s1, $s2);
186+
return array_values($ans);
193187
}
194188
}
195189
```
@@ -198,27 +192,4 @@ class Solution {
198192

199193
<!-- solution:end -->
200194

201-
<!-- solution:start -->
202-
203-
### Solution 2
204-
205-
<!-- tabs:start -->
206-
207-
#### JavaScript
208-
209-
```js
210-
/**
211-
* @param {number[]} nums1
212-
* @param {number[]} nums2
213-
* @return {number[]}
214-
*/
215-
var intersection = function (nums1, nums2) {
216-
return Array.from(new Set(nums1)).filter(num => new Set(nums2).has(num));
217-
};
218-
```
219-
220-
<!-- tabs:end -->
221-
222-
<!-- solution:end -->
223-
224195
<!-- problem:end -->

solution/0300-0399/0349.Intersection of Two Arrays/Solution.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,6 @@
44
* @return {number[]}
55
*/
66
var intersection = function (nums1, nums2) {
7-
const s = Array(1001).fill(false);
8-
for (const x of nums1) {
9-
s[x] = true;
10-
}
11-
const ans = [];
12-
for (const x of nums2) {
13-
if (s[x]) {
14-
ans.push(x);
15-
s[x] = false;
16-
}
17-
}
18-
return ans;
7+
const s = new Set(nums1);
8+
return [...new Set(nums2.filter(x => s.has(x)))];
199
};

solution/0300-0399/0349.Intersection of Two Arrays/Solution.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,9 @@ class Solution {
55
* @return Integer[]
66
*/
77
function intersection($nums1, $nums2) {
8-
$rs = [];
9-
$set1 = array_values(array_unique($nums1));
10-
$set2 = array_values(array_unique($nums2));
11-
for ($i = 0; $i < count($set1); $i++) {
12-
$hashmap[$set1[$i]] = 1;
13-
}
14-
for ($j = 0; $j < count($set2); $j++) {
15-
if ($hashmap[$set2[$j]]) {
16-
array_push($rs, $set2[$j]);
17-
}
18-
}
19-
return $rs;
8+
$s1 = array_unique($nums1);
9+
$s2 = array_unique($nums2);
10+
$ans = array_intersect($s1, $s2);
11+
return array_values($ans);
2012
}
21-
}
13+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function intersection(nums1: number[], nums2: number[]): number[] {
2+
const s = new Set(nums1);
3+
return [...new Set(nums2.filter(x => s.has(x)))];
4+
}

solution/0300-0399/0349.Intersection of Two Arrays/Solution2.js

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
public class Solution {
22
public int[] Intersection(int[] nums1, int[] nums2) {
3-
List<int> result = new List<int>();
4-
HashSet<int> arr1 = new(nums1);
5-
HashSet<int> arr2 = new(nums2);
6-
foreach (int x in arr1) {
7-
if (arr2.Contains(x)) {
8-
result.Add(x);
9-
}
10-
}
11-
return result.ToArray();
3+
HashSet<int> s1 = new HashSet<int>(nums1);
4+
HashSet<int> s2 = new HashSet<int>(nums2);
5+
s1.IntersectWith(s2);
6+
int[] ans = new int[s1.Count];
7+
s1.CopyTo(ans);
8+
return ans;
129
}
13-
}
10+
}

solution/2200-2299/2285.Maximum Total Importance of Roads/README.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ tags:
8383

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

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

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

9090
<!-- tabs:start -->
9191

@@ -135,7 +135,9 @@ public:
135135
}
136136
sort(deg.begin(), deg.end());
137137
long long ans = 0;
138-
for (int i = 0; i < n; ++i) ans += 1ll * (i + 1) * deg[i];
138+
for (int i = 0; i < n; ++i) {
139+
ans += (i + 1LL) * deg[i];
140+
}
139141
return ans;
140142
}
141143
};
@@ -144,18 +146,31 @@ public:
144146
#### Go
145147
146148
```go
147-
func maximumImportance(n int, roads [][]int) int64 {
149+
func maximumImportance(n int, roads [][]int) (ans int64) {
148150
deg := make([]int, n)
149151
for _, r := range roads {
150152
deg[r[0]]++
151153
deg[r[1]]++
152154
}
153155
sort.Ints(deg)
154-
var ans int64
155-
for i := 0; i < n; i++ {
156-
ans += int64((i + 1) * deg[i])
156+
for i, x := range deg {
157+
ans += int64(x) * int64(i+1)
157158
}
158-
return ans
159+
return
160+
}
161+
```
162+
163+
#### TypeScript
164+
165+
```ts
166+
function maximumImportance(n: number, roads: number[][]): number {
167+
const deg: number[] = Array(n).fill(0);
168+
for (const [a, b] of roads) {
169+
++deg[a];
170+
++deg[b];
171+
}
172+
deg.sort((a, b) => a - b);
173+
return deg.reduce((acc, cur, idx) => acc + (idx + 1) * cur, 0);
159174
}
160175
```
161176

0 commit comments

Comments
 (0)