파비의 매일매일 공부기록

2023.03.04 Today's Challenge 본문

Problem Solving/LeetCode

2023.03.04 Today's Challenge

fabichoi 2023. 3. 4. 23:45

https://leetcode.com/problems/count-subarrays-with-fixed-bounds/

 

Count Subarrays With Fixed Bounds - LeetCode

Can you solve this real interview question? Count Subarrays With Fixed Bounds - You are given an integer array nums and two integers minK and maxK. A fixed-bound subarray of nums is a subarray that satisfies the following conditions: * The minimum value in

leetcode.com

투 포인터로 푸는 문제.
슬라이딩 윈도우와 투 포인터가 꽤 많이 나오는 솔루션 중에 하나 인듯.

class Solution:
    def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
        answer = 0
        minp = maxp = lb = -1

        for i, n in enumerate(nums):
            if n < minK or n > maxK:
                lb = i
            if n == minK:
                minp = i
            if n == maxK:
                maxp = i
            answer += max(0, min(minp, maxp) - lb)
        
        return answer
반응형

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

2023.03.06 Today's Challenge  (0) 2023.03.06
2023.03.05 Today's Challenge  (0) 2023.03.05
2023.03.03 Today's Challenge  (0) 2023.03.03
2023.03.02 Today's Challenge  (0) 2023.03.02
2023.03.01 Today's Challenge  (0) 2023.03.01
Comments