Similar Problems

Similar Problems not available

Minimum Adjacent Swaps To Reach The Kth Smallest Number - Leetcode Solution

Companies:

LeetCode:  Minimum Adjacent Swaps To Reach The Kth Smallest Number Leetcode Solution

Difficulty: Medium

Topics: greedy string two-pointers  

Problem:

Given a positive integer n and a permutation of numbers from 1 to n, find the minimum number of adjacent swaps required to sort this permutation and make the kth smallest number.

Example 1:

Input: nums = [3, 1, 2, 4], k = 2 Output: 1 Explanation: After the first swap (1, 3), the permutation becomes [1, 3, 2, 4]. The 2nd smallest number is now 2.

Example 2:

Input: nums = [1, 2, 3, 4], k = 2 Output: 0 Explanation: Since the permutation is already sorted, no swaps are needed. The 2nd smallest number is 2.

Solution:

The problem can be solved in O(n^2) time complexity using a naive approach of checking all possible adjacent swaps. But this will not be efficient for larger values of n. Therefore, we need to come up with an optimized approach to solve the problem.

We can solve the problem using three steps:

  1. Find the kth smallest number.

  2. Construct the target permutation.

  3. Find the minimum number of adjacent swaps required to sort the given permutation to the target permutation.

Step 1: Find the kth smallest number

To find the kth smallest number, we can sort the permutation and return the kth element. To optimize the sorting process, we can use quicksort or merge sort algorithms, which have O(nlogn) time complexity.

Step 2: Construct the target permutation

The target permutation can be constructed by sorting the given permutation and taking the kth element as the pivot. We can partition the elements using the pivot and construct the target permutation. This can be done in O(n) time complexity.

Step 3: Find the minimum number of adjacent swaps required

We can find the minimum number of adjacent swaps required to sort the given permutation to the target permutation by using cycle sort algorithm. The cycle sort algorithm swaps the elements into their correct position in a cyclic manner. We need to count the number of swaps required to achieve the target permutation.

The time complexity of cycle sort algorithm is O(n^2), but it is efficient in practice for smaller values of n.

Therefore, the overall time complexity of the solution is O(nlogn) + O(n) + O(n^2), which can be simplified to O(n^2).

Code:

Here is the python code for the solution:

def find_kth_smallest(nums, k): sorted_nums = sorted(nums) return sorted_nums[k-1]

def construct_target_permutation(nums, k): pivot = find_kth_smallest(nums, k) target_permutation = []

for num in nums:
    if num < pivot:
        target_permutation.append(num)
target_permutation += [pivot] * (nums.count(pivot))

for num in nums:
    if num > pivot:
        target_permutation.append(num)

return target_permutation

def minimum_adjacent_swaps(nums, k): target_permutation = construct_target_permutation(nums, k) n = len(nums) visited = [False] * n swaps = 0

for i in range(n):
    if visited[i] or nums[i] == target_permutation[i]:
        continue
        
    cycles = 0
    j = i
    
    while not visited[j]:
        visited[j] = True
        cycles += 1
        j = nums.index(target_permutation[j])
    
    if cycles > 0:
        swaps += cycles - 1

return swaps

Example

nums = [3, 1, 2, 4] k = 2 print(minimum_adjacent_swaps(nums, k)) # Output: 1

Explanation: After the first swap (1, 3), the permutation becomes [1, 3, 2, 4]. The 2nd smallest number is now 2. Therefore, only 1 swap is required.

Example

nums = [1, 2, 3, 4] k = 2 print(minimum_adjacent_swaps(nums, k)) # Output: 0

Explanation: Since the permutation is already sorted, no swaps are needed. The 2nd smallest number is 2.

Minimum Adjacent Swaps To Reach The Kth Smallest Number Solution Code

1