Skip to content

Commit e42b7e1

Browse files
committed
Greedy
1 parent dc59b01 commit e42b7e1

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

Greedy/majorityElement.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
Given an array of size n, find the majority element. The majority element is the element that appears more than floor(n/2) times.
3+
4+
You may assume that the array is non-empty and the majority element always exist in the array.
5+
6+
Example :
7+
8+
Input : [2, 1, 2]
9+
Return : 2 which occurs 2 times which is greater than 3/2.
10+
'''
11+
import math
12+
def majority(arr):
13+
n = len(arr)
14+
res = 0
15+
for i in arr:
16+
if arr.count(i) > math.floor(n/2):
17+
res = i
18+
break
19+
return res
20+
21+
print majority([2,1,2])

Math/Greates-common-divisor.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
GCD of 2 integers m and n is defined as the greatest integer g such that g is a divisor of both m and n.
55
Both m and n fit in a 32 bit signed integer.
66
'''
7-
def gcd(self, A, B):
8-
9-
if B > A:
10-
11-
A, B = B, A
12-
while B:
13-
14-
A , B = B, A%B
15-
return A
7+
def gcd(a,b):
8+
if a < b:
9+
a, b = b, a
10+
while b:
11+
a, b = b, a%b
12+
return a

0 commit comments

Comments
 (0)