冬季已经来临。 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖。
在加热器的加热半径范围内的每个房屋都可以获得供暖。
现在,给出位于一条水平线上的房屋 houses 和供暖器 heaters 的位置,请你找出并返回可以覆盖所有房屋的最小加热半径。
说明: 所有供暖器都遵循你的半径标准,加热的半径也一样。
Example 1输入: houses = [1,2,3], heaters = [2] 输出: 1 解释: 仅在位置2上有一个供暖器。如果我们将加热半径设为1,那么所有房屋就都能得到供暖。Example 2
输入: houses = [1,2,3,4], heaters = [1,4] 输出: 1 解释: 在位置1, 4上有两个供暖器。我们需要将加热半径设为1,这样所有房屋就都能得到供暖。代码 [双指针]
class Solution {
public:
int findRadius(vector &houses, vector &heaters) {
int n = houses.size(), m = heaters.size();
sort(houses.begin(), houses.end());
sort(heaters.begin(), heaters.end());
int r = (houses[0] < heaters[0] || houses.back() > heaters.back()) ? max(heaters[0] - houses[0], houses.back() - heaters.back()) : 0;
int h1 = 0, h2 = 1;
for (int i = 0, ri; i < n; i++) {
if (houses[i] < heaters[h1]) continue;
while (h2 < m && houses[i] > heaters[h2]) ++h1, ++h2;
if (h2 == m) break;
ri = min(abs(houses[i] - heaters[h1]), abs(houses[i] - heaters[h2]));
r = max(r, ri);
}
return r;
}
};
2. 最小差 [双指针or二分]
给定两个整数数组a和b,计算具有最小差绝对值的一对数值(每个数组中取一个值),并返回该对数值的差
Example 1输入:{1, 3, 15, 11, 2}, {23, 127, 235, 19, 8}
输出:3,即数值对(11, 8)
代码 [二分]
class Solution {
private:
using ll = long long;
public:
int smallestDifference(vector &a, vector &b) {
if (a.size() > b.size()) swap(a, b);
sort(a.begin(), a.end());
ll r = LONG_LONG_MAX, d1, d2;
for (ll bi:b) {
int idx = lower_bound(a.begin(), a.end(), bi) - a.begin();
d1 = idx < a.size() ? a[idx] - bi : LONG_LONG_MAX;
d2 = idx != 0 ? bi - a[idx - 1] : LONG_LONG_MAX;
r = min({r, d1, d2});
}
return r;
}
};
3. 两地调度 [贪心]
公司计划面试 2n 人。给你一个数组 costs ,其中 costs[i] = [aCosti, bCosti] 。第 i 人飞往 a 市的费用为 aCosti ,飞往 b 市的费用为 bCosti 。
返回将每个人都飞到 a 、b 中某座城市的最低费用,要求每个城市都有 n 人抵达**。**
Example 1输入:costs = [[10,20],[30,200],[400,50],[30,20]] 输出:110 解释: 第一个人去 a 市,费用为 10。 第二个人去 a 市,费用为 30。 第三个人去 b 市,费用为 50。 第四个人去 b 市,费用为 20。 最低总费用为 10 + 30 + 50 + 20 = 110,每个城市都有一半的人在面试。Example 2
输入:costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]] 输出:1859代码
class Solution {
public:
int twoCitySchedCost(vector> &costs) {
sort(costs.begin(), costs.end(), [](const vector &lo, const vector &hi) {
return lo[0] - lo[1] < hi[0] - hi[1];
});
int total = 0, n = costs.size() >> 1;
for (int i = 0; i < n; ++i) total += costs[i][0] + costs[i + n][1];
return total;
}
};
4. 峰与谷 [贪心]
在一个整数数组中,“峰”是大于或等于相邻整数的元素,相应地,“谷”是小于或等于相邻整数的元素。例如,在数组{5, 8, 4, 2, 3, 4, 6}中,{8, 6}是峰, {5, 2}是谷。现在给定一个整数数组,将该数组按峰与谷的交替顺序排序。
Example 1输入: [5, 3, 1, 2, 3] 输出: [5, 1, 3, 2, 3]代码 1 O(nlogn)
class Solution {
public:
void wiggleSort(vector &nums) {
sort(nums.begin(), nums.end());
int lo = 0, hi = nums.size() - 1;
vector result;
result.reserve(nums.size());
while (lo < hi) {
result.push_back(nums[hi--]);
result.push_back(nums[lo++]);
}
if (lo == hi) result.push_back(nums[lo]);
nums = move(result);
}
};
代码 2 O(n)
class Solution {
public:
void wiggleSort(vector &nums) {
int i = 1;
while (i < nums.size()) {
if (nums[i] < nums[i - 1]) swap(nums[i], nums[i - 1]);
++i;
if (i < nums.size() && nums[i] > nums[i - 1]) swap(nums[i], nums[i - 1]);
++i;
}
}
};



