Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

next greater permutation leetcode

/*
	This is an implementation that demonstrates
	how to efficiently find the next greater
	permutation for a list of values.
	For example, next greater permutation for:
	- [1, 2, 3] -> [1, 3, 2]
	- [2, 3, 1] -> [3, 1, 2]

	Let n be the number of values in the list.

	Time complexity: O(n) 
	Space complexity: O(1)
*/
import java.util.Arrays;

public class NextGreaterPermutation {
	public static void main(String[] args) {
		int[] nums = { 1, 2, 3 };
		nextPermutation(nums);
		System.out.println(Arrays.toString(nums)); // [1, 3, 2]
		nums = new int[] { 2, 3, 1 };
		nextPermutation(nums);
		System.out.println(Arrays.toString(nums)); // [3, 1, 2]
	}

	public static void nextPermutation(int[] nums) {
		int i = -1;
		// Find position of rightmost value
		// that is smaller than its successor
		for (int j = nums.length - 2; j >= 0; j--) {
			if (nums[j] < nums[j + 1]) {
				i = j;
				break;
			}
		}
		if (i == -1) {
			// This is the max combination
			// We need to sort it in ascending order
			reverse(nums, 0);
			return;
		}
		// Find rightmost value greater than the one
		// at index i and swap it with the one at i
		for (int j = nums.length - 1; j > i; j--) {
			if (nums[j] > nums[i]) {
				swap(nums, i, j);
				break;
			}
		}
		// Reverse the elements to the right of i
		reverse(nums, i + 1);
	}

	private static void reverse(int[] nums, int start) {
		int i = start, j = nums.length - 1;
		while (i < j) {
			swap(nums, i, j);
			i++;
			j--;
		}
	}

	private static void swap(int[] nums, int i, int j) {
		int temp = nums[i];
		nums[i] = nums[j];
		nums[j] = temp;
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: java secretkey 
Java :: initial data for list java 
Java :: str.substring last 2 java 
Java :: generatedvalue spring boot 
Java :: string tokenizer java 
Java :: arrays in java 
Java :: byte array to file java 
Java :: map string string to java object 
Java :: discord jda 
Java :: how to find location of java jdk 
Java :: tic tac toe in java 
Java :: java get number out of string 
Java :: retainall java 
Java :: how to convert int to string in java? 
Java :: java string static arrat 
Java :: java get relative path 
Java :: intent android 
Java :: how to find the size of a queue in java 
Java :: how to find some of digits in java 
Java :: get the length of an array java 
Java :: create array of particular length in java 
Java :: java sort int array 
Java :: java long to hours minutes and seconds 
Java :: big java 2010 heaptree java program 
Java :: generic list java 
Java :: java print item to text file 
Java :: java display message 
Java :: HUFFMAN CODING IN JAVA 
Java :: pyramid pattern program in java 
Java :: convert integer array to string array 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =