"""Leetcode 189. Rotate Array
Easy

URL: https://leetcode.com/problems/rotate-array/

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

Example 1:
Input: [1,2,3,4,5,6,7] and 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: [-1,-100,3,99] and 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]

Note:
- Try to come up as many solutions as you can, 
  there are at least 3 different ways to solve this problem.
- Could you do it in-place with O(1) extra space?
"""

class SolutionCopy(object):
    def rotate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: None Do not return anything, modify nums in-place instead.

        Time complexity: O(n).
        Space complexity: O(n).
        """
        # If k >= n, truncate k by n.
        n = len(nums)
        k = k % n

        # Append right with left.
        nums[:] = nums[-k:] + nums[:-k]


class SolutionIter(object):
    def rotate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: None Do not return anything, modify nums in-place instead.

        Time complexity: O(n).
        Space complexity: O(n).
        """
        # If k >= n, truncate k by n.
        n = len(nums)
        k = k % n

        count = 0
        start_pos, cur_pos = 0, 0

        while count < n:
            next_pos = (cur_pos + k) % n
            temp_num = nums[next_pos]
            # TODO: 2019/08/30
            nums[new] = rotate_num
            rotate_num = temp_num

            while current != start:


                count += 1
                print nums

            start += 1
            current = s

        nums[new_id] = old_num


def main():
    # Ans: [5,6,7,1,2,3,4]
    nums = [1,2,3,4,5,6,7]
    k = 3
    print nums
    # SolutionCopy().rotate(nums, k)
    SolutionIter().rotate(nums, k)
    print nums

    # Ans: [3,99,-1,-100]
    nums = [-1,-100,3,99]
    k = 2
    print nums
    # SolutionCopy().rotate(nums, k)
    SolutionIter().rotate(nums, k)
    print nums

    # Ans: [2,1]
    nums = [1,2]
    k = 3
    print nums
    # SolutionCopy().rotate(nums, k)
    SolutionIter().rotate(nums, k)
    print nums


if __name__ == '__main__':
    main()

@@ -1396,33 +1396,33 @@
 e complexity: O(
-n
+1
 ).%0A        %22%22%22%0A 
@@ -1491,32 +1491,76 @@
      k = k %25 n%0A%0A
+        # Start from the 0th.%0A        s = 0%0A
         count = 
@@ -1561,24 +1561,25 @@
 unt = 0%0A
+%0A
         
 start_po
@@ -1574,92 +1574,174 @@
     
-start_pos, cur_pos = 0, 0%0A%0A        while count %3C n:%0A            next_pos = (
+# Iterate through nums to rotate them until all is updated.%0A        while count %3C n:%0A            # Backup current, and update current by the ith.%0A            
 cur_pos 
 + k)
@@ -1736,16 +1736,21 @@
 cur_pos 
+= (s 
 + k) %25 n
@@ -1762,20 +1762,19 @@
         
-temp
+cur
 _num = n
@@ -1781,210 +1781,382 @@
 ums%5B
-next_pos%5D%0A            # TODO: 2019/08/30%0A            nums%5Bnew%5D = rotate_num%0A            rotate_num = temp_num%0A%0A            while current != start:%0A%0A%0A                count += 1%0A                print nums
+cur_pos%5D%0A            nums%5Bcur_pos%5D = nums%5Bs%5D%0A            %0A            count += 1%0A            j = cur_pos%0A%0A            # If there is no cycle, keep rotating starting from next current.%0A            while j != s and count %3C n:%0A                cur_pos = (j + k) %25 n%0A                # Swap current and next current.%0A                nums%5Bcur_pos%5D, cur_num = cur_num, nums%5Bcur_pos%5D
 %0A%0A  
@@ -2165,20 +2165,24 @@
         
-star
+    coun
 t += 1%0A 
@@ -2196,51 +2196,74 @@
     
-current = s%0A%0A        nums%5Bnew_id%5D = old_num
+    j = cur_pos%0A%0A            # Increment start.%0A            s += 1
 %0A%0A%0Ad
@@ -2338,32 +2338,40 @@
  k = 3%0A    print
+ 'Raw:',
  nums%0A    # Solu
@@ -2433,32 +2433,43 @@
 ms, k)%0A    print
+ 'Rotated',
  nums%0A%0A    # Ans
@@ -2522,32 +2522,40 @@
  k = 2%0A    print
+ 'Raw:',
  nums%0A    # Solu
@@ -2621,24 +2621,35 @@
 k)%0A    print
+ 'Rotated',
  nums%0A%0A    #
@@ -2692,24 +2692,32 @@
  3%0A    print
+ 'Raw:',
  nums%0A    # 
@@ -2791,16 +2791,27 @@
    print
+ 'Rotated',
  nums%0A%0A%0A

Complete iter sol w/ time/space complexity