简体   繁体   English

滑动窗口技术:如何每次将窗口向前移动一个以上单位?

[英]Sliding-window technique: How to move a window forward by more than one unit each time?

Now I am wondering how to move a window forward by 4 uints or other number of units instead of only one unit each time.现在我想知道如何将窗口向前移动 4 个单位或其他数量的单位,而不是每次只移动一个单位。 Could you please give me clues about it?你能给我一些线索吗?

My code is like this:我的代码是这样的:

import sys
INT_MIN = -sys.maxsize - 1
def maxSum(arr,n,k):
max_sum = INT_MIN
for i in range(n-k+1):
    current_sum = 0
    for j in range(k):
        current_sum = current_sum + arr[i+j]
    print(current_sum)
    max_sum = max(current_sum, max_sum)
return max_sum

arr = [1,4,2,10,2,3,1,0,20,1,1,2,3,4,5,6,3,2,4,5,1,4,5]
k = 4
n = len(arr)

You mostly have a correct answer already.你大多已经有了正确的答案。 You wrote你写了

for i in range(n - k + 1):

All that is needed is to specify a step of k :所需要的只是指定一个step k

for i in range(0, n - k + 1, k):

nit: this code尼特:这段代码

    current_sum = 0
    for j in range(k):
        current_sum = current_sum + arr[i + j]

could be written with slice notation as current_sum = sum(arr[i:i + k]) .可以用切片表示法写成current_sum = sum(arr[i:i + k])


Pandas offers an implementation that might be of interest to you. Pandas 提供了您可能感兴趣的实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM