LeetCode-in-All

189. Rotate Array

Medium

Given an array, rotate the array to the right by k steps, where k is non-negative.

Example 1:

Input: nums = [1,2,3,4,5,6,7], k = 3

Output: [5,6,7,1,2,3,4]

Explanation:

rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4] 

Example 2:

Input: nums = [-1,-100,3,99], k = 2

Output: [3,99,-1,-100]

Explanation:

rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100] 

Constraints:

Follow up:

Solution

class Solution {
    func rotate(_ nums: inout [Int], _ k: Int) {
        guard nums.count > 1, k > 0, k != nums.count else {
            return
        }

        let requiredRotationsCount = k % nums.count
        let pivotIndex = nums.count - requiredRotationsCount

        reverse(&nums, startIndex: nums.startIndex, endIndex: nums.endIndex - 1)
        reverse(&nums, startIndex: nums.startIndex, endIndex: requiredRotationsCount - 1)
        reverse(&nums, startIndex: requiredRotationsCount, endIndex: nums.endIndex - 1)

    }

    func reverse(_ nums: inout [Int], startIndex: Int, endIndex: Int) {
        var startIndex = startIndex
        var endIndex = endIndex

        while endIndex > startIndex {
            let temp = nums[startIndex]
            nums[startIndex] = nums[endIndex]
            nums[endIndex] = temp
            startIndex += 1
            endIndex -= 1
        }
    }
}