当前位置:网站首页>LeetCode 2344. 使数组可以被整除的最少删除次数 最大公约数
LeetCode 2344. 使数组可以被整除的最少删除次数 最大公约数
2022-08-02 14:10:00 【超级码力奥】
给你两个正整数数组 nums 和 numsDivide 。你可以从 nums 中删除任意数目的元素。
请你返回使 nums 中 最小 元素可以整除 numsDivide 中所有元素的 最少 删除次数。如果无法得到这样的元素,返回 -1 。
如果 y % x == 0 ,那么我们说整数 x 整除 y 。
示例 1:
输入:nums = [2,3,2,4,3], numsDivide = [9,6,9,3,15]
输出:2
解释:
[2,3,2,4,3] 中最小元素是 2 ,它无法整除 numsDivide 中所有元素。
我们从 nums 中删除 2 个大小为 2 的元素,得到 nums = [3,4,3] 。
[3,4,3] 中最小元素为 3 ,它可以整除 numsDivide 中所有元素。
可以证明 2 是最少删除次数。
示例 2:
输入:nums = [4,3,6], numsDivide = [8,2,6,10]
输出:-1
解释:
我们想 nums 中的最小元素可以整除 numsDivide 中的所有元素。
没有任何办法可以达到这一目的。
提示:
1 <= nums.length, numsDivide.length <= 105
1 <= nums[i], numsDivide[i] <= 109
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/minimum-deletions-to-make-array-divisible
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
看起来很唬人的一道题,其实先求第二个数组的最大公约数,然后枚举第一个数组中的元素,从小到大,当存在元素可以整除最大公约数,则就满足了。
class Solution {
public:
int gcd(int a, int b)
{
return a % b == 0 ? b : gcd(b, a % b);
}
int minOperations(vector<int>& nums, vector<int>& numsDivide) {
// 0可以被任何数整除
int d = 0;
for(int i = 0; i < numsDivide.size(); i ++)
d = gcd(d, numsDivide[i]);
int res = 0;
sort(nums.begin(), nums.end());
for(int i = 0; i < nums.size(); i ++)
{
// 如果存在元素可以整除d
if(d % nums[i] == 0) break;
res ++;
}
if(res == nums.size()) res = -1;
return res;
}
};
class Solution:
def minOperations(self, nums: List[int], numsDivide: List[int]) -> int:
def gcd(a, b):
if a % b == 0:
return b
return gcd(b, a % b)
d = 0
for i in numsDivide:
d = gcd(d, i)
res = 0
nums.sort()
for i in nums:
if d % i == 0:
break
res += 1
if res == len(nums): res = -1
return res
边栏推荐
- cmake配置libtorch报错Failed to compute shorthash for libnvrtc.so
- How to solve Win11 without local users and groups
- 二叉树的遍历:递归法/ 迭代法/ 统一迭代法(强QAQ)
- 软件测试基础知识(背)
- Detailed explanation of MATLAB drawing function plot
- 动态规划理论篇
- What should I do if the Win10 system sets the application identity to automatically prompt for access denied?
- 3.用户上传头像
- Doubled and sparse tables
- Detailed explanation of Golang garbage collection mechanism
猜你喜欢

【STM32学习1】基础知识与概念明晰

二叉树遍历之后序遍历(非递归、递归)入门详解

How to update Win11 sound card driver?Win11 sound card driver update method

7. Redis

What should I do if the Win10 system sets the application identity to automatically prompt for access denied?

Detailed explanation of MATLAB drawing function plot

1.开发社区首页,注册

How to reinstall Win7 system with U disk?How to reinstall win7 using u disk?

使用 腾讯云搭建一个个人博客

第三十章:普通树的存储和遍历
随机推荐
Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
编译error D8021 :无效的数值参数“/Wextra” cl command line error d8021 invalid numeric argument ‘/wextra‘
Based on the least squares linear regression equation coefficient estimation
第二十六章:二维数组
Win7遇到错误无法正常开机进桌面怎么解决?
4.发布帖子,评论帖子
Based on the matrix calculation in the linear regression equation of the coefficient estimates
win11一直弹出用户账户控制怎么解决
The SSE instructions into ARM NEON
MATLAB drawing command fimplicit detailed introduction to drawing implicit function graphics
Lightweight AlphaPose
SQL的通用语法和使用说明(图文)
Win11系统找不到dll文件怎么修复
Article pygame drag the implementation of the method
1.开发社区首页,注册
Spark及相关生态组件安装配置——快速回忆
质数相关问题-小记
二叉树创建之层次法入门详解
Detailed introduction to drawing complex surfaces using the plot_surface command
Win11 computer off for a period of time without operating network how to solve