<p>You are given two positive integer arrays <code>nums</code> and <code>target</code>, of the same length.</p>

<p>In a single operation, you can select any subarray of <code>nums</code> and increment each element within that subarray by 1 or decrement each element within that subarray by 1.</p>

<p>Return the <strong>minimum</strong> number of operations required to make <code>nums</code> equal to the array <code>target</code>.</p>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,5,1,2], target = [4,6,2,4]</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p>We will perform the following operations to make <code>nums</code> equal to <code>target</code>:<br />
- Increment&nbsp;<code>nums[0..3]</code> by 1, <code>nums = [4,6,2,3]</code>.<br />
- Increment&nbsp;<code>nums[3..3]</code> by 1, <code>nums = [4,6,2,4]</code>.</p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,2], target = [2,1,4]</span></p>

<p><strong>Output:</strong> <span class="example-io">5</span></p>

<p><strong>Explanation:</strong></p>

<p>We will perform the following operations to make <code>nums</code> equal to <code>target</code>:<br />
- Increment&nbsp;<code>nums[0..0]</code> by 1, <code>nums = [2,3,2]</code>.<br />
- Decrement&nbsp;<code>nums[1..1]</code> by 1, <code>nums = [2,2,2]</code>.<br />
- Decrement&nbsp;<code>nums[1..1]</code> by 1, <code>nums = [2,1,2]</code>.<br />
- Increment&nbsp;<code>nums[2..2]</code> by 1, <code>nums = [2,1,3]</code>.<br />
- Increment&nbsp;<code>nums[2..2]</code> by 1, <code>nums = [2,1,4]</code>.</p>
</div>

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

<ul>
	<li><code>1 &lt;= nums.length == target.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i], target[i] &lt;= 10<sup>8</sup></code></li>
</ul>

class Solution:
    def minimumOperations(self, nums: List[int], target: List[int]) -> int:
        n = len(nums)
        deltas = [0] * n
        for i in range(n):
            deltas[i] = target[i] - nums[i]

        def countOps(arr):
            total = 0
            monostack = []
            # print("arr", arr)
            for n in arr:
                if n < 0:
                    n *= -1
                # print("processing", n)
                # print(monostack)
                if monostack and n < monostack[-1]:
                    total += (monostack[-1] - n)
                while monostack and n < monostack[-1]:
                    monostack.pop()
                monostack.append(n)
                # print(total)
            
            total += monostack[0]
            for i in range(1, len(monostack)):
                total += monostack[i] - monostack[i-1]
            
            return total
            
        
        minOps = 0
        i = j = 0
        prevSign = 0 # enum -1, 0, 1 
        while j < n:
            currSign = 1 if deltas[j] > 0 else -1 if deltas[j] < 0 else 0
            # print("turn", j, prevSign, currSign)
            if prevSign != 0 and prevSign != currSign:
                # extras = abs(sum(deltas[i:j])) - (j-i)
                # minOps += 1 + extras    
                minOps += countOps(deltas[i:j])
                i = j
            elif prevSign == 0:
                i = j
            
            prevSign = currSign
            j += 1
            # print("minOps", minOps)

        # extras = abs(sum(deltas[i:j])) - (j-i)
        # minOps += 1 + extras
        # print("curr", minOps)
        minOps += countOps(deltas[i:j])
        
        return minOps
            
30900