题解 | #奶牛排队问题#
奶牛排队问题
https://www.nowcoder.com/practice/e974a33a14a344278c3a359a1ebf833e
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param weights int整型vector * @param n int整型 * @param k int整型 * @return bool布尔型 */ bool checkDuplicate(vector<int>& weights, int n, int k) { // write code here unordered_map<int,vector<int>> temp; for (int i = 0;i < weights.size();++i) { temp[weights[i]].emplace_back(i); } for (auto it : temp) { int maxLen = 0; if (it.second.size() == 1) { continue; } else { for (int i = 1;i < it.second.size();++i) { if (it.second[i] - it.second[i - 1] <= k) { return true; } } } } return false; } };