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 8b17b79

Browse files
Add solution for Sort Colors problem with comprehensive test cases
1 parent 4df37b1 commit 8b17b79

File tree

2 files changed

+124
-2
lines changed

2 files changed

+124
-2
lines changed

‎README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
- https://leetcode.com/problems/subarray-product-less-than-k/description/
4747
- https://leetcode.com/problems/k-diff-pairs-in-an-array/description/
4848
- Two Pointers: with new idea: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
49-
-
49+
- https://leetcode.com/problems/sort-colors/description/
5050
-
5151

5252
### Hard Problems
@@ -80,4 +80,5 @@ A Hash table is defined as a data structure used to insert, look up, and remove
8080
It operates on the hashing concept,
8181
where each key is translated by a hash function into a distinct index in an array.
8282
The index functions as a storage location for the matching value.
83-
In simple words, it maps the keys with the value.
83+
In simple words, it maps the keys with the value.
84+

‎leetcode/75.sort_colors.dart

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// https://leetcode.com/problems/sort-colors/description/
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
final stopwatch = Stopwatch()..start();
6+
runTests();
7+
8+
///
9+
stopwatch.stop();
10+
print('Function Execution Time : ${stopwatch.elapsedMicroseconds} micro Sec');
11+
}
12+
13+
class Solution {
14+
////! Accepted From the first time TC: O(N) SC: O(1)
15+
void sortColors(List<int> nums) {
16+
int zerosCount = 0;
17+
int onesCount = 0;
18+
for (var i = 0; i < nums.length; i++) {
19+
if (nums[i] == 0) {
20+
zerosCount++;
21+
} else if (nums[i] == 1) {
22+
onesCount++;
23+
}
24+
}
25+
for (var i = 0; i < nums.length; i++) {
26+
if (zerosCount > 0) {
27+
nums[i] = 0;
28+
zerosCount--;
29+
} else if (onesCount > 0) {
30+
nums[i] = 1;
31+
onesCount--;
32+
} else {
33+
nums[i] = 2;
34+
}
35+
}
36+
}
37+
}
38+
39+
void runTests() {
40+
final Solution s = Solution();
41+
42+
group('Sort Colors', () {
43+
// Basic examples from problem statement
44+
test('Example 1: [2,0,2,1,1,0] → [0,0,1,1,2,2]', () {
45+
final nums = [2, 0, 2, 1, 1, 0];
46+
s.sortColors(nums);
47+
expect(nums, equals([0, 0, 1, 1, 2, 2]));
48+
});
49+
50+
test('Example 2: [2,0,1] → [0,1,2]', () {
51+
final nums = [2, 0, 1];
52+
s.sortColors(nums);
53+
expect(nums, equals([0, 1, 2]));
54+
});
55+
56+
// Edge cases
57+
test('Single element array: [0] → [0]', () {
58+
final nums = [0];
59+
s.sortColors(nums);
60+
expect(nums, equals([0]));
61+
});
62+
63+
test('All zeros: [0,0,0] → [0,0,0]', () {
64+
final nums = [0, 0, 0];
65+
s.sortColors(nums);
66+
expect(nums, equals([0, 0, 0]));
67+
});
68+
69+
test('All ones: [1,1,1] → [1,1,1]', () {
70+
final nums = [1, 1, 1];
71+
s.sortColors(nums);
72+
expect(nums, equals([1, 1, 1]));
73+
});
74+
75+
test('All twos: [2,2,2] → [2,2,2]', () {
76+
final nums = [2, 2, 2];
77+
s.sortColors(nums);
78+
expect(nums, equals([2, 2, 2]));
79+
});
80+
81+
// Already sorted
82+
test('Already sorted: [0,1,2] → [0,1,2]', () {
83+
final nums = [0, 1, 2];
84+
s.sortColors(nums);
85+
expect(nums, equals([0, 1, 2]));
86+
});
87+
88+
// Reverse sorted
89+
test('Reverse sorted: [2,1,0] → [0,1,2]', () {
90+
final nums = [2, 1, 0];
91+
s.sortColors(nums);
92+
expect(nums, equals([0, 1, 2]));
93+
});
94+
95+
// Mixed cases
96+
test('Mixed case 1: [1,0,2,1,0] → [0,0,1,1,2]', () {
97+
final nums = [1, 0, 2, 1, 0];
98+
s.sortColors(nums);
99+
expect(nums, equals([0, 0, 1, 1, 2]));
100+
});
101+
102+
test('Mixed case 2: [2,1,2,0,0,1] → [0,0,1,1,2,2]', () {
103+
final nums = [2, 1, 2, 0, 0, 1];
104+
s.sortColors(nums);
105+
expect(nums, equals([0, 0, 1, 1, 2, 2]));
106+
});
107+
108+
// Random cases
109+
test('Random case 1: [0,2,1,1,2,0] → [0,0,1,1,2,2]', () {
110+
final nums = [0, 2, 1, 1, 2, 0];
111+
s.sortColors(nums);
112+
expect(nums, equals([0, 0, 1, 1, 2, 2]));
113+
});
114+
115+
test('Random case 2: [1,2,0,1,2,0,1] → [0,0,1,1,1,2,2]', () {
116+
final nums = [1, 2, 0, 1, 2, 0, 1];
117+
s.sortColors(nums);
118+
expect(nums, equals([0, 0, 1, 1, 1, 2, 2]));
119+
});
120+
});
121+
}

0 commit comments

Comments
(0)

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