There are some cars parked. Given an array of integers 'stops', represents where each car stops. Given an integer 'k', now you're going to build a moving shed. When the shed is required to move between these cars (the front end of the shed does not exceed the car in front, and the back end of the shed does not exceed the car in the back), it can successfully cover 'k' cars in any time. Ask for the minimum length of the shed that meets the requirements.
Sample 1:
Input: stops
=[7,3,6,1,8], k
=3
Output: 6
Explanation: these 5 cars are in positions 1,3,6,7,8 respectively.The shed needs to cover at least 3 cars, with a minimum length of 6, as it can cover 3 or more cars in [1,6], [2,7], [3,8]. If the length is 5, it only covers 1 car when built in [1,5] and [2,6] , which does not meet the conditions.
Sample 2:
Input: stops
=[7,3,6,1,8], k
=2
Output: 5
Explanation: these 5 cars are in positions 1,3,6,7,8 respectively.The shed needs to cover at least 2 cars with a minimum length of 5, as it can cover 2 or more cars in [1,5], [2,6], [3,7], [4,8]. If the length is 4, it only covers 1 car when built in [2,5], which does not meet the conditions.
The length of stops
: [2,1000].
The size of the element in 'stops' : [1,10000].
2 ≤ 'k' ≤ the length of stops
.
Ensure that the elements in 'stops' are not repeated.
Input test data (one parameter per line)How to understand a testcase?
解法1:这题我一开始把它想成滑动窗口,但那样复杂度太高。
后来参考网上发现用贪婪法就可以了。但要注意,不能简单的找k个相邻的车所跨域距离的最大值,因为那样的话,下面的输入
Input: stops
=[7,3,6,1,8], k
=2
Output: 5
会返回4,就错了。因为没有考虑中间的空位。
其实解法很简单,应该考虑k+1个相邻的车所跨越距离的最大值减1,这个也就是shed应该满足的最大值。
class Solution {
public:
/**
* @param stops: An array represents where each car stops.
* @param k: The number of cars should be covered.
* @return: return the minimum length of the shed that meets the requirements.
*/
int calculate(vector &stops, int k) {
int len = stops.size();
if (len < k) return -1;
int result = 0;
sort(stops.begin(), stops.end());
if (k == len) return stops[len - 1] - stops[0] + 1;
for (int i = k; i < len; ++i) {
result = max(result, stops[i] - stops[i - k]);
}
return result;
}
};