题解 | #牛的品种排序III#
牛的品种排序III
https://www.nowcoder.com/practice/f6ab3d7e20f54860886848f0a6374987
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param cows int整型vector * @param k int整型 * @return int整型vector */ vector<int> sortCowsIII(vector<int>& cows, int k) { std::map<int, int> auxMap; for (size_t i = 0; i < cows.size(); ++i) { auxMap[cows[i]]++; } int index = cows.size() - 1; while (k--) { for (int i = 0; i < auxMap[k]; ++i) { cows[index--] = k; } } return cows; } };