- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想在 Codility 上尝试一些挑战,并从头开始。所有作业都相对容易,直到名为 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 .
来自作业的描述。
它有两个 Action :
在以下条件下发生:
这两个条件都在上面的代码中说明。显然这是错误的,但我很困惑,我不知道我怎么能理解得不一样。
为什么这段代码是错误的,我在任务描述中遗漏了什么?
评分最高的答案之一如下所示:
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
代表什么?
也许我不确定我的英语完全误解了这个问题。我就是不能过去。
编辑:
根据我提供的代码,我是如何误解作业的?一般来说,我要求对问题进行解释。是说明需要做什么,还是把代码作为正确的结果,并提供和解释为什么这样做?
最佳答案
在我看来,您以某种方式混合了计数器的索引(A
中的值)和计数器的值(counter
中的值)。所以使用 A[i]-1
没有什么神奇之处——它是问题描述中的值 X
(调整为基于 0 的索引)。
我天真的方法是,我理解问题的方式(我希望它清楚,你的代码做错了什么):
public static int[] maxPart(int N, int[] A){
int[] counters = new int[N];
for(int i = 0; i < A.Length; i++){
int X=A[i];
if(X>=1 && X<=N){ // this encodes increment(X), with X=A[i]
counters [X-1] = counters [X-1] + 1; //-1, because our index is 0-based
}
if(X == N + 1 ){// this encodes setting all counters to the max value
int tmpMax = counters.Max ();
for(int h = 0; h < counters.Length; h++){
counters [h] = tmpMax;
}
}
}
}
return counters;
}
显然,这太慢了,因为复杂度为 O(n^2)
,操作次数为 n=10^5
(数组长度 A
),在如下操作顺序的情况下:
max counter, max counter, max counter, ....
评价最高的解决方案以惰性方式解决问题,不会在每次遇到 max counter
操作时显式更新所有值,而只是记住此操作后所有计数器必须具有的最小值在 resetLimit
中。因此,每次他必须递增一个计数器时,他会检查它的值是否必须由于以前的 max counter
操作而更新,并弥补他没有进行的所有 max counter
操作。 t 在这个计数器上执行
if(result[A[K] - 1] < resetLimit) {
result[A[K] - 1] = resetLimit + 1;
}
他的解决方案在 O(n)
中运行并且足够快。
关于c# - MaxCounters codility 理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38403344/
我想在 Codility 上尝试一些挑战,并从头开始。所有作业都相对容易,直到名为 MaxCounters 的作业为止。我不认为这个特别难,尽管它是第一个标记为非无痛的。 我已阅读task并开始使用
我遇到了一个问题,我需要限制对另一个网络服务器的调用次数。它会有所不同,因为服务器是共享的,并且可能具有更多或更少的容量。 我正在考虑使用 SemaphoreSlim 类,但没有公共(public)属
我有以下问题: 你有 N 个计数器,初始设置为 0,你可以对它们进行两种可能的操作: increase(X) − counter X is increased by 1, max co
你得到一个计数器数组N,从零开始。 您有一个要在 N 数组上执行的操作列表 A。 每个 Action 都是一个 int x 即 A = [1,5,3] 对于 A 中的每个 k 作为 x 操作 如果 x
如何使用 maxCount 重试网络请求(某种失败),例如。 5 次或直到服务器使用 RxSwift 的 retry 运算符返回我想要的内容。 我想做的是: 当成功变为“ok”时,停止重试并立即发出q
我并没有真正从 cv::calcOpticalFlowPyrLK 中的 criteria 得到什么是 epsilon 和 maxCount。有人可以给我更多相关信息吗? openCV 文档中的定义对我
这是我在 Codility 类(class)中使用的代码:MaxCounters def solution(N, A): counters = [0] * N max_c = 0
我是一名优秀的程序员,十分优秀!