Skip to content

Commit 5622751

Browse files
committed
Added 2 solutions
1 parent 796a99d commit 5622751

2 files changed

+52
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public boolean containsPattern(int[] arr, int m, int k) {
3+
Queue<Integer> queue = new LinkedList<>();
4+
int count = 0;
5+
int freq = 1;
6+
for (int i = 0; i < arr.length; i++) {
7+
if (i >= m) {
8+
if (arr[i] == queue.poll()) {
9+
count++;
10+
if (count == m) {
11+
freq++;
12+
count = 0;
13+
}
14+
if (freq == k) {
15+
return true;
16+
}
17+
}
18+
else {
19+
count = 0;
20+
freq = 1;
21+
}
22+
}
23+
queue.add(arr[i]);
24+
}
25+
return false;
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int getMaxLen(int[] nums) {
3+
int startIndex = -1;
4+
int firstNegativeIndex = -1;
5+
int negativeCount = 0;
6+
int maxLen = 0;
7+
for (int i = 0; i < nums.length; i++) {
8+
if (nums[i] < 0) {
9+
negativeCount++;
10+
if (firstNegativeIndex == -1) {
11+
firstNegativeIndex = i;
12+
}
13+
}
14+
else if (nums[i] == 0) {
15+
negativeCount = 0;
16+
firstNegativeIndex = -1;
17+
startIndex = i;
18+
}
19+
maxLen = Math.max(
20+
maxLen, (negativeCount % 2 == 0 ? i - startIndex : i - firstNegativeIndex)
21+
);
22+
}
23+
return maxLen;
24+
}
25+
}

0 commit comments

Comments
 (0)