[英]number of subsequences whose sum is divisible by k
I just did a coding challenge for a company and was unable to solve this problem.我刚刚为一家公司做了一个编码挑战,但无法解决这个问题。 Problem statement goes like:
问题陈述如下:
Given an array of integers, find the number of subsequences in the array whose sum is divisible by k, where k is some positive integer.给定一个整数数组,找出数组中总和可被 k 整除的子序列的数量,其中 k 是某个正整数。
For example, for [4, 1, 3, 2]
and k = 3
, the solution is 5. [[3], [1, 2], [4,3,2], [4,2], [1,3,2]]
are the subsequences whose sum is divisible by k
, ie current_sum + nums[i] % k == 0
, where nums[i]
is the current element in the array.例如,对于
[4, 1, 3, 2]
和k = 3
,解为 5。 [[3], [1, 2], [4,3,2], [4,2], [1,3,2]]
是其总和可被k
整除的子序列,即current_sum + nums[i] % k == 0
,其中nums[i]
是数组中的当前元素。
I tried to solve this recursively, however, I was unable to pass any test cases.我试图递归地解决这个问题,但是,我无法通过任何测试用例。 My recursive code followed something like this:
我的递归代码如下:
def kSum(nums, k):
def kSum(cur_sum, i):
if i == len(nums): return 0
sol = 1 if (cur_sum + nums[i]) % k == 0 else 0
return sol + kSum(cur_sum, i+1) + kSum(cur_sum + nums[i], i+1)
return kSum(0, 0)
What is wrong with this recursive approach, and how can I correct it?这种递归方法有什么问题,我该如何纠正? I'm not interested in an iterative solution, I just want to know why this recursive solution is wrong and how I can correct it.
我对迭代解决方案不感兴趣,我只想知道为什么这个递归解决方案是错误的以及我如何纠正它。
Are you sure that is not the case test?你确定这不是案例测试吗? For example:
例如:
[4, 1, 3, 2], k = 3
has已
4+2 = 6, 1+2=3, 3, 1+2+3=6, 4+2+3 = 9
So, your function is right (it gives me 5) and I don't see a major problem with your function.所以,你的函数是正确的(它给了我 5),我认为你的函数没有重大问题。
Here is a javascript reproduction of what you wrote with some console logs to help explain its behavior.这是您使用一些控制台日志编写的内容的 javascript 复制,以帮助解释其行为。
function kSum(nums, k) { let recursive_depth = 1; function _kSum(cur_sum, i) { recursive_depth++; if (i == nums.length) { recursive_depth--; return 0; } let sol = 0; if (((cur_sum + nums[i]) % k) === 0) { sol = 1; console.log(`Found valid sequence ending with ${nums[i]} with sum = ${cur_sum + nums[i]} with partial sum ${cur_sum} at depth ${recursive_depth}`); } const _kSum1 = _kSum(cur_sum, i+1); const _kSum2 = _kSum(cur_sum + nums[i], i+1); const res = sol + _kSum1 + _kSum2; recursive_depth--; return res; } return _kSum(0, 0); } let arr = [4, 1, 3, 2], k = 3; console.log(kSum(arr, k));
I think this code actually gets the right answer.我认为这段代码实际上得到了正确的答案。 I'm not fluent in Python, but I might have inadvertently fixed a bug in your code though by adding parenthesis around
(cur_sum + nums[i]) % k
我不精通 Python,但我可能无意中修复了代码中的错误,尽管在
(cur_sum + nums[i]) % k
周围添加了括号
It seems to me that your solution is correct.在我看来,您的解决方案是正确的。 It reaches the answer by trying all subsequences, which has
2^n
complexity.它通过尝试所有复杂度为
2^n
序列得出答案。 We could formulate it recursively in an O(n*k)
search space, although it could be more efficient to table.我们可以在
O(n*k)
搜索空间中递归地制定它,尽管它可能更有效。 Let f(A, k, i, r)
represent how many subsequences leave remainder r
when their sum is divided by k
, using elements up to A[i]
.让
f(A, k, i, r)
表示当它们的总和除以k
,有多少子序列离开余数r
,使用最多为A[i]
元素。 Then:然后:
function f(A, k, i=A.length-1, r=0){ // A[i] leaves remainder r // when divided by k const c = A[i] % k == r ? 1 : 0; if (i == 0) return c; return c + // All previous subsequences // who's sum leaves remainder r // when divided by k f(A, k, i - 1, r) + // All previous subsequences who's // sum when combined with A[i] // leaves remainder r when // divided by k f(A, k, i - 1, (k + r - A[i]%k) % k); } console.log(f([1,2,1], 3)); console.log(f([2,3,5,8], 5)); console.log(f([4,1,3,2], 3)); console.log(f([3,3,3], 3));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.