-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add next permutation problem (#720)
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// A practice to find lexicographically next greater permutation of the given array of integers. | ||
// If there does not exist any greater permutation, then print the lexicographically smallest permutation of the given array. | ||
// The implementation below, finds the next permutation in linear time and constant memory and returns in place | ||
// Useful reference: https://www.geeksforgeeks.org/next-permutation/ | ||
|
||
package permutation | ||
|
||
func NextPermutation(nums []int) { | ||
pivot := 0 | ||
for pivot = len(nums) - 2; pivot >= 0; pivot-- { | ||
if nums[pivot] < nums[pivot+1] { | ||
break | ||
} | ||
} | ||
if pivot < 0 { | ||
// current permutation is the last and must be reversed totally | ||
for l, r := 0, len(nums)-1; l < r; l, r = l+1, r-1 { | ||
nums[l], nums[r] = nums[r], nums[l] | ||
} | ||
} else { | ||
succ := 0 | ||
for succ = len(nums) - 1; succ > pivot; succ = succ - 1 { | ||
if nums[succ] > nums[pivot] { | ||
break | ||
} | ||
} | ||
|
||
// Swap the pivot and successor | ||
nums[pivot], nums[succ] = nums[succ], nums[pivot] | ||
|
||
// Reverse the suffix part to minimize it | ||
for l, r := pivot+1, len(nums)-1; l < r; l, r = l+1, r-1 { | ||
nums[l], nums[r] = nums[r], nums[l] | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package permutation | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestNextPermutation(t *testing.T) { | ||
var nextPermutationTestData = []struct { | ||
description string | ||
numbers []int | ||
next []int | ||
}{ | ||
{ | ||
description: "Basic case", | ||
numbers: []int{1, 2, 3}, | ||
next: []int{1, 3, 2}, | ||
}, | ||
{ | ||
description: "Should reverse the whole slice", | ||
numbers: []int{3, 2, 1}, | ||
next: []int{1, 2, 3}, | ||
}, | ||
{ | ||
description: "A more complex test", | ||
numbers: []int{2, 4, 1, 7, 5, 0}, | ||
next: []int{2, 4, 5, 0, 1, 7}, | ||
}, | ||
} | ||
for _, test := range nextPermutationTestData { | ||
t.Run(test.description, func(t *testing.T) { | ||
NextPermutation(test.numbers) | ||
|
||
if !reflect.DeepEqual(test.numbers, test.next) { | ||
t.Logf("FAIL: %s", test.description) | ||
t.Fatalf("Expected result:%v\nFound: %v", test.next, test.numbers) | ||
} | ||
}) | ||
} | ||
} |