当前位置:网站首页>leetcode-Array
leetcode-Array
2022-07-26 05:39:00 【Codestars codestars】
Link
Ideas :
- nums If the number in can be divisible numsDivide All the numbers in , that , The condition it satisfies is division nunsDivide The least common divisor of
- So we need to ask numsDivide The least common divisor of , You need to use rotation and multiplication
int gcb(int a,int b){
return a==0?b:gcb(b%a,a);
// for instance , You can simply launch
// such as ,4 and 18 The least common divisor of , take 18 except 4 The remainder of ( by 2), Then take the remainder 2 Keep following 4 Perform the same operation ,gcb(2,4), Then you will find this time 4%2 by 0, So go back to 2, I.e. return to b
}
3. The real thing about this question is ( After understanding the meaning of the topic, it turns into :
1. loop nums Each number in , Find the smallest number that can be divided by the smallest common divisor k
2. Then traverse the loop again , This time I found everything better than k Small number
class Solution {
public int minOperations(int[] nums, int[] numsDivide) {
// The important point of this problem is to find the minimum common divisor of the rolling division
//nums If the number in can be divisible numsDivide All the numbers in , that , The condition it satisfies is division nunsDivide The least common divisor of
// So first use the rolling division method to find numsDivide The least common divisor of
int p=0;
for(int numsdivide:numsDivide){
p=gcb(p,numsdivide);
}
int res=Integer.MAX_VALUE;
int sum=0;
// Find the smallest divisible p Number of numbers
for(int i:nums){
if(p%i==0){
res=Math.min(res,i);
}
}
if(res==Integer.MAX_VALUE)return -1;
for(int i:nums){
if(i<res)sum++;
}
return sum;
}
int gcb(int a,int b){
return a==0?b:gcb(b%a,a);
// for instance , You can simply launch
// such as ,4 and 18 The least common divisor of , take 18 except 4 The remainder of , Then take the remainder and continue to follow 4 Perform the same operation , Until the remainder is 0, That's the top a=0, Just go back to b such as 4%2 and 2 4%2 by 0 了 , Just go back to 2, I.e. return to b
}
}
边栏推荐
- Mba-day28 concept of number - exercise questions
- Another open source artifact, worth collecting and learning!
- Attack and defense world flatscience
- Hack The Box -SQL Injection Fundamentals Module详细讲解中文教程
- SSH Remote Management
- 中文文本纠错任务简介
- ES Cluster in Red status: what about write & delete operations?
- 虚拟偶像代言产品出问题谁负责? 且听律师分析
- Dynamic memory management and flexible array
- Getaverse, a distant bridge to Web3
猜你喜欢

SIP账号注册的SIP软电话的使用和常见问题

ES Cluster in Red status: what about write & delete operations?

Rocbossphp free open source light community system

520 for what? DIY is a high-value RGB clock that girls want to watch

How students apply for free idea

How to understand "array name is essentially an address" from the perspective of memory parsing?

这种项目,最好别接!

Redis persistence AOF

FPGA question brushing sequence detection

Thread三种实现方式 和 Handler的用法
随机推荐
I also found excellent software and hardware projects, all open source
Lamp architecture
[cloud native] record of feign custom configuration of microservices
SQL injection
FPGA question brushing sequence detection
ES Cluster in Red status: what about write & delete operations?
How to view the container name in pod
leetcode-aboutString
OD-Paper【1】:Rich feature hierarchies for accurate object detection and semantic segmentation
SSH远程管理
MongonDB API使用
DOM operation -- operation node
Use of feign (Part 2)
Hack The Box -SQL Injection Fundamentals Module详细讲解中文教程
循环结构 practice
Hack The Box - Web Requests Module详细讲解中文教程
一招教你轻松看懂波特图
Application of canoe XML in test modules
Autumn move - Preparation Plan
Mba-day28 concept of number - exercise questions