给你一个整数数组和一个整数K,请你判断数组是否可以划分为若干大小为k序列,并满足以下条件:
如何可以划分,返回True,否则返回False。
数组长度小于等于10^5。
示例
例1:
input: array=[1,2,3,4], k = 2
output:true
例2:
input: array=[1,2,2,3], k = 3
output:false
class Solution {
public:
/**
* @param arr: the input array
* @param k: the sequence length
* @return: if it is possible, return true, otherwise false
*/
bool partitionArray(vector<int> &arr, int k) {
// write your code here
int n = arr.size();
if(n%k != 0) return false; // 不能整除
int bucket = n/k; // 桶的个数
map<int,int> m;
for(auto a : arr)
{
if(++m[a] > bucket)//个数超过桶的个数,肯定不满足各个数不一样的条件
return false;
}
return true;
}
};
151ms C++
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有