Problem · Graph
Create Lexicographically Largest Permutation
Given an undirected connected graph of g_nodes and M connections,
traverse all nodes at least once, and store the order of traversal in A.
Now create the lexicographically largest array B which is a permutation of A.
Complete the function createLexicographicallyLargestPermutation in the editor.
createLexicographicallyLargestPermutation has the following parameters:
int g_from[]: an array of integers representing the starting nodesint g_to[]: an array of integers representing the ending nodes
Returns
int[]: the lexicographically largest permutation array B
Examples
01 · Example 1
g_from = [4, 5, 1, 4, 3] g_to = [5, 1, 4, 3, 2] return = [5, 4, 3, 2, 1]
This and the following 2 explanations are not from the official, so be careful when reading them :)
The order of traversal A is [5, 4, 3, 2, 3, 4, 1].
The lexicographically largest permutation B is [5, 4, 3, 2, 1].
02 · Example 2
g_from = [3, 3] g_to = [1, 2] return = [3, 2, 1]
The order of traversal A can be [3, 1, 3, 2] or any other traversal that visits all nodes at least once.
The lexicographically largest permutation B is [3, 2, 1].
03 · Example 3
g_from = [1, 2, 3, 2, 1] g_to = [2, 3, 4, 4, 4] return = [4, 3, 2, 1]
The order of traversal A can be [1, 2, 3, 4, 2, 1, 4, 4] or any other traversal that visits all nodes at least once.
The lexicographically largest permutation B is [4, 3, 2, 1].
Constraints
🫎🫎More Oracle problems
public int[] createLexicographicallyLargestPermutation(int[] g_from, int[] g_to) {
// write your code here
}
g_from[4, 5, 1, 4, 3]
g_to[5, 1, 4, 3, 2]
expected[5, 4, 3, 2, 1]
sign in to submit