forked from SamirPaulb/DSAlgo
-
Notifications
You must be signed in to change notification settings - Fork 2
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
e1d6ab6
commit 0b8b6ca
Showing
4 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
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 |
---|---|---|
@@ -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/ | ||
|
||
|
||
|
||
|
||
|
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,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 |
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 |