Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions 189_RotateArray.py
Original file line number Diff line number Diff line change
@@ -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<end):
nums[start], nums[end] = nums[end], nums[start]
start+=1
end-=1
n = len(nums)
k = k%n
reverse(0,n-k-1,nums)
reverse(n-k,n-1,nums)
reverse(0,n-1,nums)
20 changes: 20 additions & 0 deletions 274_HIndex2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
TC: O(nlogn)+O(log n) ~ O(nlogn)
SC: O(1)
Sort and binary search
"""
class Solution:
def hIndex(self, citations: List[int]) -> 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
28 changes: 28 additions & 0 deletions 42_TrappingRainWater.py
Original file line number Diff line number Diff line change
@@ -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(l<r):
#process smaller height first
if height[l]<height[r]:
#process left side
if height[l]>leftMax:
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