Runtime 416 ms Beats 11.43% Analyze Complexity
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int sumImbalanceNumbers(int[] nums) {\n        int n = nums.length;\n        int ans = 0;\n        for (int i = 0; i < n; ++i) {\n            TreeMap<Integer, Integer> tm = new TreeMap<>();\n            int cnt = 0;\n            for (int j = i; j < n; ++j) {\n                Integer k = tm.ceilingKey(nums[j]);\n                if (k != null && k - nums[j] > 1) {\n                    ++cnt;\n                }\n                Integer h = tm.floorKey(nums[j]);\n                if (h != null && nums[j] - h > 1) {\n                    ++cnt;\n                }\n                if (h != null && k != null && k - h > 1) {\n                    --cnt;\n                }\n                tm.merge(nums[j], 1, Integer::sum);\n                ans += cnt;\n            }\n        }\n        return ans;\n    }\n}\n```
['Java']