Just count it carefully! Time 98%, Memory 95%
Intuition\nAll the subarrays ending at nums[i] are subarrays ending at nums[i-1] with [nums[i]]\u3002 Hence, we only need to check how a subarray will change if
# Intuition\nAll the subarrays ending at ```nums[i]``` are subarrays ending at ```nums[i-1]``` with ```[nums[i]]```\u3002 Hence, we only need to check how a subarray will change if we put  ```nums[i]``` into it. Only two cases are needed attention:\n1.  ```nums[i]``` splits an interval longer than $2$ into two intervals, both of which are longer than $2$, (inbalance number ```+= 1```)\n2.  ```nums[i]``` splits an interval of length $2$ into two intervals of lengths $1$. (inbalance number ```-= 1```)\nThen, summating them together.\n\n# Code\n```\nclass Solution:\n    def sumImbalanceNumbers(self, nums: List[int]) -> int:\n        ans = 0\n        last = 0\n        dist = {}\n        for i in range(len(nums)):\n            a,b,c = -1,-1,-1\n            if nums[i]+1 in dist:\n                a = dist[nums[i]+1]\n            if nums[i] in dist:\n                b = dist[nums[i]]\n            if nums[i]-1 in dist:\n                c = dist[nums[i] - 1]\n            last += i - 1 - max(a,b,c)\n            if c > b and a > b:\n                last -= min(a,c) - b\n            ans += last\n            dist[nums[i]] = i\n            #print(dist,a,b,c,ans)\n        return ans\n\n            \n            \n```
['Python3']