From 18addc6438c27a2a2e546a845df01698ca919569 Mon Sep 17 00:00:00 2001 From: Deven <63876261+devenperez@users.noreply.github.com> Date: Sun, 20 Oct 2024 13:25:57 -0400 Subject: [PATCH] Time: 11 ms (98.98%), Space: 24.7 MB (99.25%) - LeetHub --- 0189-rotate-array/0189-rotate-array.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 0189-rotate-array/0189-rotate-array.py diff --git a/0189-rotate-array/0189-rotate-array.py b/0189-rotate-array/0189-rotate-array.py new file mode 100644 index 0000000..645e3ee --- /dev/null +++ b/0189-rotate-array/0189-rotate-array.py @@ -0,0 +1,24 @@ +class Solution: + def rotate(self, nums: List[int], k: int) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + + k = k % len(nums) + + buffer = nums[-k:] + buffI = 0 + for i in range(len(nums)): + tmp = nums[i] + nums[i] = buffer[buffI] + buffer[buffI] = tmp + buffI = (buffI + 1) % len(buffer) + + + # O(kn) time / O(1) space -- too slow + # for _ in range(k): + # lastNum = nums[-1] + # for i in range(len(nums)): + # tmp = nums[i] + # nums[i] = lastNum + # lastNum = tmp