调试中...
调试中...
题目描述
题目描述
题解
题解
提交记录
提交记录
代码
代码
测试用例
测试用例
测试结果
测试结果
中等
相关标签
相关企业
提示

你和朋友们准备去野炊。给你一个下标从 0 开始的整数数组 security ,其中 security[i] 是第 i 天的建议出行指数。日子从 0 开始编号。同时给你一个整数 time 。

如果第 i 天满足以下所有条件,我们称它为一个适合野炊的日子:

  • i 天前和后都分别至少有 time 天。
  • i 天前连续 time 天建议出行指数都是非递增的。
  • i 天后连续 time 天建议出行指数都是非递减的。

更正式的,第 i 天是一个适合野炊的日子当且仅当:security[i - time] >= security[i - time + 1] >= ... >= security[i] <= ... <= security[i + time - 1] <= security[i + time].

请你返回一个数组,包含 所有 适合野炊的日子(下标从 0 开始)。返回的日子可以 任意 顺序排列。

 

示例 1:

输入:security = [5,3,3,3,5,6,2], time = 2
输出:[2,3]
解释:
第 2 天,我们有 security[0] >= security[1] >= security[2] <= security[3] <= security[4] 。
第 3 天,我们有 security[1] >= security[2] >= security[3] <= security[4] <= security[5] 。
没有其他日子符合这个条件,所以日子 2 和 3 是适合野炊的日子。

示例 2:

输入:security = [1,1,1,1,1], time = 0
输出:[0,1,2,3,4]
解释:
因为 time 等于 0 ,所以每一天都是适合野炊的日子,所以返回每一天。

示例 3:

输入:security = [1,2,3,4,5,6], time = 2
输出:[]
解释:
没有任何一天的前 2 天建议出行指数是非递增的。
所以没有适合野炊的日子,返回空数组。

 

提示:

  • 1 <= security.length <= 105
  • 0 <= security[i], time <= 105
通过次数
36.3K
提交次数
74.4K
通过率
48.8%


相关企业

提示 1
The trivial solution is to check the time days before and after each day. There are a lot of repeated operations using this solution. How could we optimize this solution?

提示 2
We can use precomputation to make the solution faster.

提示 3
Use an array to store the number of days before the ith day that is non-increasing, and another array to store the number of days after the ith day that is non-decreasing.


评论 (0)

贡献者
© 2025 领扣网络(上海)有限公司
0 人在线
行 1,列 1
运行和提交代码需要登录
security =
[5,3,3,3,5,6,2]
time =
2
Source