Skip to content

Commit 09d7ae3

Browse files
authored
Create can-make-arithmetic-progression-from-sequence.py
1 parent 755e29f commit 09d7ae3

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def canMakeArithmeticProgression(self, arr):
6+
"""
7+
:type arr: List[int]
8+
:rtype: bool
9+
"""
10+
m = min(arr)
11+
d = (max(arr)-m)//(len(arr)-1)
12+
if not d:
13+
return True
14+
i = 0
15+
while i < len(arr):
16+
if arr[i] == m+i*d:
17+
i += 1
18+
else:
19+
j, r = divmod(arr[i]-m, d)
20+
if r or j >= len(arr) or arr[i] == arr[j]:
21+
return False
22+
arr[i], arr[j] = arr[j], arr[i]
23+
return True

0 commit comments

Comments
 (0)