题解 | #未排序数组中累加和为给定值的最长子数组长度#

未排序数组中累加和为给定值的最长子数组长度

https://www.nowcoder.com/practice/36fb0fd3c656480c92b569258a1223d5

N,k = map(int,input().split())
arr = list(map(int,input().split()))

sum = 0 
length = 0
tmp_dict = {0:-1}
for i in range(len(arr)):
    sum += arr[i]
    if sum not in tmp_dict:
        tmp_dict[sum] = i
    if sum - k in tmp_dict:
        length = max(length,i-tmp_dict[sum - k])
print(length)

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
牛客网
牛客企业服务