O(n^2) Python3 simple dict
Intuition\n\nthere were n^2 sub array\nyou can try all subarray with "start with i, append i+1, i+2 ... n-1"\n\n# Approach\nwhen you append a new n (nums[i+1])\
# Intuition\n\nthere were n^2 sub array\nyou can try all subarray with "start with i, append i+1, i+2 ... n-1"\n\n# Approach\nwhen you append a new n (nums[i+1])\n\n- if n in your dict:\n    the new number did not make change to imbalance number\n- if n+1, n-1 in dict:\n    the new number close a imbalance, imbalance -=1\n- if n+1, n-1 not in dict:\n    the new number make a new imbalance, imbalance += 1\n- if n+1 or n-1 in dict:\n    the new number did not make change to imbalance number\n\n\n# Complexity\n- Time complexity:\nO(N^2)\n\n- Space complexity:\ndictionary = O(N)\n\n# Code\n```\nclass Solution:\n    def sumImbalanceNumbers(self, nums: List[int]) -> int:\n        \n        ans = 0\n        \n        for i,start in enumerate(nums):\n            d = {}\n            d[start] = 1\n            cnt = 0\n            \n            for j in range(i+1, len(nums)):\n                n = nums[j]\n                if n in d:\n                    pass\n                else:\n                    if n+1 in d and n-1 in d:\n                        cnt -= 1\n                    elif n+1 not in d and n-1 not in d:\n                        cnt += 1\n                d[n] = 1\n                ans += cnt\n        \n        return ans\n    \n```
['Python3']