Skip to content

Commit 88517e1

Browse files
committed
new soln
1 parent 05d9c2f commit 88517e1

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

2215.FindDifference.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// 2215. Find the Difference of Two Arrays
2+
// Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:
3+
4+
// answer[0] is a list of all distinct integers in nums1 which are not present in nums2.
5+
// answer[1] is a list of all distinct integers in nums2 which are not present in nums1.
6+
// Note that the integers in the lists may be returned in any order.
7+
8+
// Example 1:
9+
// Input: nums1 = [1,2,3], nums2 = [2,4,6]
10+
// Output: [[1,3],[4,6]]
11+
// Explanation:
12+
// For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3].
13+
// For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums2. Therefore, answer[1] = [4,6].
14+
// Example 2:
15+
// Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2]
16+
// Output: [[3],[]]
17+
// Explanation:
18+
// For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] == nums1[3], their value is only included once and answer[0] = [3].
19+
// Every integer in nums2 is present in nums1. Therefore, answer[1] = [].
20+
21+
// Constraints:
22+
// 1 <= nums1.length, nums2.length <= 1000
23+
// -1000 <= nums1[i], nums2[i] <= 1000
24+
25+
public class Solution {
26+
public IList<IList<int>> FindDifference(int[] nums1, int[] nums2) {
27+
HashSet<int> set1 = new();
28+
HashSet<int> set2 = new();
29+
30+
foreach(int num in nums1){
31+
set1.Add(num);
32+
}
33+
foreach(int num in nums2){
34+
set2.Add(num);
35+
}
36+
foreach(var item in set1){
37+
if(set2.Contains(item)){
38+
set2.Remove(item);
39+
set1.Remove(item);
40+
}
41+
}
42+
43+
return new List<IList<int>>(){set1.ToList(), set2.ToList()};
44+
}
45+
}
46+
47+
48+
public class Solution {
49+
public IList<IList<int>> FindDifference(int[] nums1, int[] nums2) {
50+
HashSet<int> set1 = new();
51+
HashSet<int> set2 = new();
52+
53+
foreach(int num in nums1){
54+
set1.Add(num);
55+
}
56+
foreach(int num in nums2){
57+
set2.Add(num);
58+
}
59+
List<int> result1 = new();
60+
foreach(var item in set1){
61+
if(!set2.Contains(item)){
62+
result1.Add(item);
63+
}
64+
}
65+
List<int> result2 = new();
66+
foreach(var item in set2){
67+
if(!set1.Contains(item)){
68+
result2.Add(item);
69+
}
70+
}
71+
return new List<IList<int>>(){result1, result2};
72+
}
73+
}

2248.Intersection.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// 2248. Intersection of Multiple Arrays
2+
// Given a 2D integer array nums where nums[i] is a non-empty array of distinct positive integers, return the list of integers that are present in each array of nums sorted in ascending order.
3+
4+
// Example 1:
5+
// Input: nums = [[3,1,2,4,5],[1,2,3,4],[3,4,5,6]]
6+
// Output: [3,4]
7+
// Explanation:
8+
// The only integers present in each of nums[0] = [3,1,2,4,5], nums[1] = [1,2,3,4], and nums[2] = [3,4,5,6] are 3 and 4, so we return [3,4].
9+
// Example 2:
10+
// Input: nums = [[1,2,3],[4,5,6]]
11+
// Output: []
12+
// Explanation:
13+
// There does not exist any integer present both in nums[0] and nums[1], so we return an empty list [].
14+
15+
16+
// Constraints:
17+
// 1 <= nums.length <= 1000
18+
// 1 <= sum(nums[i].length) <= 1000
19+
// 1 <= nums[i][j] <= 1000
20+
// All the values of nums[i] are unique.
21+
22+
public class Solution {
23+
public IList<int> Intersection(int[][] nums) {
24+
HashSet<int> set = new HashSet<int>(nums[0]);
25+
26+
for(int i = 1; i < nums.Length; i++){
27+
HashSet<int> tempSet = new();
28+
for(int j =0; j < nums[i].Length; j++){
29+
if(set.Contains(nums[i][j]))
30+
tempSet.Add(nums[i][j]);
31+
}
32+
set = tempSet;
33+
}
34+
var result = set.ToList();
35+
result.Sort();
36+
return result;
37+
}
38+
}

2540.GetCommon.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// 2540. Minimum Common Value
2+
// Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both arrays. If there is no common integer amongst nums1 and nums2, return -1.
3+
4+
// Note that an integer is said to be common to nums1 and nums2 if both arrays have at least one occurrence of that integer.
5+
6+
// Example 1:
7+
// Input: nums1 = [1,2,3], nums2 = [2,4]
8+
// Output: 2
9+
// Explanation: The smallest element common to both arrays is 2, so we return 2.
10+
// Example 2:
11+
// Input: nums1 = [1,2,3,6], nums2 = [2,3,4,5]
12+
// Output: 2
13+
// Explanation: There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned.
14+
15+
// Constraints:
16+
// 1 <= nums1.length, nums2.length <= 105
17+
// 1 <= nums1[i], nums2[j] <= 109
18+
// Both nums1 and nums2 are sorted in non-decreasing order.
19+
20+
public class Solution {
21+
public int GetCommon(int[] nums1, int[] nums2) {
22+
int i = 0, j = 0;
23+
while(i < nums1.Length && j < nums2.Length){
24+
if(nums1[i] == nums2[j])
25+
return nums1[i];
26+
else if(nums1[i] > nums2[j])
27+
j++;
28+
else
29+
i++;
30+
}
31+
return -1;
32+
}
33+
}

0 commit comments

Comments
 (0)