diff --git "a/leetcode3/\354\206\241\354\243\274\355\230\234/2104. Sum of Subarray Ranges" "b/leetcode3/\354\206\241\354\243\274\355\230\234/2104. Sum of Subarray Ranges" new file mode 100644 index 00000000..422a8e1f --- /dev/null +++ "b/leetcode3/\354\206\241\354\243\274\355\230\234/2104. Sum of Subarray Ranges" @@ -0,0 +1,30 @@ +class Solution: + def subArrayRanges(self, nums: list[int]) -> int: + result = 0 + n = len(nums) + + for i in range(n): + + # 각각의 최댓값 부분배열 개수 + l = i + while l > 0 and nums[l - 1] < nums[i]: + l -= 1 + + r = i + while r < n-1 and nums[r+1] <= nums[i]: + r +=1 + + result += nums[i] * (i-l+1)*(r-i+1) + + # 각각의 최솟값 부분배열 개수 + l = i + while l > 0 and nums[l - 1] > nums[i]: + l -= 1 + + r = i + while r < n-1 and nums[r+1] >= nums[i]: + r +=1 + + result -= nums[i] * (i-l+1)*(r-i+1) + + return result