Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

给定一个列表和滑动窗口的大小,找出所有滑动窗口里数值的最大值。 #137

Open
Sogrey opened this issue Feb 22, 2020 · 0 comments
Labels
Projects

Comments

@Sogrey
Copy link
Owner

Sogrey commented Feb 22, 2020

给定一个列表和滑动窗口的大小,找出所有滑动窗口里数值的最大值。

例如:如果输入列表[2,3,4,2,6,2,5,1]及滑动窗口的大小3,那么一共存在6个滑
动窗口,他们的最大值分别为[4,4,6,6,6,5]; 针对列表[2,3,4,2,6,2,5,1]的滑动
窗口有以下6个: [[2,3,4],2,6,2,5,1], [2,[3,4,2],6,2,5,1], [2,3,[4,2,6],2,5,1],
[2,3,4,[2,6,2],5,1], [2,3,4,2,[6,2,5],1], [2,3,4,2,6,[2,5,1]]。

def maxInWindows(num,size):
    if size <= 0 or len(num) < size:
        return []
    length = len(num)
    result = []
    for i in range(0,length - size + 1):
        result.append(max(num[i:i+size]))

    return result
print(maxInWindows([2,3,4,2,6,2,5,1],3))  ## [4, 4, 6, 6, 6, 5]

对滑窗内每个数字都进行扫描,直到找到和最大值相等数字,相等表明我的一个滑窗扫描结束,进入下一个滑窗进行扫描,并取最大值。

@Sogrey Sogrey added the 算法 label Feb 22, 2020
@Sogrey Sogrey added this to 算法 in Python QAs Feb 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

No branches or pull requests

1 participant