📌  相关文章
📜  通过总和找到最长的子数组 - 无论代码示例

📅  最后修改于: 2022-03-11 14:57:58.591000             🧑  作者: Mango

代码示例1
def max_length(s, k):
    current = []
    max_len = -1 # returns -1 if there is no subsequence that adds up to k.
    for i in s:
        current.append(i)
        while sum(current) > k: # Shrink the array from the left, until the sum is <= k.
           current = current[1:]
        if sum(current) == k:
            max_len = max(max_len, len(current))

    return max_len