Skip to content

Commit

Permalink
--
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb committed May 8, 2022
1 parent ebbd3d4 commit 8944938
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# https://leetcode.com/problems/maximum-subarray/
'''
continuously adding new elements to cs and also checking if the current value is greater or not.
if greater then start from current element.
'''

class Solution:
def maxSubArray(self, nums: List[int]) -> int:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# https://leetcode.com/problems/next-permutation/
'''
Next Permutation = elemtnt Just Greater than the current element.
So find the elemnt from traversing from end. if nums[i] > nums[i-1] we can swap these and get the
value. but there may any greater element in right.
'''

class Solution:
def nextPermutation(self, nums: List[int]) -> None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

'''
only one transaction posible.
find difference of right max and left min
'''
class Solution:
def maxProfit(self, prices: List[int]) -> int:
curMin = prices[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
The slow pointer moves by one step and the fast pointer moves by 2 steps and there exists a cycle so the first collision is bound to happen.
Then start a pointer from 0 and another pointer from current slow's position.
The point where both collide will be the duplicate element.
Then start a check pointer from 0 and another pointer from current slow's position.
The value where both collide will be the duplicate element.
'''

class Solution:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'''
Use Merge Sort to reduce the time complexity from O(n^2) to O(n log(n))
In the merge function we check if the left array pointer is greater than right array pointer
then all elements right of left array pointer will be greater as both of the arrays are sorted.
then all elements right-side of left array pointer will be greater as both of the arrays are sorted.
'''

from os import *
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# https://leetcode.com/problems/merge-intervals/

# check values of two consecutive elements with 2 pointers
# sort based on the 0th index element value
# check values of two consecutive elements with 2 pointers. if overlapping then change 1st index of
# 1st pointer with max and pop 2nd pointer
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals.sort(key = lambda x : x[0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
else:
nums1[m+n-1] = nums2[n-1]
n -= 1

while n > 0:
nums1[n-1] = nums2[n-1]
n -= 1

return nums1

# Time: O(N)
Expand All @@ -29,7 +29,7 @@ def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
Approach:
Initially take the gap as ceil() of (m+n)/2
Take as a pointer1 = 0 and pointer2 = gap.
Run a oop from pointer1 & pointer2 to m+n and whenever arr[pointer2] < arr[pointer1], swap those.
Run a loop from pointer1 & pointer2 to m+n and whenever arr[pointer2] < arr[pointer1], swap those.
After completion of the loop reduce the gap as gap = gap // 2.
Repeat the process until gap > 0.
'''
Expand Down
3 changes: 0 additions & 3 deletions 30-Days-SDE-Sheet-Practice/03. Day 3 Arrays Part-III/01.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@



Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# https://leetcode.com/problems/search-a-2d-matrix/

'''Approach:
First use binary search to find the row in which the target is present.
Then apply binary search to the target row to check whether target present in targetRow or not.
'''
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
# find target row
t = 0 # top row pointer
b = len(matrix) - 1 # bottom row pointer
while t <= b:
mid = (t + b) // 2
if target < matrix[mid][0]:
b = mid - 1
elif target > matrix[mid][-1]:
t = mid + 1
else:
break

# Now mid if the target row
targetRow = mid
l = 0
r = len(matrix[0]) - 1
while l <= r:
mid = (l + r) // 2
if target < matrix[targetRow][mid]:
r = mid - 1
elif target > matrix[targetRow][mid]:
l = mid + 1
else:
return True

return False

# Time: O(log(n) + log(m)) = O(log(m*n))
# Space: O(1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# https://leetcode.com/problems/powx-n/
# Using Binary Exponentiation

class Solution:
def myPow(self, x: float, n: int) -> float:
res = 1
p = n
if p < 0: p *= -1

while p:
if p % 2 == 0:
x = x*x
p /= 2
else:
res *= x
p -= 1

if n < 0: return 1 / res
return res

# Time: O(log(n)) # as get devided each time
# Space: O(1)

0 comments on commit 8944938

Please sign in to comment.