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 a9623a4

Browse files
Add Next Permutation solution with tests; mark as not recommended
1 parent 59e5d61 commit a9623a4

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
- Hash Table: https://leetcode.com/problems/sequential-digits/description/
4141
- Hash Table: https://leetcode.com/problems/find-all-duplicates-in-an-array/
4242
- https://leetcode.com/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros/description/?envType=daily-question&envId=2025年05月10日
43+
- Bad Question : I don't recommend to solve: https://leetcode.com/problems/next-permutation/description/
4344
-
4445

4546
### Hard Problems

‎leetcode/31.next_permutation.dart

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
//! Bad Question : I don't recommend to solve
2+
// https://leetcode.com/problems/next-permutation/description/
3+
// https://www.youtube.com/watch?v=JRgIqugFhTo
4+
5+
import 'package:test/test.dart';
6+
7+
void main(List<String> args) {
8+
final stopwatch = Stopwatch()..start();
9+
runTests();
10+
11+
Solution s = Solution();
12+
final nums = [2, 3, 1]; // 3,1,2
13+
14+
s.nextPermutation(nums);
15+
16+
stopwatch.stop();
17+
print('Function Execution Time : ${stopwatch.elapsedMicroseconds} micro s');
18+
}
19+
20+
class Solution {
21+
void nextPermutation(List<int> nums) {
22+
int pivotIndex = -1;
23+
24+
/// find the pivot
25+
for (int i = nums.length - 2; i >= 0; i--) {
26+
if (nums[i] < nums[i + 1]) {
27+
pivotIndex = i;
28+
break;
29+
}
30+
}
31+
32+
/// find the successorIndex
33+
int successorIndex = -1;
34+
for (int i = nums.length - 1; i > pivotIndex && pivotIndex != -1; i--) {
35+
if (nums[i] > nums[pivotIndex]) {
36+
if (successorIndex == -1) {
37+
successorIndex = i;
38+
} else if (nums[i] < nums[successorIndex]) {
39+
successorIndex = i;
40+
}
41+
}
42+
}
43+
44+
/// Switch Povit and successorIndex
45+
int temp = 0;
46+
if (pivotIndex != -1) {
47+
temp = nums[pivotIndex];
48+
nums[pivotIndex] = nums[successorIndex];
49+
nums[successorIndex] = temp;
50+
}
51+
52+
/// Reverse The rest
53+
int left = pivotIndex + 1;
54+
int right = nums.length - 1;
55+
while (left < right) {
56+
/// Switch
57+
temp = nums[left];
58+
nums[left] = nums[right];
59+
nums[right] = temp;
60+
61+
/// go to the next
62+
left++;
63+
right--;
64+
}
65+
66+
print(nums);
67+
}
68+
}
69+
70+
void runTests() {
71+
final Solution s = Solution();
72+
73+
group('Next Permutation', () {
74+
// Basic examples from problem statement
75+
test('Example 1: [1,2,3] → [1,3,2]', () {
76+
final nums = [1, 2, 3];
77+
s.nextPermutation(nums);
78+
expect(nums, equals([1, 3, 2]));
79+
});
80+
81+
test('Example 2: [3,2,1] → [1,2,3]', () {
82+
final nums = [3, 2, 1];
83+
s.nextPermutation(nums);
84+
expect(nums, equals([1, 2, 3]));
85+
});
86+
87+
test('Example 3: [1,1,5] → [1,5,1]', () {
88+
final nums = [1, 1, 5];
89+
s.nextPermutation(nums);
90+
expect(nums, equals([1, 5, 1]));
91+
});
92+
93+
// Edge cases
94+
test('Single element array: [5] → [5]', () {
95+
final nums = [5];
96+
s.nextPermutation(nums);
97+
expect(nums, equals([5]));
98+
});
99+
100+
test('Two elements ascending: [1,2] → [2,1]', () {
101+
final nums = [1, 2];
102+
s.nextPermutation(nums);
103+
expect(nums, equals([2, 1]));
104+
});
105+
106+
test('Two elements descending: [2,1] → [1,2]', () {
107+
final nums = [2, 1];
108+
s.nextPermutation(nums);
109+
expect(nums, equals([1, 2]));
110+
});
111+
112+
// All same elements
113+
test('All same elements: [0,0,0] → [0,0,0]', () {
114+
final nums = [0, 0, 0];
115+
s.nextPermutation(nums);
116+
expect(nums, equals([0, 0, 0]));
117+
});
118+
119+
// Multiple permutations
120+
test('Next permutation 1: [1,3,2] → [2,1,3]', () {
121+
final nums = [1, 3, 2];
122+
s.nextPermutation(nums);
123+
expect(nums, equals([2, 1, 3]));
124+
});
125+
126+
test('Next permutation 2: [2,1,3] → [2,3,1]', () {
127+
final nums = [2, 1, 3];
128+
s.nextPermutation(nums);
129+
expect(nums, equals([2, 3, 1]));
130+
});
131+
132+
test('Next permutation 3: [2,3,1] → [3,1,2]', () {
133+
final nums = [2, 3, 1];
134+
s.nextPermutation(nums);
135+
expect(nums, equals([3, 1, 2]));
136+
});
137+
138+
// Complex cases
139+
test('Complex case 1: [1,5,8,4,7,6,5,3,1] → [1,5,8,5,1,3,4,6,7]', () {
140+
final nums = [1, 5, 8, 4, 7, 6, 5, 3, 1];
141+
s.nextPermutation(nums);
142+
expect(nums, equals([1, 5, 8, 5, 1, 3, 4, 6, 7]));
143+
});
144+
145+
test('Complex case 2: [2,4,3,2,1] → [3,1,2,2,4]', () {
146+
final nums = [2, 4, 3, 2, 1];
147+
s.nextPermutation(nums);
148+
expect(nums, equals([3, 1, 2, 2, 4]));
149+
});
150+
151+
// Maximum constraints
152+
test('Maximum length array: 100 elements', () {
153+
final nums = List.generate(100, (i) => i.isEven ? 1 : 2);
154+
final expected = [...nums];
155+
// Manually calculate expected next permutation
156+
expected[98] = 2;
157+
expected[99] = 1;
158+
s.nextPermutation(nums);
159+
expect(nums, equals(expected));
160+
});
161+
162+
test('Maximum value elements: [100,100] → [100,100]', () {
163+
final nums = [100, 100];
164+
s.nextPermutation(nums);
165+
expect(nums, equals([100, 100]));
166+
});
167+
168+
// Random cases
169+
test('Random case 1: [4,2,0,2,3,2,0] → [4,2,0,3,0,2,2]', () {
170+
final nums = [4, 2, 0, 2, 3, 2, 0];
171+
s.nextPermutation(nums);
172+
expect(nums, equals([4, 2, 0, 3, 0, 2, 2]));
173+
});
174+
175+
test('Random case 2: [5,4,7,5,3,2] → [5,5,2,3,4,7]', () {
176+
final nums = [5, 4, 7, 5, 3, 2];
177+
s.nextPermutation(nums);
178+
expect(nums, equals([5, 5, 2, 3, 4, 7]));
179+
});
180+
181+
/// From Leet Code
182+
test('From Leet Code 1: [2,2,7,5,4,3,2,2,1] → 2,3,1,2,2,2,4,5,7]', () {
183+
final nums = [2, 2, 7, 5, 4, 3, 2, 2, 1];
184+
s.nextPermutation(nums);
185+
expect(nums, equals([2, 3, 1, 2, 2, 2, 4, 5, 7]));
186+
});
187+
188+
test('From Leet Code 1: [2,3,1,3,3] → [2,3,3,1,3]', () {
189+
final nums = [2, 3, 1, 3, 3];
190+
s.nextPermutation(nums);
191+
expect(nums, equals([2, 3, 3, 1, 3]));
192+
});
193+
});
194+
}

0 commit comments

Comments
(0)

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