[JavaScript] Bucket sort
Intuition\n Describe your first thoughts on how to solve this problem. \nUse bucket sort to keep track of the sorted items (and if there are any gaps) as we add
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse bucket sort to keep track of the sorted items (and if there are any gaps) as we add items to each subarray.\n\n# Complexity\n- Time complexity: O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nconst sumImbalanceNumbers = function(nums) {\n    const F = Array(nums.length + 1); // use F to bucket sort subarray items\n    let result = 0;\n    for (let i = 0; i < nums.length - 1; ++i) {\n        F.fill(0);\n        let cur = 0;\n        let min = Infinity;\n        let max = -Infinity;\n        for (let j = i; j < nums.length; ++j) {\n            ++F[nums[j]];\n            min = Math.min(min, nums[j]);\n            max = Math.max(max, nums[j]);\n            if (F[nums[j]] === 1) { // first of this value in the subarray\n                if (max > nums[j] && min < nums[j] && F[nums[j]+1] === 0 && F[nums[j]-1] === 0) ++cur;\n                else if (nums[j] === min && max > nums[j] && F[nums[j]+1] === 0) ++cur;\n                else if (nums[j] === max && min < nums[j] && F[nums[j]-1] === 0) ++cur;\n                \n            }\n            if (F[nums[j]] === 1 && min < nums[j] && max > nums[j] && F[nums[j]+1] > 0 && F[nums[j]-1] > 0) --cur;\n            result += cur;\n        }\n    }\n    return result;\n};\n```
['JavaScript']