-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heaters
146 lines (134 loc) · 4.98 KB
/
Heaters
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
/*
LeetCode 475:
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses.
Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters.
So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters.
Note:
Numbers of houses and heaters you are given are non-negative and will not exceed 25000.
Positions of houses and heaters you are given are non-negative and will not exceed 10^9.
As long as a house is in the heaters' warm radius range, it can be warmed.
All the heaters follow your radius standard and the warm radius will the same.
Example 1:
Input: [1,2,3],[2]
Output: 1
Explanation: The only heater was placed in the position 2, and if we use the radius 1 standard, then all the houses can be warmed.
Example 2:
Input: [1,2,3,4],[1,4]
Output: 1
Explanation: The two heater was placed in the position 1 and 4. We need to use radius 1 standard, then all the houses can be warmed.
Solution:
Language: C++
Tag: Binary Search
*/
class Solution {
public:
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
int findRadius(vector<int>& houses, vector<int>& heaters) {
// 1. binary search ?? -- O((m + n)logn) time + O(1) space: n is size of heaters, m is size of houses.
sort(heaters.begin(), heaters.end(), less<int>());
//quicksort(heaters, 0, heaters.size() - 1);
//heapsort(heaters);
//int *temp = new int[heaters.size()];
//mergesort(heaters, 0, heaters.size() - 1, temp);
int size = heaters.size(), res = 0;
for (int i = 0; i < houses.size(); ++i) {
int left = 0, right = size - 1;
while (left + 1 < right) {
int mid = left + (right - left) / 2;
if (heaters[mid] <= houses[i]) {
left = mid;
} else {
right = mid;
}
}
if (heaters[left] > houses[i]) {
res = max(res, heaters[left] - houses[i]);
} else if (heaters[right] < houses[i]) {
res = max(res, houses[i] - heaters[right]);
} else {
res = max(res, min(houses[i] - heaters[left], heaters[right] - houses[i]));
}
}
return res;
}
void quicksort(vector<int> &nums, int start, int end) {
if (start >= end) {
return;
}
int left = start, right = end, pivot = nums[(start + end) / 2];
while (left <= right) {
while (left <= right && nums[left] < pivot) {
++left;
}
while (left <= right && nums[right] > pivot) {
--right;
}
if (left <= right) {
swap(nums[left++], nums[right--]);
}
}
quicksort(nums, start, right);
quicksort(nums, left, end);
}
void mergesort(vector<int> &nums, int start, int end, int *temp) {
if (start >= end) {
return;
}
int mid = start + (end - start) / 2;
mergesort(nums, start, mid, temp);
mergesort(nums, mid + 1, end, temp);
merge(nums, start, end, temp);
}
void merge(vector<int> &nums, int start, int end, int *temp) {
int mid = start + (end - start) / 2;
int left = start, right = mid + 1, idx = start;
while (left <= mid && right <= end) {
if (nums[left] <= nums[right]) {
temp[idx++] = nums[left++];
} else {
temp[idx++] = nums[right++];
}
}
while (left <= mid) {
temp[idx++] = nums[left++];
}
while (right <= end) {
temp[idx++] = nums[right++];
}
for (int i = start; i <= end; ++i) {
nums[i] = temp[i];
}
}
void heapsort(vector<int> &nums) {
int size = nums.size();
for (int i = (size - 1) / 2; i >= 0; --i) {
siftdown(nums, i, size);
}
while (size > 0) {
swap(nums[--size], nums[0]);
siftdown(nums, 0, size);
}
int left = 0, right = nums.size() - 1;
while (left < right) {
swap(nums[left++], nums[right--]);
}
}
void siftdown(vector<int> &nums, int father, int size) {
while (father < size) {
int min_idx = father;
int left = father * 2 + 1, right = left + 1;
if (left < size && nums[left] < nums[min_idx]) {
min_idx = left;
}
if (right < size && nums[right] < nums[min_idx]) {
min_idx = right;
}
if (father == min_idx) {
break;
}
swap(nums[father], nums[min_idx]);
father = min_idx;
}
}
};