- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我在 Codility 类(class)中使用的代码:MaxCounters
def solution(N, A):
counters = [0] * N
max_c = 0
for el in A:
if el >= 1 and el <= N:
counters[el-1] += 1
max_c = max(counters[el-1], max_c)
elif el > N:
counters = [max_c] * N
return counters
每个测试都通过,但最后一个(“所有 max_counter 操作”)在 7 秒后超时,因此结果仅为 88%,时间复杂度为 O(N+M)。
是否可以使用Python提高算法的时间复杂度并获得100%的测试结果?
你有 N 个计数器,初始设置为 0,你可以对它们进行两种可能的操作:
给定一个非空数组 A,其中包含 M 个整数。这个数组代表连续的操作:
为以下假设编写一个有效的算法:
最佳答案
编辑:跟进这个答案的评论中的讨论,跟踪最后的操作以避免在连续的 max_counter 操作中不必要地重置数组是实现目标的关键。以下是不同的解决方案(一个跟踪最大值,第二个按需计算最大值)在实现该更改时的样子:
def solution(N, A):
counters = [0] * N
max_c = 0
last_was_max = False
for el in A:
if el <= N:
counters[el - 1] += 1
max_c = max(counters[el - 1], max_c)
last_was_max = False
elif not last_was_max:
counters = [max_c] * N
last_was_max = True
return counters
def solution2_1(N, A):
counters = [0] * N
last_was_max = False
for el in A:
if el <= N:
counters[el - 1] += 1
last_was_max = False
elif not last_was_max:
counters = [max(counters)] * N
last_was_max = True
return counters
我不知道提交中使用了哪个实现。
首先,您在 if 条件上浪费了一些时间:没有必要检查整数是否大于或等于 1,这是练习中给出的。然后,无需评估第二个 elif 语句,只需在那里寻找 else 即可。如果不满足第一个条件,则根据练习的定义满足第二个条件
其次,根据我的测试,仅在需要时计算最大值比在所有运行中跟踪它要快得多。这可能是因为最大操作很少发生,特别是对于大的 M 值,因此你浪费时间多次跟踪东西而不是在运行期间只计算最大值几次。
针对@Nearoo 的评论,重新分配数组似乎确实改变了物理地址,但根据我运行的一些测试,重新分配无论如何比 for 循环快得多。
def solution2(N, A):
counters = [0] * N
for el in A:
if el <= N:
counters[el - 1] += 1
else:
counters = [max(counters)] * N
return counters
在使用不同种子值的多次测试运行中,我提出的这个解决方案比您的解决方案高出 2-3 倍。这是要重现的代码:
import random
random.seed(101)
N = random.randint(1, 100000)
M = random.randint(1, 100000)
A = [random.randint(1, N + 1) for i in range(M)]
%timeit solution(N,A)
>>> 11.7 ms ± 805 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit solution2(N,A)
>>> 3.63 ms ± 169 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
关于python - 对于使用 python 的 Codility 类(class) MaxCounters,是否有可能比 O(N+M) 更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58854370/
试图了解 Codility NailingPlanks 的解决方案。 问题链接: https://app.codility.com/programmers/lessons/14-binary_sear
我遇到了这个 codility 测试,问题是发现函数中的错误并调整它以使其正常工作。 传递给函数的数组是{1,3,3},K =2。如果在数组中找不到 K,该函数应该返回 false,但它返回 true
我有一个 Codility 测试即将进行。我试图通过使用 LONG 而不是 INT 在代码中找到一个修改来避免 EXTREME LARGE NUMBERS ERROR...但这没有用。 有人试过使用
昨晚我在 Codility 上查看了 Equi 演示任务,并为以下功能获得了 12/100 分: function solution(A) { var n = A.length; va
这是我对计数半素数可修正性问题的解决方案,它适用于中小型输入,但会导致大型测试用例的段错误。 https://codility.com/demo/results/demo8JU794-FC7/ 这通常
问题: 给定一串数字,计算是任何回文的字谜的子词(一致的子序列)的数量。 例子: 对于输入字符串“02002”,结果应该是 11,即: “0”、“2”、“0”、“0”、“2”、“00”、“020”、“
我正在 codility.com 上执行排列任务。目标基本上是检查数组是否作为与排列大小完全匹配的一个元素传递。 IE。对于 array size N,它应该包含值 1,2,3...N,每个值恰好一次
我做了 codility 演示测试“NumberOfDiscIntersections”: https://codility.com/programmers/lessons/4 我有:性能 = 100
这个问题在这里已经有了答案: Finding minimal absolute sum of a subarray (11 个答案) 关闭 4 年前。 您好,我参加了两次 Codility 测试,得
我刚刚在 Codility,遇到了一个任务,我找不到目标 O(n) 效率的解决方案;我的解决方案运行时间为 O(n2)。如果有人能给我一些关于如何让它运行得更快的提示,我将非常高兴。这是任务。 给定一
我的解决方案在 Codility 上的正确率仅为 40%。 我做错了什么? Here是测试结果(https://codility.com/demo/results/trainingU7KSSG-YNX
Codality 有一种有趣的命名方式。例如:他们说“领导者”而不是多数元素。 他们描述了一种技术here称为 Caterpillar method.这项技术的真正技术名称是什么? (我猜是回溯,但我
这个问题在这里已经有了答案: Counting palindromic substrings in O(n) (3 个答案) 关闭 9 年前。 在这个问题中,我们只考虑由小写英文字母 (a−z) 组
所以我决定试试 Codility .第一个任务 - FrogJmp太简单了,但令我惊讶的是我得到了 44%。解决方案,即使是正确的,在性能方面显然也是 Not Acceptable 。 原始解决方案:
我一直在努力解决以下任务: 你有 N 个计数器,初始设置为 0,你可以对它们进行两种可能的操作: increase(X) − counter X is increased by 1,
我试过这个 Codility 测试:MinAbsSum。 https://codility.com/programmers/lessons/17-dynamic_programming/min_abs
我正在尝试找到 a codility question on minimum slice of a subarray 的解决方案,并且我使用 Kadane 算法的修改版本设计了一个解决方案。我目前得到
任务是: 给出了一个非空的零索引字符串 S。字符串 S 由大写英文字母 A、C、G、T 集合中的 N 个字符组成。 这个字符串实际上代表一个DNA序列,大写字母代表单个核苷酸。 你还得到了由 M 个整
我需要一些帮助来解决这个 codility 挑战的算法: 编写一个函数,给定三个整数 A、B 和 K,返回 [A..B] 范围内可被 K 整除的整数个数。例如,对于 A = 6,B = 11 和 K
我使用 Scala 编写了 Codility 上 TapeEquilibrium 问题的解决方案。我已经尝试了许多不同负载的测试输入,当我使用 Codility Develipment 环境和 ecl
我是一名优秀的程序员,十分优秀!