Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6fabffa

Browse files
Add solution for First Missing Positive problem with comprehensive test cases
1 parent de0ba09 commit 6fabffa

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

‎leetcode/41.first_missing_positive.dart

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// https://leetcode.com/problems/first-missing-positive/description/
2+
3+
import 'package:test/test.dart';
4+
5+
void main(List<String> args) {
6+
final stopwatch = Stopwatch()..start();
7+
8+
runTests();
9+
print(Solution().firstMissingPositive([1, 2, 6, 3, 5, 4]));
10+
11+
///
12+
stopwatch.stop();
13+
print('Function Execution Time : ${stopwatch.elapsedMicroseconds} micro sec');
14+
}
15+
16+
class Solution {
17+
////! Solved The problem
18+
////? Hash map is good
19+
int firstMissingPositive(List<int> nums) {
20+
final Map<int, int> map = {};
21+
22+
for (int i = 0; i < nums.length; i++) {
23+
if (nums[i] > 0) map[nums[i]] = 1;
24+
}
25+
26+
for (int i = 1; i <= map.length + 1; i++) {
27+
if (map[i] == null) return i;
28+
}
29+
return -1;
30+
}
31+
}
32+
33+
void runTests() {
34+
final Solution s = Solution();
35+
36+
group('First Missing Positive', () {
37+
// Basic examples from problem statement
38+
test('Example 1: [1,2,0] → 3', () {
39+
expect(s.firstMissingPositive([1, 2, 0]), equals(3));
40+
});
41+
42+
test('Example 2: [3,4,-1,1] → 2', () {
43+
expect(s.firstMissingPositive([3, 4, -1, 1]), equals(2));
44+
});
45+
46+
test('Example 3: [7,8,9,11,12] → 1', () {
47+
expect(s.firstMissingPositive([7, 8, 9, 11, 12]), equals(1));
48+
});
49+
50+
// Edge cases
51+
test('Single element array [1] → 2', () {
52+
expect(s.firstMissingPositive([1]), equals(2));
53+
});
54+
55+
test('Single element array [2] → 1', () {
56+
expect(s.firstMissingPositive([2]), equals(1));
57+
});
58+
59+
test('Single element array [0] → 1', () {
60+
expect(s.firstMissingPositive([0]), equals(1));
61+
});
62+
63+
test('Single element array [-1] → 1', () {
64+
expect(s.firstMissingPositive([-1]), equals(1));
65+
});
66+
67+
// All negatives
68+
test('All negatives: [-5,-3,-1] → 1', () {
69+
expect(s.firstMissingPositive([-5, -3, -1]), equals(1));
70+
});
71+
72+
// All positives but missing first
73+
test('Missing first positive: [2,3,4] → 1', () {
74+
expect(s.firstMissingPositive([2, 3, 4]), equals(1));
75+
});
76+
77+
// Complete sequence
78+
test('Complete sequence: [1,2,3,4] → 5', () {
79+
expect(s.firstMissingPositive([1, 2, 3, 4]), equals(5));
80+
});
81+
82+
// Large numbers
83+
test('Large numbers: [2147483647] → 1', () {
84+
expect(s.firstMissingPositive([2147483647]), equals(1));
85+
});
86+
87+
test('Large range: [1,2,3,...,99999,100000] → 100001', () {
88+
final nums = List.generate(100000, (i) => i + 1);
89+
expect(s.firstMissingPositive(nums), equals(100001));
90+
});
91+
92+
// Missing in middle
93+
test('Missing in middle: [1,3,4,5] → 2', () {
94+
expect(s.firstMissingPositive([1, 3, 4, 5]), equals(2));
95+
});
96+
97+
test('Multiple missing: [1,5,6] → 2', () {
98+
expect(s.firstMissingPositive([1, 5, 6]), equals(2));
99+
});
100+
101+
// Duplicates
102+
test('With duplicates: [1,1,2,2] → 3', () {
103+
expect(s.firstMissingPositive([1, 1, 2, 2]), equals(3));
104+
});
105+
106+
test('All duplicates: [7,7,7] → 1', () {
107+
expect(s.firstMissingPositive([7, 7, 7]), equals(1));
108+
});
109+
110+
// Mixed cases
111+
test('Mixed positive/negative: [-1,-2,0,1,3] → 2', () {
112+
expect(s.firstMissingPositive([-1, -2, 0, 1, 3]), equals(2));
113+
});
114+
115+
test('Mixed with large range: [1,1000] → 2', () {
116+
expect(s.firstMissingPositive([1, 1000]), equals(2));
117+
});
118+
119+
// Maximum constraints
120+
test('Maximum length array with all numbers', () {
121+
final nums = List.generate(100000, (i) => i + 1);
122+
expect(s.firstMissingPositive(nums), equals(100001));
123+
});
124+
125+
test('Maximum length array missing 1', () {
126+
final nums = List.generate(100000, (i) => i + 2);
127+
expect(s.firstMissingPositive(nums), equals(1));
128+
});
129+
});
130+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /