Leetcode Problem 2717. Semi-Ordered Permutation

2717. Semi-Ordered Permutation

Leetcode Solutions

Calculate Swaps for and n

  1. Find the index of 1 and store it in index_of_1.
  2. Find the index of n and store it in index_of_n.
  3. Calculate the number of swaps to move 1 to the beginning: swaps_for_1 = index_of_1.
  4. Calculate the number of swaps to move n to the end: swaps_for_n = (n - 1) - index_of_n.
  5. If index_of_1 is greater than index_of_n, subtract one from the total swaps since one swap will move both 1 and n.
  6. Return the total number of swaps required.
UML Thumbnail

Sequential Swapping

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...