파비의 매일매일 공부기록

2023.11.24 Today's Challenge 본문

Problem Solving/LeetCode

2023.11.24 Today's Challenge

fabichoi 2023. 11. 24. 23:45

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

 

Arithmetic Subarrays - LeetCode

Can you solve this real interview question? Arithmetic Subarrays - A sequence of numbers is called arithmetic if it consists of at least two elements, and the difference between every two consecutive elements is the same. More formally, a sequence s is ari

leetcode.com

오랫만에 짧은 시간동안 스스로 풀어봄

class Solution:
    def checkArithmeticSubarrays(self, nums: List[int], l: List[int], r: List[int]) -> List[bool]:
        result = []
        for i in range(len(l)):
            num = nums[l[i]:r[i]+1]
            num.sort()
            if len(num) < 2:
                result.append(True)
                continue
            diff = num[1] - num[0]
            is_found = False
            for j in range(len(num) - 1):
                if num[j+1] - num[j] != diff:
                    result.append(False)
                    is_found = True
                    break
            if not is_found:
                result.append(True)
        return result
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.11.26 Today's Challenge  (0) 2023.11.26
2023.11.25 Today's Challenge  (2) 2023.11.25
2023.11.23 Today's Challenge  (1) 2023.11.23
2023.11.22 Today's Challenge  (0) 2023.11.22
2023.11.21 Today's Challenge  (0) 2023.11.21
Comments