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
30 changes: 30 additions & 0 deletions Hindex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution(object):
# tc. : O(n) for bucket sort and again parsign to get the sum
# sc : O(n), bucket array
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
# our hindex wont be greater than, total number of papers and any citation greater than total number of papers are considered greater and no need to be sorted
# only the elements from 0 to len of citations need to be sorted and rest can be considered the same value
# so instead of sortign whoel array wil be bucket sort
# once bucket sorted will iteratre from the last and take sumation and check if the summation value is greater than the index and that's out hindex

# bucket sort
n = len(citations)
buckets = [0]*(n+1)

for i in range(n):
if citations[i] < n:
buckets[citations[i]] +=1
else:
buckets[n] +=1

sum = 0
for i in range(n,-1,-1):
sum += buckets[i]
if sum >= i:
return i

return -1
27 changes: 27 additions & 0 deletions Trappingrainwater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution(object):
def trap(self, height):
# tc : O(n)
# sc : O(1)
"""
:type height: List[int]
:rtype: int
"""
# water is trapped when there is a wall which is greater than the current height
# so will use two pointers from left and right and check the which height is smaller as we can stoee only that much water
# and keep moving the pointers

left , right = 0, len(height)-1
left_max, right_max = 0,0 # these will act the walls
ans = 0

while left < right : # until we have explored all the heights
if height[left] < height[right]:
left_max = max(left_max,height[left]) # every time we have to check whats the max height we have seen so far
ans += left_max - height[left] # water trapped
left +=1
else:
right_max = max(right_max, height[right]) # if right side is smaller then the water is trapped here
ans += right_max - height[right]
right -= 1

return ans
33 changes: 33 additions & 0 deletions rotatearraybyk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution(object):
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: None Do not return anything, modify nums in-place instead.
"""
# tc : o(n), sc : O(1)
# rotate arra by k steps : first reverse the rigth part of the array, then left part and then whole array
# right_index is k
# left arr = n-k

n = len(nums)

if k > n :
k = k%n

left_index = n-k

nums[:n-k] = self.reverse(nums[:n-k])
nums[n-k:n] = self.reverse(nums[n-k:n])
nums = self.reverse(nums)

def reverse (self, arr):
i = 0
j = len(arr)-1

while (i < j ):
arr[i] , arr[j] = arr[j], arr[i]
i +=1
j -=1

return arr