From 9735775d746008ad00a6003b152e7b3371f034cd Mon Sep 17 00:00:00 2001 From: zzu <90972641+Zuhye@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:18:54 +0900 Subject: [PATCH] 2104. Sum of Subarray Ranges --- .../2104. Sum of Subarray Ranges" | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 "leetcode3/\354\206\241\354\243\274\355\230\234/2104. Sum of Subarray Ranges" 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