forked from SamirPaulb/DSAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ebbd3d4
commit 8944938
Showing
10 changed files
with
81 additions
and
12 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
30-Days-SDE-Sheet-Practice/01. Day 1 Arrays/Kadane’s Algorithm.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
30-Days-SDE-Sheet-Practice/01. Day 1 Arrays/Next Permutation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
...ce/01. Day 1 Arrays/Stock buy and Sell.py → ...and Sell only one transaction possible.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
30-Days-SDE-Sheet-Practice/02. Day 2 Arrays Part-II/Merge Overlapping Subintervals.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +0,0 @@ | ||
|
||
|
||
|
||
37 changes: 37 additions & 0 deletions
37
30-Days-SDE-Sheet-Practice/03. Day 3 Arrays Part-III/Search in a 2d Matrix.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
22 changes: 22 additions & 0 deletions
22
...tice/03. Day 3 Arrays Part-III/x raised to the power n without using built-in function.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |