Skip to content

Commit e860b0a

Browse files
committed
leetcode
1 parent b07ae6c commit e860b0a

File tree

4 files changed

+363
-0
lines changed

4 files changed

+363
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
105105
- [Integer to Roman](IntegerToRoman/integer_to_roman.dart)
106106
- [Ugly Number](UglyNumber/ugly_number.dart)
107107
- [Minimum Window Substring](MinimumWindowSubstring/minimum_window_substring.dart)
108+
- [Set Mismatch](SetMismatch/set_mismatch.dart)
108109

109110
## Reach me via
110111

SetMismatch/set_mismatch.dart

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
3+
4+
-* Set Mismatch *-
5+
6+
7+
You have a set of integers s, which originally contains all the numbers from 1 to n. Unfortunately, due to some error, one of the numbers in s got duplicated to another number in the set, which results in repetition of one number and loss of another number.
8+
9+
You are given an integer array nums representing the data status of this set after the error.
10+
11+
Find the number that occurs twice and the number that is missing and return them in the form of an array.
12+
13+
14+
15+
Example 1:
16+
17+
Input: nums = [1,2,2,4]
18+
Output: [2,3]
19+
Example 2:
20+
21+
Input: nums = [1,1]
22+
Output: [1,2]
23+
24+
25+
Constraints:
26+
27+
2 <= nums.length <= 104
28+
1 <= nums[i] <= 104
29+
30+
*/
31+
32+
import 'dart:collection';
33+
34+
class A {
35+
// Runtime: 723 ms, faster than 50.00% of Dart online submissions for Set Mismatch.
36+
// Memory Usage: 164.9 MB, less than 50.00% of Dart online submissions for Set Mismatch.
37+
38+
List<int> findErrorNums(List<int> nums) {
39+
int dup = 0, miss = 0;
40+
HashSet<int> hashset = HashSet();
41+
42+
// find duplicate using set
43+
for (int i = 0; i < nums.length; ++i) {
44+
if (hashset.contains(nums[i])) dup = nums[i];
45+
hashset.add(nums[i]);
46+
}
47+
48+
// find missing
49+
for (int i = 1; i <= nums.length; ++i) {
50+
if (hashset.contains(i) == false) {
51+
miss = i;
52+
break;
53+
}
54+
}
55+
56+
return [dup, miss];
57+
}
58+
}
59+
60+
class B {
61+
// Runtime: 632 ms, faster than 50.00% of Dart online submissions for Set Mismatch.
62+
// Memory Usage: 150.3 MB, less than 100.00% of Dart online submissions for Set Mismatch.
63+
List<int> findErrorNums(List<int> nums) {
64+
int n = nums.length;
65+
int i = 0;
66+
67+
while (i < n) {
68+
int idx = nums[i] - 1;
69+
70+
if (nums[i] != nums[idx]) {
71+
int temp = nums[i];
72+
nums[i] = nums[idx];
73+
nums[idx] = temp;
74+
} else {
75+
++i;
76+
}
77+
}
78+
79+
for (i = 0; i < n; ++i) {
80+
if (nums[i] != i + 1) {
81+
return [nums[i], i + 1];
82+
}
83+
}
84+
85+
return [];
86+
}
87+
}
88+
89+
class C {
90+
List<int> findErrorNums(List<int> nums) {
91+
int n = nums.length;
92+
List<int> arr = List.filled(n + 1, 0);
93+
List<int> vec = List.empty(growable: true);
94+
95+
for (int i in nums) {
96+
arr[i]++;
97+
}
98+
99+
for (int i = 1; i < n + 1; i++) {
100+
if (arr[i] > 1) vec.add(i);
101+
}
102+
for (int i = 1; i < n + 1; i++) {
103+
if (arr[i] == 0) vec.add(i);
104+
}
105+
return vec;
106+
}
107+
}
108+
109+
class D {
110+
List<int> findErrorNums(List<int> nums) {
111+
nums.sort();
112+
List<int> b = List.filled(nums.length + 1, 0);
113+
int x = 0;
114+
int y = 0;
115+
// int c = 1;
116+
for (int i = 0; i < nums.length; i++) {
117+
b[nums[i]] = b[nums[i]] + 1;
118+
}
119+
for (int i = 1; i < b.length; i++) {
120+
if (b[i] == 0) {
121+
y = i;
122+
}
123+
if (b[i] == 2) {
124+
x = i;
125+
}
126+
}
127+
return [x, y];
128+
}
129+
}
130+
131+
/*
132+
133+
134+
*/
135+
class E {
136+
// Runtime: 369 ms, faster than 100.00% of Dart online submissions for Set Mismatch.
137+
// Memory Usage: 159.3 MB, less than 50.00% of Dart online submissions for Set Mismatch.
138+
List<int> findErrorNums(List<int> nums) {
139+
List<int> ans = List.filled(2, 0);
140+
for (int i = 0; i < nums.length; i++) {
141+
int val = (nums[i]).abs();
142+
ans[1] ^= (i + 1) ^ val;
143+
if (nums[val - 1] < 0)
144+
ans[0] = val;
145+
else
146+
nums[val - 1] = -nums[val - 1];
147+
}
148+
ans[1] ^= ans[0];
149+
return ans;
150+
}
151+
}

SetMismatch/set_mismatch.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
// func findErrorNums(nums []int) []int {
4+
5+
// var ans []int = make([]int, 0)
6+
// for i := 0; i < len(nums); i++ {
7+
// val := math.Abs(nums[i])
8+
// ans[1] ^= (i + 1) ^ int(val)
9+
// if nums[val - 1] < 0 {
10+
11+
// }
12+
13+
// }
14+
// }
15+
16+
func findErrorNums(nums []int) (res []int) {
17+
var a int
18+
for i, n := range nums {
19+
a ^= n // XOR once per number in nums
20+
a ^= i + 1 // XOR once per expected number
21+
}
22+
23+
// find lowest bit, it belongs either to the missing or double value
24+
lowbit := a & -a
25+
b := a
26+
for i, n := range nums {
27+
// filter by low bit
28+
// this will skip either the missing or double value
29+
if n&lowbit > 0 {
30+
b ^= n
31+
}
32+
if (i+1)&lowbit > 0 {
33+
b ^= i + 1
34+
}
35+
}
36+
37+
// XOR a with b to remove b from a
38+
a ^= b
39+
40+
// At this stage, a and b are the double and missing values, but we don't
41+
// know which one is which! Do a final loop to figure out the return order.
42+
for _, n := range nums {
43+
if n == a {
44+
return []int{a, b}
45+
}
46+
if n == b {
47+
return []int{b, a}
48+
}
49+
}
50+
return nil
51+
}

SetMismatch/set_mismatch.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# 🔥 Set Mismatch 🔥 || 5 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Approach
4+
5+
### Idea
6+
7+
`(Note: I've added another solution that has a better space complexity at O(1) extra space vs. the original solution below at O(N) extra space, but it does so at the cost of an extra iteration through nums, though the time complexity remains O(N). Skip down to the Alternate Idea section for the breakdown.)`
8+
9+
- For this problem, we can take advantage of some math, because one thing we know about a sequence of numbers from 1 to N is that their sum should equal the Nth triangular number (N * (N + 1) / 2).
10+
11+
- Since the only difference between the ideal array ranging from 1 to N and our input array nums is the duplicated number, that means that the difference between the sum of nums and the Nth triangular number is the same as the difference between our duplicated number (dupe) and the missing number.
12+
13+
- We can easily find the duplicated number by utilizing a boolean array (seen) to keep track of which numbers have already been seen. While iterating through nums, whenever we come across a number for the second time, that number must be our dupe. We can also use this iteration to find the difference in the sums.
14+
15+
- Then we can just return the dupe and the sum difference applied to the dupe to identify the missing number.
16+
17+
## Solution - 1
18+
19+
```dart
20+
import 'dart:collection';
21+
22+
class Solution {
23+
// Runtime: 723 ms, faster than 100.0% of Dart online submissions for Set Mismatch.
24+
// Memory Usage: 164.9 MB, less than 50.00% of Dart online submissions for Set Mismatch.
25+
26+
List<int> findErrorNums(List<int> nums) {
27+
int dup = 0, miss = 0;
28+
HashSet<int> hashset = HashSet();
29+
30+
// find duplicate using set
31+
for (int i = 0; i < nums.length; ++i) {
32+
if (hashset.contains(nums[i])) dup = nums[i];
33+
hashset.add(nums[i]);
34+
}
35+
36+
// find missing
37+
for (int i = 1; i <= nums.length; ++i) {
38+
if (hashset.contains(i) == false) {
39+
miss = i;
40+
break;
41+
}
42+
}
43+
44+
return [dup, miss];
45+
}
46+
}
47+
```
48+
49+
## Solution - 2
50+
51+
```dart
52+
class Solution {
53+
// Runtime: 632 ms, faster than 100.0% of Dart online submissions for Set Mismatch.
54+
// Memory Usage: 150.3 MB, less than 100.00% of Dart online submissions for Set Mismatch.
55+
List<int> findErrorNums(List<int> nums) {
56+
int n = nums.length;
57+
int i = 0;
58+
59+
while (i < n) {
60+
int idx = nums[i] - 1;
61+
62+
if (nums[i] != nums[idx]) {
63+
int temp = nums[i];
64+
nums[i] = nums[idx];
65+
nums[idx] = temp;
66+
} else {
67+
++i;
68+
}
69+
}
70+
71+
for (i = 0; i < n; ++i) {
72+
if (nums[i] != i + 1) {
73+
return [nums[i], i + 1];
74+
}
75+
}
76+
77+
return [];
78+
}
79+
}
80+
```
81+
82+
## Solution - 3
83+
84+
### The idea is based on
85+
86+
(1 ^ 2 ^ 3 ^ .. ^ n) ^ (1 ^ 2 ^ 3 ^ .. ^ n) = 0
87+
88+
- Suppose we change 'a' to 'b', then all but 'a' and 'b' are XORed exactly 2 times. The result is then
89+
0 ^ a ^ b ^ b ^ b = a ^ b
90+
- Let c = a ^ b, if we can find 'b' which appears 2 times in the original array, 'a' can be easily calculated by a = c ^ b.
91+
92+
```dart
93+
class Solution {
94+
// Runtime: 369 ms, faster than 100.00% of Dart online submissions for Set Mismatch.
95+
// Memory Usage: 159.3 MB, less than 50.00% of Dart online submissions for Set Mismatch.
96+
List<int> findErrorNums(List<int> nums) {
97+
List<int> ans = List.filled(2, 0);
98+
for (int i = 0; i < nums.length; i++) {
99+
int val = (nums[i]).abs();
100+
ans[1] ^= (i + 1) ^ val;
101+
if (nums[val - 1] < 0)
102+
ans[0] = val;
103+
else
104+
nums[val - 1] = -nums[val - 1];
105+
}
106+
ans[1] ^= ans[0];
107+
return ans;
108+
}
109+
}
110+
```
111+
112+
## Solution - 4
113+
114+
```dart
115+
class Solution {
116+
List<int> findErrorNums(List<int> nums) {
117+
int n = nums.length;
118+
List<int> arr = List.filled(n + 1, 0);
119+
List<int> vec = List.empty(growable: true);
120+
121+
for (int i in nums) {
122+
arr[i]++;
123+
}
124+
125+
for (int i = 1; i < n + 1; i++) {
126+
if (arr[i] > 1) vec.add(i);
127+
}
128+
for (int i = 1; i < n + 1; i++) {
129+
if (arr[i] == 0) vec.add(i);
130+
}
131+
return vec;
132+
}
133+
}
134+
```
135+
136+
## Solution - 5
137+
138+
```dart
139+
class D {
140+
List<int> findErrorNums(List<int> nums) {
141+
nums.sort();
142+
List<int> b = List.filled(nums.length + 1, 0);
143+
int x = 0;
144+
int y = 0;
145+
// int c = 1;
146+
for (int i = 0; i < nums.length; i++) {
147+
b[nums[i]] = b[nums[i]] + 1;
148+
}
149+
for (int i = 1; i < b.length; i++) {
150+
if (b[i] == 0) {
151+
y = i;
152+
}
153+
if (b[i] == 2) {
154+
x = i;
155+
}
156+
}
157+
return [x, y];
158+
}
159+
}
160+
```

0 commit comments

Comments
 (0)