The title

A sequence is an arithmetic sequence if it consists of at least two elements and the difference between each two consecutive elements is the same. More formally, the sequence S is an arithmetic sequence that satisfies only that: for every valid I, s[I +1] -s [I] == s[1] -s [0] is true.

For example, the following are all arithmetic sequences:

1, 3, 5, 7, 9 7, 7, 7 3, -1, -5, -9 The following sequences are not arithmetic:

1, 1, 2, 5, 7 gives you an array of n integers, nums, and two arrays of M integers, L and R. The last two arrays represent the m-range query, where the i-th query corresponds to the range [L [I], r[I]]. All arrays have subscripts starting at 0.

Return a list of answers made up of Boolean elements. If the subarray nums[l[I]], nums[l[I]+1… , nums[r[I]] can be rearranged to form arithmetic sequence, the value of answer[I] is true; Otherwise the value of answer[I] is false.

 

Example 1: input: nums = [4,6,5,9,3,7], l = [0,0,2], r = [2,3,5] output: [true,false,true] description: the 0th query corresponds to the subarray [4,6,5]. Can be rearranged as an arithmetic sequence [6,5,4]. The first query corresponds to the subarray [4,6,5,9]. Unable to rearrange to form an arithmetic sequence. The second query corresponds to the subarray [5,9,3,7]. Can be rearranged as an arithmetic sequence [3,5,7,9]. Example 2: input: nums = [- 12, 9, 3, and 12, 6,15,20, 25, 20, 15, 10], l =,1,6,4,8,7 [0], r =,4,9,7,9,10 [4] output: [false,true,false,false,true,true]Copy the code

Tip:

n == nums.length m == l.length m == r.length 2 <= n <= 500 1 <= m <= 500 0 <= l[i] < r[i] < n -105 <= nums[i] <= 105

Their thinking

class Solution: def checkArithmeticSubarrays(self, nums: List[int], l: List[int], r: List[int]) -> List[bool]: # fanweiList = [(l[I], r[i]) for i in range(len(l))] fanweiList = zip(l,r) resList = [] from collections import Counter for x,y in fanweiList: Sorted (nums[x:y+1]) # print(newNums) # print(newNums) # print(newNums) # print(newNums) ZipList = list(zip(newNums[:],newNums[1:])) dengchaList = [y-x for x, y in zipList] resCount = Counter(dengchaList) if len(resCount) == 1: resList.append(True) else: resList.append(False) return resList if __name__ == '__main__': ,6,5,9,3,7 nums = [4] = l,0,2 [0] r = ret,3,5 [2] = Solution () checkArithmeticSubarrays (nums, l, r) print (ret)Copy the code