From 4072c96fa69f1ddc8e4b2cf8ff977da9036f30cb Mon Sep 17 00:00:00 2001 From: ongch Date: Thu, 6 Oct 2022 11:51:00 +0800 Subject: [PATCH 1/2] add: next permutation --- .../Algorithms/Next_Permutation.js | 68 +++++++++++++++++++ .../Algorithms/Next_Permutation_Test.js | 16 +++++ README.md | 1 + 3 files changed, 85 insertions(+) create mode 100644 LeetcodeProblems/Algorithms/Next_Permutation.js create mode 100644 LeetcodeProblemsTests/Algorithms/Next_Permutation_Test.js diff --git a/LeetcodeProblems/Algorithms/Next_Permutation.js b/LeetcodeProblems/Algorithms/Next_Permutation.js new file mode 100644 index 0000000..79c14e6 --- /dev/null +++ b/LeetcodeProblems/Algorithms/Next_Permutation.js @@ -0,0 +1,68 @@ +/** +Next Permutation +https://leetcode.com/problems/next-permutation/ + +A permutation of an array of integers is an arrangement of its members into a sequence or linear order. + +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]. +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). + +For example, the next permutation of arr = [1,2,3] is [1,3,2]. +Similarly, the next permutation of arr = [2,3,1] is [3,1,2]. +While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement. +Given an array of integers nums, find the next permutation of nums. + +The replacement must be in place and use only constant extra memory. + +Example 1: +Input: nums = [1,2,3] +Output: [1,3,2] + +Example 2: +Input: nums = [3,2,1] +Output: [1,2,3] + +Example 3: +Input: nums = [1,1,5] +Output: [1,5,1] + * + */ + +/** + * @param {number[]} nums + * @return {void} Do not return anything, modify nums in-place instead. + */ + + var nextPermutation = function(nums) { + let k = nums.length - 2; + while ( k >= 0 && nums[k] >= nums[k + 1]) { + --k; + } + if (k === -1) { + reverse(nums, 0, nums.length-1); + return; + } + + for (let l = nums.length - 1; l > k; l--) { + if (nums[l] > nums[k]) { + let temp = nums[k]; + nums[k] = nums[l]; + nums[l] = temp; + break; + } + } + + reverse(nums, k + 1, nums.length -1); +}; + +function reverse(nums, start ,end) { + while(start < end) { + let temp = nums[start]; + nums[start] = nums[end]; + nums[end] = temp; + start++; + end--; + } +} + +module.exports.nextPermutation = nextPermutation; diff --git a/LeetcodeProblemsTests/Algorithms/Next_Permutation_Test.js b/LeetcodeProblemsTests/Algorithms/Next_Permutation_Test.js new file mode 100644 index 0000000..e14a5e0 --- /dev/null +++ b/LeetcodeProblemsTests/Algorithms/Next_Permutation_Test.js @@ -0,0 +1,16 @@ +const assert = require('assert'); +const nextPermutation = require('../../LeetcodeProblems/Algorithms/Next_Permutation').nextPermutation; + +function test() { + let test1 = [1,2,3]; + let test2 = [3,2,1]; + let test3 = [1,1,5]; + nextPermutation(test1); + nextPermutation(test2); + nextPermutation(test3); + assert.deepEqual(test1, [1,3,2]); + assert.deepEqual(test2, [1,2,3]); + assert.deepEqual(test3, [1,5,1]); +} + +module.exports.test = test; diff --git a/README.md b/README.md index 6085d84..da7a6e0 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ To run a specific problem in your console run `node ` (e.g. | [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/ | | [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/ | | [Maximum Sum of an Hourglass](/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js) | Medium | https://leetcode.com/problems/maximum-sum-of-an-hourglass/ | +| [Next Permutation](/LeetcodeProblems/Algorithms/Next_Permutation.js) | Medium | https://leetcode.com/problems/next-permutation/ | | [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ | | [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ | | [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/ | From f31f1ce1dcfb091c0582fe4c2b7fac927976ef63 Mon Sep 17 00:00:00 2001 From: ongch Date: Thu, 6 Oct 2022 21:50:17 +0800 Subject: [PATCH 2/2] refactor: swap function --- LeetcodeProblems/Algorithms/Next_Permutation.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/LeetcodeProblems/Algorithms/Next_Permutation.js b/LeetcodeProblems/Algorithms/Next_Permutation.js index 79c14e6..e125d7f 100644 --- a/LeetcodeProblems/Algorithms/Next_Permutation.js +++ b/LeetcodeProblems/Algorithms/Next_Permutation.js @@ -45,9 +45,7 @@ Output: [1,5,1] for (let l = nums.length - 1; l > k; l--) { if (nums[l] > nums[k]) { - let temp = nums[k]; - nums[k] = nums[l]; - nums[l] = temp; + swap(nums, k, l); break; } } @@ -55,11 +53,15 @@ Output: [1,5,1] reverse(nums, k + 1, nums.length -1); }; +function swap(arr, a, b) { + let temp = arr[a]; + arr[a] = arr[b]; + arr[b] = temp; +} + function reverse(nums, start ,end) { while(start < end) { - let temp = nums[start]; - nums[start] = nums[end]; - nums[end] = temp; + swap(nums, start, end); start++; end--; }