<p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p>

<p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p>

<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The 6 subarrays of nums are the following:
[1], range = largest - smallest = 1 - 1 = 0 
[2], range = 2 - 2 = 0
[3], range = 3 - 3 = 0
[1,2], range = 2 - 1 = 1
[2,3], range = 3 - 2 = 1
[1,2,3], range = 3 - 1 = 2
So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,3]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The 6 subarrays of nums are the following:
[1], range = largest - smallest = 1 - 1 = 0
[3], range = 3 - 3 = 0
[3], range = 3 - 3 = 0
[1,3], range = 3 - 1 = 2
[3,3], range = 3 - 3 = 0
[1,3,3], range = 3 - 1 = 2
So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [4,-2,-3,4,1]
<strong>Output:</strong> 59
<strong>Explanation:</strong> The sum of all subarray ranges of nums is 59.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>

class Solution:
    def func1(self, arr):
        n = len(arr)
        st1=[]
        left = [[None,None] for _ in range(n)]

        # let's find prev smaller

        for i in range(n):
            while st1 and st1[-1][0]>=arr[i]:
                st1.pop()
            
            left[i] = [-1,-1] if not st1 else st1[-1]
            # left[i][1] = st1[-1][1]
            st1.append([arr[i],i])
        
        print(left)


        # let's find next smaller
        st2=[]
        right = [[None,None] for _ in range(n)]

        for i in range(n-1,-1,-1):
            while st2 and st2[-1][0]>arr[i]:
                st2.pop()
            
            right[i] = [-1,-1] if not st2 else st2[-1]
            # right[i][1] = st2[-1][1]
            st2.append([arr[i],i])
        
        print(right)
        print("-"*20)

        # lst =[]
        # sm = 0

        # for i in range(n):
        #     from_left = left[i][1]
        #     from_right = right[i][1]

        #     if from_left>-1:
        #         left_dis = i - from_left # ok
        #     else:
        #         left_dis = i+1 # ok
            

        #     if from_right>-1:
        #         right_dis = from_right - i
        #     else:
        #         right_dis = n-i
            
        #     lst.append([left_dis, right_dis, left_dis*right_dis ])
        #     sm+= left_dis*right_dis*arr[i] 

        # print(lst)
            
        # return sm

        return left, right
    
    def func2(self, arr):
        n = len(arr)
        st1=[]
        left = [[None,None] for _ in range(n)]

        # let's find prev greater

        for i in range(n):
            while st1 and st1[-1][0]<=arr[i]:
                st1.pop()
            
            left[i] = [-1,-1] if not st1 else st1[-1]
            # left[i][1] = st1[-1][1]
            st1.append([arr[i],i])
        
        print(left)


        # let's find next greater
        st2=[]
        right = [[None,None] for _ in range(n)]

        for i in range(n-1,-1,-1):
            while st2 and st2[-1][0]<arr[i]:
                st2.pop()
            
            right[i] = [-1,-1] if not st2 else st2[-1]
            # right[i][1] = st2[-1][1]
            st2.append([arr[i],i])
        
        print(right)
        print("-"*20)

        # lst =[]
        # sm = 0

        # for i in range(n):
        #     from_left = left[i][1]
        #     from_right = right[i][1]

        #     if from_left>-1:
        #         left_dis = i - from_left # ok
        #     else:
        #         left_dis = i+1 # ok
            

        #     if from_right>-1:
        #         right_dis = from_right - i
        #     else:
        #         right_dis = n-i
            
        #     lst.append([left_dis, right_dis, left_dis*right_dis ])
        #     sm+= left_dis*right_dis*arr[i] 

        # print(lst)
            

        # return sm

        return left,right


    def subArrayRanges(self, arr: List[int]) -> int:
        left_s, right_s = self.func1(arr)
        left_g, right_g = self.func2(arr)

        # print(left_s, right_s)
        # print(left_g, right_g)

        lst =[]
        sm = 0
        n = len(arr)
        for i in range(n):
            from_left_s = left_s[i][1]
            from_right_s = right_s[i][1]

            if from_left_s>-1:
                left_dis_s = i - from_left_s # ok
            else:
                left_dis_s = i+1 # ok
            

            if from_right_s>-1:
                right_dis_s = from_right_s - i
            else:
                right_dis_s = n-i
            
            # lst.append([left_dis_s, right_dis_s, left_dis_s*right_dis_s ])

            sm1 = left_dis_s*right_dis_s*arr[i]

            #-----------------------------------------------------

            from_left_g = left_g[i][1]
            from_right_g = right_g[i][1]

            if from_left_g>-1:
                left_dis_g = i - from_left_g # ok
            else:
                left_dis_g = i+1 # ok
            

            if from_right_g>-1:
                right_dis_g = from_right_g - i
            else:
                right_dis_g = n-i

            sm2= left_dis_g*right_dis_g*arr[i] 

            sm+= sm2-sm1

        # print(lst)
            

        return sm
128