Skip to content

Commit b15c30c

Browse files
add: next permutation (ignacio-chiazzo#56)
2 parents e4c6481 + c146931 commit b15c30c

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
swap(nums, k, l);
49+
break;
50+
}
51+
}
52+
53+
reverse(nums, k + 1, nums.length -1);
54+
};
55+
56+
function swap(arr, a, b) {
57+
let temp = arr[a];
58+
arr[a] = arr[b];
59+
arr[b] = temp;
60+
}
61+
62+
function reverse(nums, start ,end) {
63+
while(start < end) {
64+
swap(nums, start, end);
65+
start++;
66+
end--;
67+
}
68+
}
69+
70+
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

+3-2
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ 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-
| [Reverse Integer](/LeetcodeProblems/Algorithms/Reverse_Integer.js) | Medium | https://leetcode.com/problems/reverse-integer/ |
66-
| [Time Needed to Rearrange a Binary String](/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js) | Medium | https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/ |
65+
| [Next Permutation](/LeetcodeProblems/Algorithms/Next_Permutation.js) | Medium | https://leetcode.com/problems/next-permutation/ |
66+
| [Time Needed to Rearrange a Binary String](/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js)| Medium | https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/ |
6767
| [Find Subarrays With Equal Sum ](/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sums.js) | Medium | https://leetcode.com/problems/find-subarrays-with-equal-sum/ |
68+
| [Reverse Integer](/LeetcodeProblems/Algorithms/Reverse_Integer.js) | Medium | https://leetcode.com/problems/reverse-integer/ |
6869
| [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ |
6970
| [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ |
7071
| [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)