본문 바로가기

IT/코딩테스트

[Leetcode] 1630. Arithmetic Subarrays

728x90

https://leetcode.com/problems/arithmetic-subarrays

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

class Solution:
    def checkArithmeticSubarrays(self, nums: List[int], l: List[int], r: List[int]) -> List[bool]:
        result = []

        for i in range(0, len(l)):
            subarray = nums[l[i]:r[i]+1]
            subarray.sort()

            if len(subarray) < 2:
                result.append(False)
                break
            
            d = subarray[1] - subarray[0]

            arithmetic = True
            for j in range(1, len(subarray)):
                if subarray[j] - subarray[j-1] != d:
                    arithmetic = False
                    break
            
            result.append(arithmetic)

        return result
728x90