diff --git a/solution/0300-0399/0349.Intersection of Two Arrays/README.md b/solution/0300-0399/0349.Intersection of Two Arrays/README.md index 7c30b676c89a4..08dd8354d295b 100644 --- a/solution/0300-0399/0349.Intersection of Two Arrays/README.md +++ b/solution/0300-0399/0349.Intersection of Two Arrays/README.md @@ -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 @@ -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)))]; }; ``` @@ -163,15 +162,12 @@ var intersection = function (nums1, nums2) { ```cs public class Solution { public int[] Intersection(int[] nums1, int[] nums2) { - List result = new List(); - HashSet arr1 = new(nums1); - HashSet arr2 = new(nums2); - foreach (int x in arr1) { - if (arr2.Contains(x)) { - result.Add(x); - } - } - return result.ToArray(); + HashSet s1 = new HashSet(nums1); + HashSet s2 = new HashSet(nums2); + s1.IntersectWith(s2); + int[] ans = new int[s1.Count]; + s1.CopyTo(ans); + return ans; } } ``` @@ -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); } } ``` @@ -206,27 +194,4 @@ class Solution { - - -### 方法二 - - - -#### 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)); -}; -``` - - - - - diff --git a/solution/0300-0399/0349.Intersection of Two Arrays/README_EN.md b/solution/0300-0399/0349.Intersection of Two Arrays/README_EN.md index 5b781abce29c9..6a523e51b5f7f 100644 --- a/solution/0300-0399/0349.Intersection of Two Arrays/README_EN.md +++ b/solution/0300-0399/0349.Intersection of Two Arrays/README_EN.md @@ -52,7 +52,13 @@ tags: -### 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. @@ -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 @@ -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)))]; }; ``` @@ -155,15 +160,12 @@ var intersection = function (nums1, nums2) { ```cs public class Solution { public int[] Intersection(int[] nums1, int[] nums2) { - List result = new List(); - HashSet arr1 = new(nums1); - HashSet arr2 = new(nums2); - foreach (int x in arr1) { - if (arr2.Contains(x)) { - result.Add(x); - } - } - return result.ToArray(); + HashSet s1 = new HashSet(nums1); + HashSet s2 = new HashSet(nums2); + s1.IntersectWith(s2); + int[] ans = new int[s1.Count]; + s1.CopyTo(ans); + return ans; } } ``` @@ -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); } } ``` @@ -198,27 +192,4 @@ class Solution { - - -### Solution 2 - - - -#### 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)); -}; -``` - - - - - diff --git a/solution/0300-0399/0349.Intersection of Two Arrays/Solution.js b/solution/0300-0399/0349.Intersection of Two Arrays/Solution.js index 8e448921df71f..8e02cc05f2944 100644 --- a/solution/0300-0399/0349.Intersection of Two Arrays/Solution.js +++ b/solution/0300-0399/0349.Intersection of Two Arrays/Solution.js @@ -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)))]; }; diff --git a/solution/0300-0399/0349.Intersection of Two Arrays/Solution.php b/solution/0300-0399/0349.Intersection of Two Arrays/Solution.php index 18775d08022c1..b2f8fba097fa3 100644 --- a/solution/0300-0399/0349.Intersection of Two Arrays/Solution.php +++ b/solution/0300-0399/0349.Intersection of Two Arrays/Solution.php @@ -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); } -} +} \ No newline at end of file diff --git a/solution/0300-0399/0349.Intersection of Two Arrays/Solution.ts b/solution/0300-0399/0349.Intersection of Two Arrays/Solution.ts new file mode 100644 index 0000000000000..c5b5388f1c689 --- /dev/null +++ b/solution/0300-0399/0349.Intersection of Two Arrays/Solution.ts @@ -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)))]; +} diff --git a/solution/0300-0399/0349.Intersection of Two Arrays/Solution2.js b/solution/0300-0399/0349.Intersection of Two Arrays/Solution2.js deleted file mode 100644 index c5d90bee0b32d..0000000000000 --- a/solution/0300-0399/0349.Intersection of Two Arrays/Solution2.js +++ /dev/null @@ -1,8 +0,0 @@ -/** - * @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)); -}; diff --git a/solution/0300-0399/0349.Intersection of Two Arrays/solution.cs b/solution/0300-0399/0349.Intersection of Two Arrays/solution.cs index f62d6b57dbf96..abd947b382d5d 100644 --- a/solution/0300-0399/0349.Intersection of Two Arrays/solution.cs +++ b/solution/0300-0399/0349.Intersection of Two Arrays/solution.cs @@ -1,13 +1,10 @@ public class Solution { public int[] Intersection(int[] nums1, int[] nums2) { - List result = new List(); - HashSet arr1 = new(nums1); - HashSet arr2 = new(nums2); - foreach (int x in arr1) { - if (arr2.Contains(x)) { - result.Add(x); - } - } - return result.ToArray(); + HashSet s1 = new HashSet(nums1); + HashSet s2 = new HashSet(nums2); + s1.IntersectWith(s2); + int[] ans = new int[s1.Count]; + s1.CopyTo(ans); + return ans; } -} +} \ No newline at end of file diff --git a/solution/2200-2299/2285.Maximum Total Importance of Roads/README.md b/solution/2200-2299/2285.Maximum Total Importance of Roads/README.md index 01f3f9039266e..ce52b7fe85081 100644 --- a/solution/2200-2299/2285.Maximum Total Importance of Roads/README.md +++ b/solution/2200-2299/2285.Maximum Total Importance of Roads/README.md @@ -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)$。 @@ -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; } }; @@ -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); } ``` diff --git a/solution/2200-2299/2285.Maximum Total Importance of Roads/README_EN.md b/solution/2200-2299/2285.Maximum Total Importance of Roads/README_EN.md index ac606129a4e6d..f5553a84d30d1 100644 --- a/solution/2200-2299/2285.Maximum Total Importance of Roads/README_EN.md +++ b/solution/2200-2299/2285.Maximum Total Importance of Roads/README_EN.md @@ -77,7 +77,11 @@ It can be shown that we cannot obtain a greater total importance than 20. -### Solution 1 +### Solution 1: Greedy + Sorting + +We consider the contribution of each city to the total importance of all roads, recorded in the array $\text{deg}$. Then, we sort $\text{deg}$ by contribution from smallest to largest and allocate $[1, 2, ..., n]$ to the cities in order. + +The time complexity is $O(n \log n)$, and the space complexity is $O(n)$. @@ -127,7 +131,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; } }; @@ -136,18 +142,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); } ``` diff --git a/solution/2200-2299/2285.Maximum Total Importance of Roads/Solution.cpp b/solution/2200-2299/2285.Maximum Total Importance of Roads/Solution.cpp index 2772b440780c4..e9df4cd183ff3 100644 --- a/solution/2200-2299/2285.Maximum Total Importance of Roads/Solution.cpp +++ b/solution/2200-2299/2285.Maximum Total Importance of Roads/Solution.cpp @@ -8,7 +8,9 @@ class Solution { } 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; } }; \ No newline at end of file diff --git a/solution/2200-2299/2285.Maximum Total Importance of Roads/Solution.go b/solution/2200-2299/2285.Maximum Total Importance of Roads/Solution.go index 5cf2ae1a932c6..ac9c1a17303e7 100644 --- a/solution/2200-2299/2285.Maximum Total Importance of Roads/Solution.go +++ b/solution/2200-2299/2285.Maximum Total Importance of Roads/Solution.go @@ -1,13 +1,12 @@ -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 } \ No newline at end of file diff --git a/solution/2200-2299/2285.Maximum Total Importance of Roads/Solution.ts b/solution/2200-2299/2285.Maximum Total Importance of Roads/Solution.ts new file mode 100644 index 0000000000000..d5a50f2226ba0 --- /dev/null +++ b/solution/2200-2299/2285.Maximum Total Importance of Roads/Solution.ts @@ -0,0 +1,9 @@ +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); +}