- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在学习 Codility 计数课 ( https://codility.com/media/train/2-CountingElements.pdf ),我需要帮助来了解最快的解决方案。
我想知道为什么第 8 行中差值除以 2 d //= 2
?差异是否足以找到我们可以在数组之间交换的元素?
问题:
You are given an integer
m
(1 < m < 1000000
) and two non-empty, zero-indexed arraysA
andB
ofn
integers,a0
,a1
, ... ,an−1
andb0
,b1
, ... ,bn−1
respectively (0 < ai
,bi < m
). The goal is to check whether there is a swap operation which can be performed on these arrays in such a way that the sum of elements in arrayA
equals the sum of elements in arrayB
after the swap. By swap operation we mean picking one element from arrayA
and one element from arrayB
and exchanging them.
解决方法:
def fast_solution(A, B, m):
n = len(A)
sum_a = sum(A)
sum_b = sum(B)
d = sum_b - sum_a
if d % 2 == 1:
return False
d //= 2
count = counting(A, m)
for i in xrange(n):
if 0 <= B[i] - d and B[i] - d <= m and count[B[i] - d] > 0:
return True
return False
最佳答案
A[i
] 和 B[j]
之间的交换将 B[j]-A[i]
添加到 sum(A)
和从sum(B)
中减去相同的值;因此它影响总和的差 两倍 B[j]-A[i]
。
因此,将原始差异减半(在检查它是偶数之后——如果它是奇数,则没有交换将起作用!-)以形成可能交换的目标是正确的。
另请注意,它们提供的 counting
函数并不是现代 Python 的最佳选择——为避免重新发明“计数项目”的特定轮子,请参阅 https://docs.python.org/2/library/collections.html#collections.Counter对于 Python 2,或 https://docs.python.org/3/library/collections.html#collections.Counter适用于 Python 3。
关于python - Codility计数课,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27581933/
试图了解 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
我是一名优秀的程序员,十分优秀!