1 Star 0 Fork 0

唐梓迅/leetcode题解

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
.LeetCode 1775 822 Bytes
一键复制 编辑 原始数据 按行查看 历史
唐梓迅 提交于 2022-12-07 11:19 +08:00 . 通过最少操作次数使数组的和相等
class Solution {
public:
int minOperations(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size();
int m = nums2.size();
if(6*n<m || 6*m<n)
return -1;
int d = accumulate(nums2.begin(),nums2.end(),0) -
accumulate(nums1.begin(),nums1.end(),0);
if(d < 0)
{
d = -d;
swap(nums1,nums2);
}
vector<int> cnt(6,0);
for(auto& x : nums1)
++cnt[6-x];
for(auto& x : nums2)
++cnt[x-1];
int ans = 0;
for(int i = 5;;i--)
{
if(i * cnt[i] >= d)
{
return ans + (d + i - 1) / i;
}
ans += cnt[i];
d -= i * cnt[i];
}
return -1;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Tang-CMer/leetcode-problem-solving.git
git@gitee.com:Tang-CMer/leetcode-problem-solving.git
Tang-CMer
leetcode-problem-solving
leetcode题解
master

搜索帮助