let's try this again shall we

problems 1->10 of LC150
This commit is contained in:
2025-06-03 20:23:08 -07:00
parent ac27bdd840
commit 151d0214ff
10 changed files with 166 additions and 0 deletions

22
189-rotate-array/soln.py Normal file
View File

@@ -0,0 +1,22 @@
def rotate(self, nums: list[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
# O(n) space and time
# n = len(nums)
# k %= n
# nums[:] = nums[n-k:] + nums[:n-k]
# O(n) time, O(1) space
# reverse full - then reverse chunks
def partrev(arr, low, high):
while low < high:
arr[low], arr[high] = arr[high], arr[low]
low += 1
high -= 1
n = len(nums)
k %= n
partrev(nums, 0, n - 1)
partrev(nums, 0, k - 1)
partrev(nums, k, n - 1)