Skip to content

Commit bc1778d

Browse files
committed
Time: 1 ms (100.00%), Space: 45.5 MB (46.81%) - LeetHub
1 parent c1217fa commit bc1778d

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

3254-find-the-power-of-k-size-subarrays-i/3254-find-the-power-of-k-size-subarrays-i.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
11
class Solution {
22
public int[] resultsArray(int[] nums, int k) {
3+
// int n = nums.length;
4+
// int start =0;
5+
// int end = start+k-1;
6+
// int[] res = new int[n-k+1];
7+
// while(end<nums.length && start<n-k+1){
8+
// int sol = findMaxAndCheckSorted(nums, start , end);
9+
// res[start]=sol;
10+
// start++;
11+
// end++;
12+
// }
13+
// return res;
14+
15+
316
int n = nums.length;
4-
int start =0;
5-
int end = start+k-1;
6-
int[] res = new int[n-k+1];
7-
while(end<nums.length && start<n-k+1){
8-
int sol = findMaxAndCheckSorted(nums, start , end);
9-
res[start]=sol;
10-
start++;
11-
end++;
12-
17+
int[] p = new int[n];
18+
for (int i = 1; i < n; i++)
19+
{
20+
p[i] = p[i - 1] + ((nums[i] == (nums[i - 1] + 1)) ? 1 : 0);
1321
}
14-
return res;
22+
23+
var ans = new int[n - k + 1];
24+
for (int i = 0; i < n - k + 1; i++)
25+
{
26+
if (p[i + k - 1] == (p[i] + k - 1))
27+
{
28+
ans[i] = (nums[i + k - 1]);
29+
}
30+
else
31+
{
32+
ans[i] = -1;
33+
}
34+
}
35+
return ans;
1536
}
1637

1738
public int findMaxAndCheckSorted(int[] arr ,int start , int end){

0 commit comments

Comments
 (0)