我一直在尝试解决以下任务:
您将获得 N 个计数器,最初设置为 0,并且对它们有两个可能的操作:
increase(X) − counter X is increased by 1,
max_counter − all counters are set to the maximum value of any counter.
给出了一个由M个整数组成的非空零索引数组A。该数组表示连续操作:
if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X),
if A[K] = N + 1 then operation K is max_counter.
例如,给定整数N=5和数组A,如下所示:
A[0] = 3
A[1] = 4
A[2] = 4
A[3] = 6
A[4] = 1
A[5] = 4
A[6] = 4
每个连续操作后计数器的值将为:
(0, 0, 1, 0, 0)
(0, 0, 1, 1, 0)
(0, 0, 1, 2, 0)
(2, 2, 2, 2, 2)
(3, 2, 2, 2, 2)
(3, 2, 2, 3, 2)
(3, 2, 2, 4, 2)
目标是在所有操作之后计算每个计数器的值。
struct Results {
int * C;
int L;
};
写一个函数:
struct Results solution(int N, int A[], int M);
给定一个整数N和一个由M个整数组成的非空零索引数组A,返回一个表示计数器值的整数序列。
序列应返回为:
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).
例如,给定:
A[0] = 3
A[1] = 4
A[2] = 4
A[3] = 6
A[4] = 1
A[5] = 4
A[6] = 4
如上所述,该函数应返回 [3, 2, 2, 4, 2]。
假设:
N and M are integers within the range [1..100,000];
each element of array A is an integer within the range [1..N + 1].
复杂性:
expected worst-case time complexity is O(N+M);
expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
输入数组的元素可以修改。
以下是我的解决方案:
import java.util.Arrays;
class Solution {
public int[] solution(int N, int[] A) {
final int condition = N + 1;
int currentMax = 0;
int countersArray[] = new int[N];
for (int iii = 0; iii < A.length; iii++) {
int currentValue = A[iii];
if (currentValue == condition) {
Arrays.fill(countersArray, currentMax);
} else {
int position = currentValue - 1;
int localValue = countersArray[position] + 1;
countersArray[position] = localValue;
if (localValue > currentMax) {
currentMax = localValue;
}
}
}
return countersArray;
}
}
这里是代码估值:https://codility.com/demo/results/demo6AKE5C-EJQ/
你能给我提示一下这个解决方案有什么问题吗?
我开发的另一个值得考虑的解决方案是:http://codility.com/demo/results/demoM658NU-DYR/
问题是,当你得到很多max_counter
操作时,你会得到很多对Arrays.fill
的调用,这使得你的解决方案很慢。
您应该保留一个< code>currentMax和一个< code>currentMin:
currentMin=currentMax
如果您得到另一个值,我们将其称为i:
- 如果位置
i-1处的值小于或等于currentMin
,则将其设置为current Min 1
否则将增加该值
最后,只需再次检查计数器数组,并将小于currentMin
的所有值设置为currentMin
。
这段代码带来了问题:
for (int iii = 0; iii < A.length; iii++) {
...
if (currentValue == condition) {
Arrays.fill(countersArray, currentMax);
}
...
}
假设数组A
的每个元素都是用值N 1
初始化的。由于函数调用Arrays。fill(counterArray,currentMax)
的时间复杂度为O。我认为,解决这个问题的一种方法是,在调用max_counter
操作时,您可以将上次更新的值作为变量,而不是显式更新整个数组。当调用第一个操作(递增)时,您只需查看尝试递增的值是否大于last_update
。如果是,只需使用1更新值,否则将其初始化为last_update 1
。调用第二个操作时,只需将last_update
更新为current_max
。最后,当您完成并尝试返回最终值时,再次将每个值与last_update
进行比较。如果大于此值,则只保留该值,否则返回last_update
class Solution {
public int[] solution(int N, int[] A) {
final int condition = N + 1;
int currentMax = 0;
int lastUpdate = 0;
int countersArray[] = new int[N];
for (int iii = 0; iii < A.length; iii++) {
int currentValue = A[iii];
if (currentValue == condition) {
lastUpdate = currentMax
} else {
int position = currentValue - 1;
if (countersArray[position] < lastUpdate)
countersArray[position] = lastUpdate + 1;
else
countersArray[position]++;
if (countersArray[position] > currentMax) {
currentMax = countersArray[position];
}
}
}
for (int iii = 0; iii < N; iii++) {
if (countersArray[iii] < lastUpdate)
countersArray[iii] = lastUpdate;
}
return countersArray;
}
}