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
098f5f1
commit cd485ad
Showing
5 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
54 changes: 54 additions & 0 deletions
54
30-Days-SDE-Sheet-Practice/03. Day 3 Arrays Part-III/Count Reverse Pairs.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,54 @@ | ||
# https://leetcode.com/problems/reverse-pairs/ | ||
# https://youtu.be/S6rsAlj_iB4 | ||
''' | ||
use the merge sort concept of Inversion of Array | ||
https://github.com/SamirPaul1/DSAlgo/blob/main/30-Days-SDE-Sheet-Practice/02.%20Day%202%20Arrays%20Part-II/Inversion%20of%20Array%20-using%20Merge%20Sort.py | ||
''' | ||
class Solution: | ||
def __init__(self): | ||
self.count = 0 | ||
|
||
def reversePairs(self, nums: List[int]) -> int: | ||
self.mergeSort(nums) | ||
return self.count | ||
|
||
def mergeSort(self, nums): | ||
if len(nums) > 1: | ||
# calculate mid | ||
mid = len(nums) // 2 | ||
# divide the input array in to right and left | ||
left = nums[:mid] | ||
right = nums[mid:] | ||
|
||
self.mergeSort(left) | ||
self.mergeSort(right) | ||
|
||
# the tricky part - updating the count of number of possible pairs | ||
j = 0 | ||
for i in range(len(left)): | ||
while j < len(right) and left[i] > 2 * right[j]: | ||
j += 1 | ||
self.count += j | ||
|
||
# merge two sorted array | ||
i = j = k = 0 | ||
while i < len(left) and j < len(right): | ||
if left[i] <= right[j]: | ||
nums[k] = left[i] | ||
k += 1 | ||
i += 1 | ||
else: | ||
nums[k] = right[j] | ||
k += 1 | ||
j += 1 | ||
while i < len(left): | ||
nums[k] = left[i] | ||
k += 1 | ||
i += 1 | ||
while j < len(right): | ||
nums[k] = right[j] | ||
k += 1 | ||
j += 1 | ||
|
||
# Time: O(n log(n)) | ||
# Space: O(n) |
24 changes: 24 additions & 0 deletions
24
...-Sheet-Practice/03. Day 3 Arrays Part-III/Majority Element (greater than N by 2 times).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,24 @@ | ||
# https://leetcode.com/problems/majority-element/ | ||
''' | ||
take an assumed majority element and count of that element. if current element equal then increase the count | ||
else decrease by 1. the element present at the end will be the majority element. | ||
''' | ||
|
||
class Solution: | ||
def majorityElement(self, nums: List[int]) -> int: | ||
me = -1 # me = Majority Element | ||
count = 0 # count = Count of Majority Element | ||
|
||
for i in range(len(nums)): | ||
if nums[i] == me: | ||
count += 1 | ||
elif count > 0 and nums[i] != me: | ||
count -= 1 | ||
|
||
if count == 0: | ||
me = nums[i] | ||
count = 1 | ||
|
||
if nums.count(me) >= len(nums) // 2: | ||
return me | ||
return -1 |
29 changes: 29 additions & 0 deletions
29
...-Sheet-Practice/03. Day 3 Arrays Part-III/Majority Element (greater than N by 3 times).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,29 @@ | ||
# https://leetcode.com/problems/majority-element-ii/ | ||
|
||
class Solution: | ||
def majorityElement(self, nums: List[int]) -> List[int]: | ||
# Note: *There will be only two majority element*. suppose there are 3 majority element then count of each element will be n/3 and there will be only there 3 unique elements in the array. But question is elements that appear *more than ⌊ n/3 ⌋ times*. | ||
n = len(nums) | ||
me1 = -1 # Majority element 1 | ||
me2 = -2 # Majority element 2 | ||
count1 = 0 # Count of Majority element 1 | ||
count2 = 0 # Count of Majority element 2 | ||
|
||
for num in nums: | ||
if num == me1: count1 += 1 | ||
|
||
elif num == me2: count2 += 1 | ||
|
||
elif count1 == 0: me1 = num; count1 = 1 | ||
|
||
elif count2 == 0: me2 = num; count2 = 1 | ||
|
||
else: | ||
count1 -= 1 | ||
count2 -= 1 | ||
|
||
res = set() # taking set instead of list as me1 and me2 can be equal | ||
if nums.count(me1) > n // 3: res.add(me1) | ||
if nums.count(me2) > n // 3: res.add(me2) | ||
|
||
return res |
21 changes: 21 additions & 0 deletions
21
30-Days-SDE-Sheet-Practice/03. Day 3 Arrays Part-III/Unique Paths.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,21 @@ | ||
# https://leetcode.com/problems/unique-paths/ | ||
|
||
class Solution: | ||
def uniquePaths(self, m: int, n: int) -> int: | ||
''' | ||
def solve(m, n): | ||
if m == 1 or n == 1: return 1 | ||
return solve(m, n-1) + solve(m-1, n) | ||
return solve(m, n) | ||
''' | ||
# converting above recursive solution into DP | ||
# filling with 1 so we don't need to write loops for base case | ||
dp = [[1]*(n+1) for i in range(m+1)] | ||
|
||
# as for m == 1 or n == 1 path is 1 so starting from (2,2) | ||
for i in range(2, m+1): | ||
for j in range(2, n+1): | ||
dp[i][j] = dp[i][j-1] + dp[i-1][j] | ||
|
||
return dp[-1][-1] # last cell contains all paths |