从大到小遍历一遍,每次取M个元素,然后求得最小的floor即可
int minimum(int M, vector <int> heights) { sort(heights.begin(),heights.end()); int minFloor = 10000; for(int i = heights.size()-1; i >=M-1; -- i){ int floor = 0; for(int j = 0; j < M; ++j) floor +=heights[i]-heights[i-j]; minFloor =min(minFloor,floor); } return minFloor; }