O(N) solution using max/min heap
Intuition\n\nInstead of sorting whole array, use max/min heap to maintain top 3 largest element and top 3 smallest element.\n\n# Approach\n\n# Complexity\n- Tim
# Intuition\n\nInstead of sorting whole array, use max/min heap to maintain top 3 largest element and top 3 smallest element.\n\n# Approach\n\n# Complexity\n- Time complexity:\nO(N)\nBut I don\'t know why this solution takes longer that typical sorting solutions.\n\n- Space complexity:\nO(N)\n\n# Code\n```\nclass Solution {\n    public int minimizeSum(int[] nums) {\n        PriorityQueue<Integer> minHeap = new PriorityQueue<>();\n        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());\n        int n = nums.length;\n        \n        for(int i=0; i<n; i++) {\n            minHeap.add(nums[i]);\n            maxHeap.add(nums[i]);\n\n            if(minHeap.size() > 3) {\n                minHeap.poll();\n                maxHeap.poll();\n            }\n        }\n\n        int[] min = new int[3];\n        int[] max = new int[3];\n\n        for(int i=0; i<3; i++) {\n            min[i] = maxHeap.poll();\n            max[i] = minHeap.poll();\n        }\n\n        return Math.min(max[2]-min[0], Math.min(max[1]-min[1], max[0]-min[2]));\n    }\n}\n```
['Java']