Golang 156 ms 10.9 MB
Complexity\n- Time complexity: O((n+m)*logn)\n- Space complexity: O(n+m)\n# Code\n\nfunc minOperations(nums []int, queries []int) []int64 {\n\tn := len(nums)\n\
# Complexity\n- Time complexity: $$O((n+m)*logn)$$\n- Space complexity: $$O(n+m)$$\n# Code\n```\nfunc minOperations(nums []int, queries []int) []int64 {\n\tn := len(nums)\n\tsort.Ints(nums)\n\tprefixSums := make([]int, n)\n\tprefixSums[0] = nums[0]\n\tfor i := 1; i < n; i++ {\n\t\tprefixSums[i] = prefixSums[i-1] + nums[i]\n\t}\n\tresult := make([]int64, len(queries))\n\tfor i, query := range queries {\n\t\tidx := sort.SearchInts(nums, query)\n\t\tif idx == 0 {\n\t\t\tresult[i] = int64(prefixSums[n-1] - query*n)\n\t\t} else {\n\t\t\tresult[i] = int64(prefixSums[n-1] - query*(n-idx) + query*idx - prefixSums[idx-1]<<1)\n\t\t}\n\t}\n\treturn result\n}\n```
['Go']