Skip to content

Commit 9c50821

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.9 MB (19.04%)
1 parent 1afe0db commit 9c50821

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

0031-next-permutation/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<p>A <strong>permutation</strong> of an array of integers is an arrangement of its members into a sequence or linear order.</p>
2+
3+
<ul>
4+
<li>For example, for <code>arr = [1,2,3]</code>, the following are all the permutations of <code>arr</code>: <code>[1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1]</code>.</li>
5+
</ul>
6+
7+
<p>The <strong>next permutation</strong> of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the <strong>next permutation</strong> of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).</p>
8+
9+
<ul>
10+
<li>For example, the next permutation of <code>arr = [1,2,3]</code> is <code>[1,3,2]</code>.</li>
11+
<li>Similarly, the next permutation of <code>arr = [2,3,1]</code> is <code>[3,1,2]</code>.</li>
12+
<li>While the next permutation of <code>arr = [3,2,1]</code> is <code>[1,2,3]</code> because <code>[3,2,1]</code> does not have a lexicographical larger rearrangement.</li>
13+
</ul>
14+
15+
<p>Given an array of integers <code>nums</code>, <em>find the next permutation of</em> <code>nums</code>.</p>
16+
17+
<p>The replacement must be <strong><a href="http://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in place</a></strong> and use only constant extra memory.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> nums = [1,2,3]
24+
<strong>Output:</strong> [1,3,2]
25+
</pre>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> nums = [3,2,1]
31+
<strong>Output:</strong> [1,2,3]
32+
</pre>
33+
34+
<p><strong class="example">Example 3:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> nums = [1,1,5]
38+
<strong>Output:</strong> [1,5,1]
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
<p><strong>Constraints:</strong></p>
43+
44+
<ul>
45+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
46+
<li><code>0 &lt;= nums[i] &lt;= 100</code></li>
47+
</ul>

0031-next-permutation/solution.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Approach 2: Single Pass Approach
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def nextPermutation(self, nums: List[int]) -> None:
8+
"""
9+
Do not return anything, modify nums in-place instead.
10+
"""
11+
# Step 1: Find the first pair from right where nums[i] < nums[i+1]
12+
i = len(nums) - 2
13+
while i >= 0 and nums[i + 1] <= nums[i]:
14+
i -= 1
15+
16+
if i >= 0:
17+
# Step 2: Find the smallest number greater than nums[i] to its right
18+
j = len(nums) - 1
19+
while nums[j] <= nums[i]:
20+
j -= 1
21+
22+
# Step 3: Swap nums[i] and nums[j]
23+
nums[i], nums[j] = nums[j], nums[i]
24+
25+
# Step 4: Reverse the subarray after position i
26+
nums[i + 1 :] = reversed(nums[i + 1 :])
27+

0 commit comments

Comments
 (0)