我想尝试一些关于Codility的挑战,并从头开始。所有的任务都比较容易,直到MaxCounters。我不认为这个特别难,虽然它是第一个标记为不痛的。MaxCounters编码理解
我已阅读task并开始在C#语言编码:
public static int[] maxPart(int N, int[] A){
int[] counters = new int[N];
for(int i = 0; i < A.Length; i++){
for(int j = 0; j < counters.Length; j++){
if(A[i] == counters[j] && (counters[j] >= 1 && counters[j] <= N)){
counters [j] = counters [j] + 1;
}
if(A[i] == N + 1){
int tmpMax = counters.Max();
for(int h = 0; h < counters.Length; h++){
counters [h] = tmpMax;
}
}
}
}
return counters;
}
有当然的3个回路使得它很慢,但让我们把它供以后使用。我的担心是我如何理解这一点,所有其他人在这个问题上看到它就像here。
来自作业的说明。
它有2个操作:
- 增加(X) - 计数器X增加1,
- 最大值计数器 - 所有计数器被设置为任何 计数器的最大值。
条件下发生:
- 如果A [K] = X,使得1≤X≤N,则操作K是增加(X),
- 如果A [K] = N + 1,则操作K是最大计数器。
这两个条件都在上面的代码中说明。显然这是错误的,但我很困惑,我不知道我怎么能理解它的不同。
为什么这段代码错了,我从任务描述中遗漏了什么?
一个最精彩的答案是这样的:
public int[] solution(int N, int[] A) {
int[] result = new int[N];
int maximum = 0;
int resetLimit = 0;
for (int K = 0; K < A.Length; K++)
{
if (A[K] < 1 || A[K] > N + 1)
throw new InvalidOperationException();
if (A[K] >= 1 && A[K] <= N)
{
if (result[A[K] - 1] < resetLimit) {
result[A[K] - 1] = resetLimit + 1;
} else {
result[A[K] - 1]++;
}
if (result[A[K] - 1] > maximum)
{
maximum = result[A[K] - 1];
}
}
else
{
// inefficiency here
//for (int i = 0; i < result.Length; i++)
// result[i] = maximum;
resetLimit = maximum;
}
}
for (int i = 0; i < result.Length; i++)
result[i] = Math.max(resetLimit, result[i]);
return result;
}
此代码的结果与上Codility 100%。
问:
我想知道笔者从任务怎么知道使用result[A[K] - 1]
? resetLimit
代表什么?
也许我完全误解了由于我的英语问题,我不确定。我只是不能过去。
编辑:
根据我提供的代码,我怎么会误解分配?一般来说,我要求解释这个问题。是否解释需要做什么,或者将代码作为正确的结果提供和解释为什么这样做?
你能更具体吗?你的问题到底是什么? – Amit
@Amit请检查我的编辑 – eomeroff