当前位置:网站首页>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
边栏推荐
- MATLAB绘制平面填充图入门详解
- LeetCode 2354. 优质数对的数目 二进制01表示和集合之间的转换
- What should I do if I install a solid-state drive in Win10 and still have obvious lags?
- Win7 encounters an error and cannot boot into the desktop normally, how to solve it?
- Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment
- cmake configure libtorch error Failed to compute shorthash for libnvrtc.so
- Masters and Masters
- Article pygame drag the implementation of the method
- Publish module to NPM should be how to operate?Solutions to problems and mistake
- The SSE instructions into ARM NEON
猜你喜欢
随机推荐
如何用硬币模拟1/3的概率,以及任意概率?
STM32LL library - USART interrupt to receive variable length information
How to solve Win11 without local users and groups
Introduction to in-order traversal (non-recursive, recursive) after binary tree traversal
Daily - Notes
Yolov5 official code reading - prior to transmission
What should I do if the Win10 system sets the application identity to automatically prompt for access denied?
Redis的线程模型
Letter combination of LeetCode2 phone number
奇技淫巧-位运算
4. Publish Posts, Comment on Posts
LeetCode 2354. 优质数对的数目 二进制01表示和集合之间的转换
动态规划理论篇
Use tencent cloud builds a personal blog
GMP scheduling model of golang
A clean start Windows 7?How to load only the basic service start Windows 7 system
MATLAB图形加标注的基本方法入门简介
pygame拖动条的实现方法
Configure clangd for vscode
Win10 cannot directly use photo viewer to open the picture