Skip to content

Commit 4072c96

Browse files
author
ongch
committed
add: next permutation
1 parent 675f694 commit 4072c96

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
Next Permutation
3+
https://leetcode.com/problems/next-permutation/
4+
5+
A permutation of an array of integers is an arrangement of its members into a sequence or linear order.
6+
7+
For example, for arr = [1,2,3], the following are all the permutations of arr: [1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1].
8+
The next permutation 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 next permutation 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).
9+
10+
For example, the next permutation of arr = [1,2,3] is [1,3,2].
11+
Similarly, the next permutation of arr = [2,3,1] is [3,1,2].
12+
While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.
13+
Given an array of integers nums, find the next permutation of nums.
14+
15+
The replacement must be in place and use only constant extra memory.
16+
17+
Example 1:
18+
Input: nums = [1,2,3]
19+
Output: [1,3,2]
20+
21+
Example 2:
22+
Input: nums = [3,2,1]
23+
Output: [1,2,3]
24+
25+
Example 3:
26+
Input: nums = [1,1,5]
27+
Output: [1,5,1]
28+
*
29+
*/
30+
31+
/**
32+
* @param {number[]} nums
33+
* @return {void} Do not return anything, modify nums in-place instead.
34+
*/
35+
36+
var nextPermutation = function(nums) {
37+
let k = nums.length - 2;
38+
while ( k >= 0 && nums[k] >= nums[k + 1]) {
39+
--k;
40+
}
41+
if (k === -1) {
42+
reverse(nums, 0, nums.length-1);
43+
return;
44+
}
45+
46+
for (let l = nums.length - 1; l > k; l--) {
47+
if (nums[l] > nums[k]) {
48+
let temp = nums[k];
49+
nums[k] = nums[l];
50+
nums[l] = temp;
51+
break;
52+
}
53+
}
54+
55+
reverse(nums, k + 1, nums.length -1);
56+
};
57+
58+
function reverse(nums, start ,end) {
59+
while(start < end) {
60+
let temp = nums[start];
61+
nums[start] = nums[end];
62+
nums[end] = temp;
63+
start++;
64+
end--;
65+
}
66+
}
67+
68+
module.exports.nextPermutation = nextPermutation;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const assert = require('assert');
2+
const nextPermutation = require('../../LeetcodeProblems/Algorithms/Next_Permutation').nextPermutation;
3+
4+
function test() {
5+
let test1 = [1,2,3];
6+
let test2 = [3,2,1];
7+
let test3 = [1,1,5];
8+
nextPermutation(test1);
9+
nextPermutation(test2);
10+
nextPermutation(test3);
11+
assert.deepEqual(test1, [1,3,2]);
12+
assert.deepEqual(test2, [1,2,3]);
13+
assert.deepEqual(test3, [1,5,1]);
14+
}
15+
16+
module.exports.test = test;

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
6262
| [Construct Binary Tree from Preorder and Inorder Traversal ](/LeetcodeProblems/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js) | Medium | https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ |
6363
| [Lowest Common Ancestor of a Binary Tree ](/LeetcodeProblems/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree.js) | Medium | https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ |
6464
| [Maximum Sum of an Hourglass](/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js) | Medium | https://leetcode.com/problems/maximum-sum-of-an-hourglass/ |
65+
| [Next Permutation](/LeetcodeProblems/Algorithms/Next_Permutation.js) | Medium | https://leetcode.com/problems/next-permutation/ |
6566
| [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ |
6667
| [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ |
6768
| [Number of Segments in a String ](/LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String.js) | Easy | https://leetcode.com/problems/number-of-segments-in-a-string/ |

0 commit comments

Comments
 (0)