Skip to content

Commit

Permalink
DSA
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPaulb committed Mar 28, 2022
1 parent e1d6ab6 commit 0b8b6ca
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
3 changes: 2 additions & 1 deletion 02_Dynamic-Programming/01. Integer Break.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def integerBreak(self, n):
i += 1
dp.append(max_product)
return dp[n]

# Time: O(n^2)
# Space: O(n)

# Method 3 ------ Mathematics -------
'''
Expand Down
8 changes: 8 additions & 0 deletions 08_Heap/02. Median of Running stream of Integers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# https://www.facebookrecruiting.com/portal/coding_practice_question/?problem_id=547645422524434&ppid=454615229006519&practice_plan=0
# https://practice.geeksforgeeks.org/problems/find-median-in-a-stream-1587115620/1
# https://www.interviewbit.com/blog/find-median-in-a-stream/





20 changes: 20 additions & 0 deletions 19_HashMap/05. Majority Element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# https://leetcode.com/problems/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 19_HashMap/06. Majority Element II.py
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

0 comments on commit 0b8b6ca

Please sign in to comment.