嘿,伙计们,我会解释下面的问题。
键=整数列表我必须找到键中元素的最大可分性程度。但是当我计算可分性的程度时,我应该只考虑键元素。例如:
keys = [2,4,8,2]
2 = [2,2] degree of divisibility is 2
4 = [2,4,2] degree of divisibility is 3
8 = [2,4,8,2] degree of divisibility is 4 so we choose 8 with 4 degrees of divisibility.在那之后,我们必须计算
if maxDegreeOfDivisibility(4 in our case) * 10^5 < validityPeriod* instructionCount then its
true. 我们返回1和4*10^5,我希望我能解释这个问题,如果你们对这个问题有任何疑问的话:D我可以回答。
public static List<Integer> encryptionValidity(int instructionCount, int validityPeriod,
List<Integer> keys) {
List<Integer> result = Arrays.asList(0, 0);
Map<Integer, Integer> degreeOfDivisibilityCache = new HashMap<>();
for (int i: keys) {
Integer degreeOfDivisibility = degreeOfDivisibilityCache.getOrDefault(i, -1);
if (degreeOfDivisibility == -1) {
int count = 0;
for (int j : keys) {
if (j > 0 && i % j == 0) {
count++;
}
}
degreeOfDivisibilityCache.put(i, count);
}
}
int maxDegreeOfDivisibility = degreeOfDivisibilityCache.values().stream()
.max(Comparator.comparingInt(Integer::intValue)).orElse(0);
int s = maxDegreeOfDivisibility * 10000;
result.set(1, s);
BigInteger ic = BigInteger.valueOf(instructionCount);
BigInteger a = ic.multiply(BigInteger.valueOf(validityPeriod));
if (a.compareTo(BigInteger.valueOf(s)) > 0) {
result.set(0, 1);
}
return result;
}发布于 2021-10-27 09:30:53
例子: 19,4,12,6,4,3,8
时间复杂度:如果n是整数总数,m是唯一整数数,则在BigO表示法中O(n) + O(m^2),但实际上O(n) + C*(m^2)/3 (对于某些常数C)
分析:
步骤1中的第一次扫描: O(n)
桶扫描:最坏的情况(满桶)是倒二叉树。这是一个无限系列,总计为1/3 (参见维基百科相关文章:这里和这里)
感谢里克·詹姆斯的候选人旗主意。
发布于 2021-10-18 05:16:59
示例:
https://stackoverflow.com/questions/69609022
复制相似问题