diff --git a/189_RotateArray.py b/189_RotateArray.py new file mode 100644 index 00000000..ae04d8e8 --- /dev/null +++ b/189_RotateArray.py @@ -0,0 +1,23 @@ +""" +TC: O(n) +SC: O(1) +3 reverse operations +reverse(0, n-k-1) +reverse(n-k,n-1) +reverse(0,n-1) +""" +class Solution: + def rotate(self, nums: List[int], k: int) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + def reverse(start, end, nums): + while(start int: + citations.sort() + n = len(citations) + l = 0 + h = len(citations)-1 + while(l<=h): + m = l+(h-l)//2 + if citations[m]==n-m: + return citations[m] + elif citations[m]>n-m: + h=m-1 + else: + l = m+1 + return n-l \ No newline at end of file diff --git a/42_TrappingRainWater.py b/42_TrappingRainWater.py new file mode 100644 index 00000000..5f090f73 --- /dev/null +++ b/42_TrappingRainWater.py @@ -0,0 +1,28 @@ +""" +TC: O(n) +SC: O(1) +Logic: 2 pointer method. Maintain a left and right max +""" +class Solution: + def trap(self, height: List[int]) -> int: + leftMax = rightMax = 0 + l = 0 + r = len(height)-1 + total_water = 0 + while(lleftMax: + leftMax = height[l] + else: + total_water+= leftMax-height[l] + l+=1 + else: + #process right side + if height[r]>rightMax: + rightMax = height[r] + else: + total_water+= rightMax - height[r] + r-=1 + return total_water \ No newline at end of file