deno.land / x / masx200_leetcode_test@10.6.5 / find-all-good-indices / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
export default function goodIndices(nums: number[], k: number): number[] { const n = nums.length; if (k * 2 + 1 > n) return [];
const pre: number[] = Array(n - k * 2).fill(1); const post: number[] = Array(n - k * 2).fill(1);
for (let i = 0; i < pre.length; i++) { if (i === 0) { for (let j = 1; j < k; j++) { pre[i] = nums[j] <= nums[j - 1] ? pre[i] + 1 : 1; } } else { const j = i + k - 1;
pre[i] = nums[j] <= nums[j - 1] ? pre[i - 1] + 1 : 1; } } for (let i = 0; i < post.length; i++) { if (i === 0) { for (let j = n - 2; j > n - k - 1; j--) { post[post.length - 1 - i] = nums[j] <= nums[j + 1] ? post[post.length - 1 - i] + 1 : 1; } } else { const j = nums.length - i - k;
post[post.length - 1 - i] = nums[j] <= nums[j + 1] ? post[post.length - i] + 1 : 1; } }
return [...Array(n - k * 2).keys()] .filter((i) => pre[i] >= k && post[i] >= k) .map((i) => i + k);}
masx200_leetcode_test

Version Info

Tagged at
a year ago