혼자 풀어본 느린 O(nlogn) 솔루션class Solution: def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int: m,M,ob = [],[],[] n = len(nums) for i in range(n): if nums[i] == minK: m.append(i) if nums[i] == maxK: M.append(i) if nums[i] < minK or maxK < nums[i]: ob.append(i) def next(arr, cur): if len(arr) == 0: return -1 n = len(arr) l,r = 0,n-1 if cur < arr[0]: return arr[0] if cur >= arr[-1]: return -1 while l + 1 < r: m = (l+r) // 2 if arr[m] > cur: r = m else: l = m return arr[r] res = 0 if minK == maxK: for i in range(n): x = nums[i] if x == minK: nextob = next(ob,i) if nextob == -1: nextob = n res += max(0, nextob - i) return res for i in range(n): x = nums[i] if x == minK: nextM = next(M,i) nextob = next(ob,i) if nextM != -1: if nextob == -1: nextob = n res += max(0, nextob - nextM) elif x == maxK: nextm = next(m,i) nextob = next(ob,i) if nextm != -1: if nextob == -1: nextob = n res += max(0, nextob - nextm) elif minK < x < maxK: nextm = next(m,i) nextM = next(M,i) nextob = next(ob,i) if nextm != -1 and nextM != -1: if nextob == -1: nextob = n res += max(0, nextob - max(nextm, nextM)) return res
댓글 영역
획득법
① NFT 발행
작성한 게시물을 NFT로 발행하면 일주일 동안 사용할 수 있습니다. (최초 1회)
② NFT 구매
다른 이용자의 NFT를 구매하면 한 달 동안 사용할 수 있습니다. (구매 시마다 갱신)
사용법
디시콘에서지갑연결시 바로 사용 가능합니다.