-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0857-MinimumCostToHireKWorkers.cs
179 lines (150 loc) · 4.74 KB
/
0857-MinimumCostToHireKWorkers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//-----------------------------------------------------------------------------
// Runtime: 156ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace LeetCode
{
public class _0857_MinimumCostToHireKWorkers
{
public double MincostToHireWorkers(int[] quality, int[] wage, int K)
{
var workers = new List<Worker>();
for (int i = 0; i < quality.Length; i++)
workers.Add(new Worker(quality[i], wage[i]));
workers.Sort();
var pool = new Heap(K);
var qualityTotal = 0;
var result = double.MaxValue;
foreach (var worker in workers)
{
pool.Offer(-worker.Quality);
qualityTotal += worker.Quality;
if (pool.Size() > K)
{
var maxQualityWorker = -1 * pool.Poll();
qualityTotal -= (int)maxQualityWorker;
}
if (pool.Size() == K)
{
result = Math.Min(result, qualityTotal * worker.Ratio);
}
}
return result;
}
public class Worker : IComparable<Worker>
{
public Worker(int quality, int wage)
{
Quality = quality;
Wage = wage;
}
public int Quality { get; set; }
public int Wage { get; set; }
public double Ratio { get { return Wage * 1.0 / Quality; } }
public int CompareTo(Worker other)
{
return Ratio.CompareTo(other.Ratio);
}
}
public class Heap
{
double[] heap = null;
int size = 0;
public Heap(int k)
{
heap = new double[k + 1];
}
public int Size()
{
return size;
}
public void Offer(double val)
{
heap[size++] = val;
HeapifyUp();
}
public double Peek()
{
if (size == 0) throw new InvalidOperationException();
return heap[0];
}
public double Poll()
{
if (size == 0) throw new InvalidOperationException();
double resp = heap[0];
if (size > 1)
{
heap[0] = heap[size - 1];
HeapifyDown();
}
size--;
return resp;
}
private void HeapifyUp()
{
int index = size - 1;
while (HasParent(index))
{
int parentIndex = GetParentIndex(index);
if (heap[parentIndex] > heap[index])
Swap(parentIndex, index);
else
break;
index = parentIndex;
}
}
private void HeapifyDown()
{
int index = 0;
while (HasLeftChild(index))
{
int left = GetLeftChildIndex(index), right = GetRightChildIndex(index), minIndex = left;
double min = heap[left];
if (HasRightChild(index) && heap[right] < min)
{
min = heap[right];
minIndex = right;
}
if (heap[index] > min)
Swap(index, minIndex);
else
break;
index = minIndex;
}
}
private void Swap(int index1, int index2)
{
double temp = heap[index1];
heap[index1] = heap[index2];
heap[index2] = temp;
}
private bool HasParent(int index)
{
return (index + 1) / 2 - 1 >= 0;
}
private bool HasLeftChild(int index)
{
return index * 2 + 1 < size;
}
private bool HasRightChild(int index)
{
return index * 2 + 2 < size;
}
private int GetParentIndex(int index)
{
return (index + 1) / 2 - 1;
}
private int GetLeftChildIndex(int index)
{
return index * 2 + 1;
}
private int GetRightChildIndex(int index)
{
return index * 2 + 2;
}
}
}
}