การหาการเรียงลำดับถัดไปของอาร์เรย์ (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 an arrangement is impossible, the array must be rearranged in 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.
There are 2 lines of inputs.
- The first line is a number of items in the array.
- The second line is the integers in the array.
| Input | Output |
|---|---|
| 3 3 2 1 | 1 2 3 |
โค้ด
import java.util.Arrays;
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = scanner.nextInt();
}
if (n > 1) {
int i = n - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) {
i--;
}
if (i >= 0) {
int j = n - 1;
while (nums[j] <= nums[i]) {
j--;
}
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
int start = i + 1;
int end = n - 1;
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
for (int num : nums) {
System.out.print(num + " ");
}
}
}คำอธิบาย
ขั้นตอนที่ 1: หาตำแหน่งที่สามารถสลับเพื่อทำให้ค่าเพิ่มขึ้น
int i = n - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) {
i--;
}- เริ่มจากท้ายอาร์เรย์
- หาตำแหน่งแรกที่ตัวเลขน้อยกว่าตัวถัดไป
- ถ้าไม่พบ (i < 0) แสดงว่าเป็นการเรียงลำดับมากที่สุดแล้ว
ขั้นตอนที่ 2: หาตัวเลขที่เหมาะสมที่จะสลับ
if (i >= 0) {
int j = n - 1;
while (nums[j] <= nums[i]) {
j--;
}
// สลับตำแหน่ง i และ j
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}- หาตัวเลขที่มากกว่า nums[i] แต่น้อยที่สุดจากด้านขวา
- สลับตำแหน่งทั้งสอง
ขั้นตอนที่ 3: กลับด้านตัวเลขที่เหลือ
int start = i + 1;
int end = n - 1;
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}- กลับด้านตัวเลขทั้งหมดตั้งแต่ตำแหน่ง i+1 ถึงท้ายอาร์เรย์
- ทำให้ได้การเรียงลำดับที่น้อยที่สุดของส่วนที่เหลือ
แผนภาพการทำงาน
ตัวอย่างการทำงาน
กรณี 1: [1, 2, 3] → [1, 3, 2]
- พบ 2 < 3 ที่ตำแหน่ง i = 1
- สลับ 2 กับ 3
- ไม่ต้องกลับด้านเพราะมีตัวเลขเหลือแค่ตัวเดียว
กรณี 2: [2, 3, 1] → [3, 1, 2]
- พบ 2 < 3 ที่ตำแหน่ง i = 0
- สลับ 2 กับ 3
- กลับด้านตัวเลขที่เหลือ (3, 1)
กรณี 3: [3, 2, 1] → [1, 2, 3]
- ไม่พบตำแหน่งที่ตัวเลขน้อยกว่าตัวถัดไป
- กลับด้านทั้งอาร์เรย์