從大到小遍歷一遍,每次取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; }