Java 计算数组中不是每个元素的除数的元素数
我试图从可理解性的角度来理解问题的解决方案。这个问题要求计算数组中不是每个元素的除数的元素数。下文提供了完整的说明Java 计算数组中不是每个元素的除数的元素数,java,algorithm,performance,time-complexity,space-complexity,Java,Algorithm,Performance,Time Complexity,Space Complexity,我试图从可理解性的角度来理解问题的解决方案。这个问题要求计算数组中不是每个元素的除数的元素数。下文提供了完整的说明 You are given a non-empty zero-indexed array A consisting of N integers. For each number A[i] such that 0 ≤ i < N, we want to count the number of elements of the array that are not the
You are given a non-empty zero-indexed array A consisting of N integers.
For each number A[i] such that 0 ≤ i < N, we want to count the number of elements of the array that are not the divisors of A[i]. We say that these elements are non-divisors.
For example, consider integer N = 5 and array A such that:
A[0] = 3
A[1] = 1
A[2] = 2
A[3] = 3
A[4] = 6
For the following elements:
A[0] = 3, the non-divisors are: 2, 6,
A[1] = 1, the non-divisors are: 3, 2, 3, 6,
A[2] = 2, the non-divisors are: 3, 3, 6,
A[3] = 3, the non-divisors are: 2, 6,
A[6] = 6, there aren't any non-divisors.
Write a function:
class Solution { public int[] solution(int[] A); }
that, given a non-empty zero-indexed array A consisting of N integers, returns a sequence of integers representing the amount of non-divisors.
The sequence should be returned as:
a structure Results (in C), or
a vector of integers (in C++), or
a record Results (in Pascal), or
an array of integers (in any other programming language).
For example, given:
A[0] = 3
A[1] = 1
A[2] = 2
A[3] = 3
A[4] = 6
the function should return [2, 4, 3, 2, 0], as explained above.
Assume that:
N is an integer within the range [1..50,000];
each element of array A is an integer within the range [1..2 * N].
Complexity:
expected worst-case time complexity is O(N*log(N));
expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.
将为您提供一个由N个整数组成的非空零索引数组。
对于每个数字A[i],0≤ i
我也有一个解决办法
// int[] A = {3, 1, 2, 3, 6};
public static int[] solution(int[] A) {
int[][] D = new int[2 * A.length + 1][2];
int[] res = new int[A.length];
//-----
// 0 1
// 0 0
// 1 -1
// 1 -1
// 2 -1
// 0 0
// 0 0
// 1 -1
// 0 0
// 0 0
// 0 0
// 0 0
//-----
for (int i = 0; i < A.length; i++) {
// D[A[i]][0]++;
D[A[i]][0] = D[A[i]][0] + 1;
D[A[i]][1] = -1;
}
for (int i = 0; i < A.length; i++){
if(D[A[i]][1]==-1){
D[A[i]][1]=0;
for (int j = 1; j*j <= A[i]; j++) {
if(A[i] % j == 0) {
// D[A[i]][1] = D[A[i]][1] + D[j][0];
D[A[i]][1] += D[j][0];
if (A[i]/j != j){
D[A[i]][1]+= D[A[i]/j][0];
}
}
}
}
}
for (int i = 0; i < A.length; i++) {
res[i] = A.length - D[A[i]][1];
}
return res;
}
//int[]A={3,1,2,3,6};
公共静态int[]解决方案(int[]A){
int[][]D=新int[2*A.length+1][2];
int[]res=新的int[A.长度];
//-----
// 0 1
// 0 0
// 1 -1
// 1 -1
// 2 -1
// 0 0
// 0 0
// 1 -1
// 0 0
// 0 0
// 0 0
// 0 0
//-----
for(int i=0;i 对于(int j=1;j*j,D
数据结构/矩阵是这样的:0th
列和jth
行计算j
在数组A
中出现的次数。换句话说,D[A[j][0]
是A[j]
值在数组中的次数
循环之后,1st
列和kth
行计算数组中划分A[k]
的元素数。换句话说,D[A[k][1]
是数组中A[k]
的除数
最后的结果是,r[j]
只是r[j]=(A.length)-D[A[j][1]
。因为我们想要的是不是除数的元素数
为什么循环工作
如果A[i]%j==0
,那么我们要做的是计算j
出现在A
中的次数,然后将其添加到D[A[i][1]
。这就是为什么你有行D[A[i][1]+=D[j][0];
。此外A[i]/j
也将是一个不同的因素(除非A[i]=j
)
数学部分来证明集合{A,B|A*B=N&AThej*j这非常有用。非常感谢你。
for (int j = 1; j*j <= A[i]; j++) {
if(A[i] % j == 0) {
// D[A[i]][1] = D[A[i]][1] + D[j][0];
D[A[i]][1] += D[j][0];
if (A[i]/j != j){
D[A[i]][1]+= D[A[i]/j][0];
}
}
}