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
15 changes: 15 additions & 0 deletions Two-sum_C1/two-sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
res=[]
numSet={}
for i in range(len(nums)):
if(target-nums[i] in numSet):
res.append(i)
res.append(numSet[target-nums[i]])
numSet.pop(target-nums[i])
else:
numSet[nums[i]]=i
return res


# TC=> O(n) and SC => O(n)
14 changes: 14 additions & 0 deletions best-time-to-buy-and-sell/best-time-to-buy-and-sell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def maxProfit(self, prices: List[int]) -> int:
l,r=0,1
res=0

while(r<len(prices)):
if(prices[r]>prices[l]):
res=max(res,prices[r]-prices[l])
else:
l=r
r+=1
return res if res!=0 else 0
# TC=o(n)
# SC=o(1)
9 changes: 9 additions & 0 deletions contains-duplicate/contains-duplicate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
numSet=set()
for num in nums:
if num in numSet:
return True
else:
numSet.add(num)
return False
18 changes: 18 additions & 0 deletions find-min-in-rotated-arry/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

class Solution:
def findMin(self, nums: List[int]) -> int:
res=nums[0]
l,r=0,len(nums)-1
while(l<=r):
if nums[l] < nums[r]:
res = min(res, nums[l])
break
m=(r+l)//2
res=min(res,nums[m])
if(nums[l]<=nums[m]):
l=m+1
else:
r=m-1
return res


15 changes: 15 additions & 0 deletions product-of-array-execpt-self/product-array-execpt-self.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
result=[1] *len(nums)
left = [1] *len(nums)
right = [1] *len(nums)
for i in range(1,len(nums)):
left[i]=nums[i-1]*left[i-1]
for j in range(len(nums)-2,-1,-1):
right[j] = nums[j+1]*right[j+1]
for k in range(len(nums)):
result[k]= left[k]*right[k]

return result
# TC: o(n)
# SC: 3o(n) -> o(n)
21 changes: 21 additions & 0 deletions search-in-rotated-sorted-array/search-in-rotated-sorted-arrya.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution:
def search(self, nums: List[int], target: int) -> int:
left,right=0,len(nums)-1
while (left<=right):
mid=(right+left)//2
if nums[mid]==target:
return mid
if nums[left]<=nums[mid]:
if nums[left] <=target <nums[mid]:
right=mid-1
else:
left=mid+1
else:
if nums[mid] < target <= nums[right]:
left = mid + 1
else:
right = mid - 1
return -1



7 changes: 7 additions & 0 deletions sum-of-integer/two-integer-sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution:
def getSum(self, a: int, b: int) -> int:
c=a+b
return c

# TC: o(n)
# SC: o(n)
19 changes: 19 additions & 0 deletions sum-of-product-array/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def maxProduct(self, arr: List[int]) -> int:
if not arr:
return 0

max_ending_here = arr[0]
min_ending_here = arr[0]
max_so_far = arr[0]

for i in range(1, len(arr)):
if arr[i] < 0:
max_ending_here, min_ending_here = min_ending_here, max_ending_here

max_ending_here = max(arr[i], max_ending_here * arr[i])
min_ending_here = min(arr[i], min_ending_here * arr[i])

max_so_far = max(max_so_far, max_ending_here)

return max_so_far
9 changes: 9 additions & 0 deletions sum-of-subarray/sum-of-subarry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
currSum=nums[0]
maxSum=nums[0]

for i in range(1,len(nums)):
currSum=max(nums[i],currSum+nums[i])
maxSum=max(currSum,maxSum)
return maxSum