Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6af4af1

Browse files
solves
1 parent 171c6e7 commit 6af4af1

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

‎README.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@
836836
| 2744 | [Find Maximum Number of String Pairs](https://leetcode.com/problems/find-maximum-number-of-string-pairs) | [![Java](assets/java.png)](src/FindMaximumNumberOfStringPairs.java) | |
837837
| 2748 | [Number of Beautiful Pairs](https://leetcode.com/problems/number-of-beautiful-pairs) | [![Java](assets/java.png)](src/NumberOfBeautifulPairs.java) | |
838838
| 2760 | [Longest Even Odd Subarray With Threshold](https://leetcode.com/problems/longest-even-odd-subarray-with-threshold) | [![Java](assets/java.png)](src/LongestEvenOddSubarrayWithThreshold.java) | |
839-
| 2765 | [Longest Alternating Subarray](https://leetcode.com/problems/longest-alternating-subarray) | | |
839+
| 2765 | [Longest Alternating Subarray](https://leetcode.com/problems/longest-alternating-subarray) | [![Java](assets/java.png)](src/LongestAlternatingSubarray.java) | |
840840
| 2769 | [Find the Maximum Achievable Number](https://leetcode.com/problems/find-the-maximum-achievable-number) | | |
841841
| 2778 | [Sum of Squares of Special Elements](https://leetcode.com/problems/sum-of-squares-of-special-elements) | | |
842842
| 2784 | [Check if Array is Good](https://leetcode.com/problems/check-if-array-is-good) | | |

‎src/LongestAlternatingSubarray.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// https://leetcode.com/problems/longest-alternating-subarray
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class LongestAlternatingSubarray {
6+
public int alternatingSubarray(int[] array) {
7+
int maxLength = 0, j = 0;
8+
for (int i = 0 ; i < array.length ; i = Math.max(i + 1, j - 1)) {
9+
for (j = i + 1 ; j < array.length && array[j] == array[i] + (j - i) % 2 ; j++) {
10+
maxLength = Math.max(maxLength, j - i + 1);
11+
}
12+
}
13+
return maxLength > 1 ? maxLength : -1;
14+
}
15+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /