|
| 1 | +package problem2462 |
| 2 | + |
| 3 | +import ( |
| 4 | + "container/heap" |
| 5 | +) |
| 6 | + |
| 7 | +/* |
| 8 | +You are given a 0-indexed integer array costs where costs[i] is the cost of hiring the ith worker. |
| 9 | +You are also given two integers k and candidates. We want to hire exactly k workers according to the following rules: |
| 10 | + You will run k sessions and hire exactly one worker in each session. |
| 11 | + In each hiring session, choose the worker with the lowest cost from either the first candidates workers or the last candidates workers. |
| 12 | + Break the tie by the smallest index. |
| 13 | + For example, if costs = [3,2,7,7,1,2] and candidates = 2, then in the first hiring session, |
| 14 | + we will choose the 4th worker because they have the lowest cost [3,2,7,7,1,2]. |
| 15 | + In the second hiring session, we will choose 1st worker because they have the same lowest cost as |
| 16 | + 4th worker but they have the smallest index [3,2,7,7,2]. |
| 17 | + Please note that the indexing may be changed in the process. |
| 18 | + If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index. |
| 19 | + A worker can only be chosen once. |
| 20 | +Return the total cost to hire exactly k workers. |
| 21 | +*/ |
| 22 | + |
| 23 | +func totalCost(costs []int, k, candidates int) int64 { |
| 24 | + var res int64 |
| 25 | + |
| 26 | + if len(costs) == 1 { |
| 27 | + return int64(costs[0]) |
| 28 | + } |
| 29 | + |
| 30 | + // left and right represent the inner index of the left and right groups |
| 31 | + left, right := 0, len(costs)-1 |
| 32 | + |
| 33 | + // lPq and rPq are priority que |
| 34 | + lPq, rPq := MakeHeap(MinHeap), MakeHeap(MinHeap) |
| 35 | + for i := 0; i < candidates && right > left; i++ { |
| 36 | + heap.Push(lPq, costs[i]) |
| 37 | + heap.Push(rPq, costs[len(costs)-i-1]) |
| 38 | + left++ |
| 39 | + right-- |
| 40 | + } |
| 41 | + |
| 42 | + for ; k > 0; k-- { |
| 43 | + if lPq.Len() == 0 { |
| 44 | + // If left is empty, take from right |
| 45 | + res += int64(heap.Pop(rPq).(int)) |
| 46 | + continue |
| 47 | + } else if rPq.Len() == 0 { |
| 48 | + // If right is empty, take from left |
| 49 | + res += int64(heap.Pop(lPq).(int)) |
| 50 | + continue |
| 51 | + } |
| 52 | + |
| 53 | + lTemp, rTemp := lPq.Peek(), rPq.Peek() |
| 54 | + if rTemp < lTemp { |
| 55 | + // Take from right |
| 56 | + res += int64(heap.Pop(rPq).(int)) |
| 57 | + if right >= left { |
| 58 | + // Only push if the groups aren't colliding |
| 59 | + heap.Push(rPq, costs[right]) |
| 60 | + right-- |
| 61 | + } |
| 62 | + } else { |
| 63 | + // Take from left |
| 64 | + res += int64(heap.Pop(lPq).(int)) |
| 65 | + if right >= left { |
| 66 | + // Only push if the groups aren't colliding |
| 67 | + heap.Push(lPq, costs[left]) |
| 68 | + left++ |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return res |
| 74 | +} |
| 75 | + |
| 76 | +// Priority queue implementation |
| 77 | +type Heap struct { |
| 78 | + Values []int |
| 79 | + LessFunc func(int, int) bool |
| 80 | +} |
| 81 | + |
| 82 | +func (h *Heap) Less(i, j int) bool { return h.LessFunc(h.Values[i], h.Values[j]) } |
| 83 | +func (h *Heap) Swap(i, j int) { h.Values[i], h.Values[j] = h.Values[j], h.Values[i] } |
| 84 | +func (h *Heap) Len() int { return len(h.Values) } |
| 85 | +func (h *Heap) Peek() int { return h.Values[0] } |
| 86 | +func (h *Heap) Pop() (v interface{}) { |
| 87 | + h.Values, v = h.Values[:h.Len()-1], h.Values[h.Len()-1] |
| 88 | + return v |
| 89 | +} |
| 90 | +func (h *Heap) Push(v interface{}) { h.Values = append(h.Values, v.(int)) } |
| 91 | + |
| 92 | +func MakeHeap(less func(int, int) bool) *Heap { |
| 93 | + return &Heap{LessFunc: less} |
| 94 | +} |
| 95 | + |
| 96 | +func MaxHeap(i, j int) bool { return i > j } |
| 97 | +func MinHeap(i, j int) bool { return i < j } |
0 commit comments