From f82d4e103663f8115ce883dc076138fda01e4486 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 4 Jan 2025 18:25:35 +0000 Subject: [PATCH 1/5] Added rotate_array.py --- data_structures/arrays/rotate_array.py | 130 +++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 data_structures/arrays/rotate_array.py diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py new file mode 100644 index 000000000000..6d5033fb7b7c --- /dev/null +++ b/data_structures/arrays/rotate_array.py @@ -0,0 +1,130 @@ +""" +Given an integer array nums and a non-negative integer k, rotate +nums to the right by k places. + +LeetCode Link: https://leetcode.com/problems/rotate-array/ + +4 Different Implementations +""" + +from collections import deque + + +# Time: O(n * k) Space: O(1) +def rotate_array_nk_time(nums: list[int], k: int) -> list[int]: + """ + Takes the last k integers, and one by one, inserts them at the start of the array. + + >>> rotate_array_nk_time([-1, 7, 25, 6], 3) + [7, 25, 6, -1] + >>> rotate_array_nk_time([4, 14, 2, -6, -9, 1, -37], 2) + [1, -37, 4, 14, 2, -6, -9] + >>> rotate_array_nk_time([19, -2, 5, 5, 11], 5) + [19, -2, 5, 5, 11] + >>> rotate_array_nk_time([1, 2, 4, 1, 2, 4], 3) + [1, 2, 4, 1, 2, 4] + >>> rotate_array_nk_time([8, -7, 22, -6], 9) + [-6, 8, -7, 22] + """ + + length = len(nums) + k = k % length + curr = length - k + for i in range(k): + dummy = nums[curr] + for j in range(curr - 1, i - 1, -1): + nums[j + 1] = nums[j] + + nums[i] = dummy + curr += 1 + + return nums + + +# Time: O(n) Space: O(n) +def rotate_array_n_space(nums: list[int], k: int) -> list[int]: + """ + Creates new array consisting of last k integers in nums followed by remaining + integers in nums. + + >>> rotate_array_n_space([-1, 7, 25, 6], 3) + [7, 25, 6, -1] + >>> rotate_array_n_space([4, 14, 2, -6, -9, 1, -37], 2) + [1, -37, 4, 14, 2, -6, -9] + >>> rotate_array_n_space([19, -2, 5, 5, 11], 5) + [19, -2, 5, 5, 11] + >>> rotate_array_n_space([1, 2, 4, 1, 2, 4], 3) + [1, 2, 4, 1, 2, 4] + >>> rotate_array_n_space([8, -7, 22, -6], 9) + [-6, 8, -7, 22] + """ + + return nums[-k:] + nums[:k] + + +# Time: O(n) Space: O(k) +def rotate_array_k_space(nums: list[int], k: int) -> list[int]: + """ + Uses a deque buffer of size k to shift each integer in nums by k places. + + >>> rotate_array_k_space([-1, 7, 25, 6], 3) + [7, 25, 6, -1] + >>> rotate_array_k_space([4, 14, 2, -6, -9, 1, -37], 2) + [1, -37, 4, 14, 2, -6, -9] + >>> rotate_array_k_space([19, -2, 5, 5, 11], 5) + [19, -2, 5, 5, 11] + >>> rotate_array_k_space([1, 2, 4, 1, 2, 4], 3) + [1, 2, 4, 1, 2, 4] + >>> rotate_array_k_space([8, -7, 22, -6], 9) + [-6, 8, -7, 22] + """ + + length = len(nums) + k = k % length + buffer = deque(nums[length - k :]) + for i in range(length): + buffer.append(nums[i]) + nums[i] = buffer.popleft() + + return nums + + +# Time: O(n) Space: O(1) +def rotate_array_constant_space(nums: list[int], k: int) -> list[int]: + """ + Reverse whole array. Then reverse first k elements. Then reverse remaining elements. + + >>> rotate_array_constant_space([-1, 7, 25, 6], 3) + [7, 25, 6, -1] + >>> rotate_array_constant_space([4, 14, 2, -6, -9, 1, -37], 2) + [1, -37, 4, 14, 2, -6, -9] + >>> rotate_array_constant_space([19, -2, 5, 5, 11], 5) + [19, -2, 5, 5, 11] + >>> rotate_array_constant_space([1, 2, 4, 1, 2, 4], 3) + [1, 2, 4, 1, 2, 4] + >>> rotate_array_constant_space([8, -7, 22, -6], 9) + [-6, 8, -7, 22] + """ + + length = len(nums) + k = k % length + + def reverse(left: int, right: int) -> None: + while left < right: + temp = nums[left] + nums[left] = nums[right] + nums[right] = temp + left += 1 + right -= 1 + + reverse(0, length - 1) + reverse(0, k - 1) + reverse(k, length - 1) + + return nums + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From bca5e52671802e4ea1eba84412c9090eaca260a1 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 4 Jan 2025 18:30:11 +0000 Subject: [PATCH 2/5] Added rotate_array.py --- data_structures/arrays/rotate_array.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py index 6d5033fb7b7c..8635f35a169b 100644 --- a/data_structures/arrays/rotate_array.py +++ b/data_structures/arrays/rotate_array.py @@ -58,7 +58,8 @@ def rotate_array_n_space(nums: list[int], k: int) -> list[int]: >>> rotate_array_n_space([8, -7, 22, -6], 9) [-6, 8, -7, 22] """ - + length = len(nums) + k = k % length return nums[-k:] + nums[:k] From 90b278080cb4dc85a88946de4500b936d51440db Mon Sep 17 00:00:00 2001 From: David Date: Sat, 4 Jan 2025 18:35:33 +0000 Subject: [PATCH 3/5] Added rotate_array.py --- data_structures/arrays/rotate_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py index 8635f35a169b..3d07c8ba47e4 100644 --- a/data_structures/arrays/rotate_array.py +++ b/data_structures/arrays/rotate_array.py @@ -60,7 +60,7 @@ def rotate_array_n_space(nums: list[int], k: int) -> list[int]: """ length = len(nums) k = k % length - return nums[-k:] + nums[:k] + return nums[-k:] + nums[: length - k] # Time: O(n) Space: O(k) From 610f42396ee64c54a38f67830880c2574abbab8f Mon Sep 17 00:00:00 2001 From: David Date: Sat, 4 Jan 2025 18:39:49 +0000 Subject: [PATCH 4/5] Added rotate_array.py --- data_structures/arrays/rotate_array.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py index 3d07c8ba47e4..e659d1e3a5f4 100644 --- a/data_structures/arrays/rotate_array.py +++ b/data_structures/arrays/rotate_array.py @@ -58,9 +58,18 @@ def rotate_array_n_space(nums: list[int], k: int) -> list[int]: >>> rotate_array_n_space([8, -7, 22, -6], 9) [-6, 8, -7, 22] """ + length = len(nums) k = k % length - return nums[-k:] + nums[: length - k] + new_nums = [] + + for i in range(length - k, length): + new_nums.append(nums[i]) + + for i in range(length - k): + new_nums.append(nums[i]) + + return new_nums # Time: O(n) Space: O(k) From b532190a484fed5aaa16cb8ea410443d4c51937b Mon Sep 17 00:00:00 2001 From: David Date: Tue, 13 May 2025 20:51:36 +0100 Subject: [PATCH 5/5] Formatting --- data_structures/arrays/rotate_array.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py index e659d1e3a5f4..5460b49d36c1 100644 --- a/data_structures/arrays/rotate_array.py +++ b/data_structures/arrays/rotate_array.py @@ -61,15 +61,8 @@ def rotate_array_n_space(nums: list[int], k: int) -> list[int]: length = len(nums) k = k % length - new_nums = [] - for i in range(length - k, length): - new_nums.append(nums[i]) - - for i in range(length - k): - new_nums.append(nums[i]) - - return new_nums + return nums[length - k :] + nums[: length - k] # Time: O(n) Space: O(k)