Golang Heap O(n log n) Beats 100.00%of users with Go
\n\n# Complexity\n- Time complexity: O(n log n)\n Add your time complexity here, e.g. O(n) \n\n# Code\n\ntype MaxHeap [][]int\n\nfunc (h MaxHeap) Len() int     
\n\n# Complexity\n- Time complexity: O(n log n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\ntype MaxHeap [][]int\n\nfunc (h MaxHeap) Len() int           { return len(h) }\nfunc (h MaxHeap) Less(i, j int) bool { return h[i][0] < h[j][0] }\nfunc (h MaxHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }\n\nfunc (h *MaxHeap) Push(x interface{}) {\n\t*h = append(*h, x.([]int))\n}\n\nfunc (h *MaxHeap) Pop() interface{} {\n\told := *h\n\tn := len(old)\n\tx := old[n-1]\n\t*h = old[0 : n-1]\n\treturn x\n}\n\nfunc minimumSum(nums []int) int {\n\tlm := nums[0]\n\tans := math.MaxInt\n\trHeap := &MaxHeap{}\n\theap.Init(rHeap)\n\n\tfor i := 2; i < len(nums); i++ {\n\t\theap.Push(rHeap, []int{nums[i], i})\n\t}\n\n\tfor i := 1; i < len(nums)-1; i++ {\n\t\tif rHeap.Len() == 0 {\n\t\t\treturn ans\n\t\t}\n\n\t\tif lm < nums[i] && (*rHeap)[0][0] < nums[i] {\n\t\t\tans = Min(ans, lm+nums[i]+(*rHeap)[0][0])\n\t\t}\n\n\t\tif nums[i] < lm {\n\t\t\tlm = nums[i]\n\t\t}\n\n\t\tif (*rHeap)[0][0] == nums[i+1] {\n\t\t\tfor rHeap.Len() > 0 && (*rHeap)[0][1] <= i+1 {\n\t\t\t\theap.Pop(rHeap)\n\t\t\t}\n\t\t}\n\t}\n\n\tif ans == math.MaxInt {\n\t\treturn -1\n\t}\n\n\treturn ans\n}\n\nfunc Min(a, b int) int {\n\tif a < b {\n\t\treturn a\n\t}\n\treturn b\n}\n```
['Heap (Priority Queue)', 'Go']