Skip to content

Commit 51053f1

Browse files
authored
Create arithmetic-subarrays.py
1 parent d88bdc5 commit 51053f1

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Python/arithmetic-subarrays.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Time: O(n * q)
2+
# Space: O(n)
3+
4+
import itertools
5+
6+
7+
class Solution(object):
8+
def checkArithmeticSubarrays(self, nums, l, r):
9+
"""
10+
:type nums: List[int]
11+
:type l: List[int]
12+
:type r: List[int]
13+
:rtype: List[bool]
14+
"""
15+
def is_arith(n):
16+
mx, mn, lookup = max(n), min(n), set(n)
17+
if mx == mn:
18+
return True
19+
d, r = divmod(mx-mn, len(n)-1)
20+
if r:
21+
return False
22+
return all(i in lookup for i in xrange(mn, mx, d))
23+
24+
result = []
25+
for left, right in itertools.izip(l, r):
26+
result.append(is_arith(nums[left:right+1]))
27+
return result

0 commit comments

Comments
 (0)