问题描述
给出一个整数数组A。有一个大小为B的滑动窗口,其中 从数组的最左边移到最右边。 您只能在窗口中看到w数字。每次滑动窗移动 向右移动一个位置。您必须找到每个窗口的最大值。 以下示例将使您更加清楚。
数组A为[1 3 -1 -3 5 3 6 7],而B为3。 示例:
Window position Max
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
Input 1:
A = [1,3,-1,-3,5,6,7]
B = 3
Output 1:
C = [3,7]
注意:您只需要实现给定的功能。不要读取输入,而应使用函数的参数。不打印输出,而是返回指定的值。
我的代码:
class Solution:
# @param A : tuple of integers
# @param B : integer
# @return a list of integers
def slidingMaximum(self,A,B):
stack = []
if B>=len(A):
return [max(A)]
else:
for i in range((len(A))+1-B):
stack.append(A[i:i+B])
stack[i]=max(stack[i])
return stack
显示我超出了时间限制。谁能告诉我为什么?
Time Limit Exceeded. Your submission didn't complete in the allocated time limit
解决方法
这是因为在分支function GlobalFilter({ globalFilter,setGlobalFilter }) {
const [value,setValue] = React.useState(globalFilter)
const onChange = React.useCallback(
value => {
const throttledSetGlobalFilter = throttle(
value => {
setGlobalFilter(value || undefined)
},2000
)
throttledSetGlobalFilter(value)
},[setGlobalFilter]
)
return (
<span className={styles.componentGlobalFilter}>
<input
className={styles.input}
value={value || ''}
onChange={(e) => {
setValue(e.target.value)
onChange(e.target.value)
}}
placeholder={`Zoek in deze tabel`}
/>
<Icon onClick={handleClick} layoutClassName={styles.icon} {...{ icon }} />
</span>
)
function handleClick() {
}
}
中您将返回单个值。应该有if B>=len(A):