-
Notifications
You must be signed in to change notification settings - Fork 43
/
find-greatest-common-divisor-of-array.py
79 lines (59 loc) · 1.74 KB
/
find-greatest-common-divisor-of-array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
1979. Find Greatest Common Divisor of Array
Easy
Given an integer array nums, return the greatest common divisor of the smallest number and largest number in nums.
The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers.
Example 1:
Input: nums = [2,5,6,9,10]
Output: 2
Explanation:
The smallest number in nums is 2.
The largest number in nums is 10.
The greatest common divisor of 2 and 10 is 2.
Example 2:
Input: nums = [7,5,6,8,3]
Output: 1
Explanation:
The smallest number in nums is 3.
The largest number in nums is 8.
The greatest common divisor of 3 and 8 is 1.
Example 3:
Input: nums = [3,3]
Output: 3
Explanation:
The smallest number in nums is 3.
The largest number in nums is 3.
The greatest common divisor of 3 and 3 is 3.
Constraints:
2 <= nums.length <= 1000
1 <= nums[i] <= 1000
"""
# V0
# V1
# https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1459270/python-solution
class Solution:
def findGCD(self, nums):
min_ = min(nums)
max_ = max(nums)
for i in range(min_, 1, -1):
if not max_ % i and not min_ % i:
return i
return 1
# V1'
# https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1442477/Python-Euclidean-algorithm
class Solution:
def findGCD(self, nums):
a, b = min(nums), max(nums)
while a:
a, b = b % a, a
return b
# V1''
# https://leetcode.com/problems/find-greatest-common-divisor-of-array/discuss/1418724/python-easy-to-undetstand
class Solution:
def findGCD(self, nums):
maxNum = max(nums)
minNum = min(nums)
for i in range(minNum, 0, -1):
if maxNum % i == 0 and minNum % i == 0:
return i
# V2