Skip to content

Commit 171c6e7

Browse files
solves #2760: Longest Even Odd Subarray With Threshold in java
1 parent 26bd397 commit 171c6e7

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

README.txt

+7-7
Original file line numberDiff line numberDiff line change
@@ -828,14 +828,14 @@
828828
| 2706 | [Buy Two Chocolates](https://leetcode.com/problems/buy-two-chocolates) | [![Java](assets/java.png)](src/BuyTwoChocolates.java) | |
829829
| 2710 | [Remove Trailing Zeros From a String](https://leetcode.com/problems/remove-trailing-zeros-from-a-string) | [![Java](assets/java.png)](src/RemoveTrailingZerosFromAString.java) | |
830830
| 2716 | [Minimize String Length](https://leetcode.com/problems/minimize-string-length) | [![Java](assets/java.png)](src/MinimizeStringLength.java) | |
831-
| 2717 | [Semi-Ordered Permutation](https://leetcode.com/problems/semi-ordered-permutation) | [![Java](assets/java.png)](src/SemiOrderedPermutation.java) | |
831+
| 2717 | [Semi-Ordered Permutation](https://leetcode.com/problems/semi-ordered-permutation) | [![Java](assets/java.png)](src/SemiOrderedPermutation.java) | |
832832
| 2728 | [Count Houses in a Circular Street](https://leetcode.com/problems/count-houses-in-a-circular-street) | | |
833-
| 2729 | [Check if The Number is Fascinating](https://leetcode.com/problems/check-if-the-number-is-fascinating) | [![Java](assets/java.png)](src/CheckIfTheNumberIsFascinating.java) | |
834-
| 2733 | [Neither Minimum nor Maximum](https://leetcode.com/problems/neither-minimum-nor-maximum) | [![Java](assets/java.png)](src/NeitherMinimumNorMaximum.java) | |
835-
| 2739 | [Total Distance Traveled](https://leetcode.com/problems/total-distance-traveled) | [![Java](assets/java.png)](src/TotalDistanceTraveled.java) | |
836-
| 2744 | [Find Maximum Number of String Pairs](https://leetcode.com/problems/find-maximum-number-of-string-pairs) | [![Java](assets/java.png)](src/FindMaximumNumberOfStringPairs.java) | |
837-
| 2748 | [Number of Beautiful Pairs](https://leetcode.com/problems/number-of-beautiful-pairs) | [![Java](assets/java.png)](src/)NumberOfBeautifulPairs.java) | |
838-
| 2760 | [Longest Even Odd Subarray With Threshold](https://leetcode.com/problems/longest-even-odd-subarray-with-threshold) | | |
833+
| 2729 | [Check if The Number is Fascinating](https://leetcode.com/problems/check-if-the-number-is-fascinating) | [![Java](assets/java.png)](src/CheckIfTheNumberIsFascinating.java) | |
834+
| 2733 | [Neither Minimum nor Maximum](https://leetcode.com/problems/neither-minimum-nor-maximum) | [![Java](assets/java.png)](src/NeitherMinimumNorMaximum.java) | |
835+
| 2739 | [Total Distance Traveled](https://leetcode.com/problems/total-distance-traveled) | [![Java](assets/java.png)](src/TotalDistanceTraveled.java) | |
836+
| 2744 | [Find Maximum Number of String Pairs](https://leetcode.com/problems/find-maximum-number-of-string-pairs) | [![Java](assets/java.png)](src/FindMaximumNumberOfStringPairs.java) | |
837+
| 2748 | [Number of Beautiful Pairs](https://leetcode.com/problems/number-of-beautiful-pairs) | [![Java](assets/java.png)](src/NumberOfBeautifulPairs.java) | |
838+
| 2760 | [Longest Even Odd Subarray With Threshold](https://leetcode.com/problems/longest-even-odd-subarray-with-threshold) | [![Java](assets/java.png)](src/LongestEvenOddSubarrayWithThreshold.java) | |
839839
| 2765 | [Longest Alternating Subarray](https://leetcode.com/problems/longest-alternating-subarray) | | |
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) | | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://leetcode.com/problems/longest-even-odd-subarray-with-threshold
2+
// T: O(N)
3+
// S: O(N)
4+
5+
public class LongestEvenOddSubarrayWithThreshold {
6+
public int longestAlternatingSubarray(int[] array, int threshold) {
7+
final int[] dp = new int[array.length];
8+
dp[0] = array[0] % 2 == 0 && array[0] <= threshold ? 1 : 0;
9+
int maxLength = dp[0];
10+
for (int i = 1 ; i < dp.length ; i++) {
11+
if (array[i] <= threshold && array[i] % 2 != array[i - 1] % 2 && dp[i - 1] > 0) dp[i] = dp[i - 1] + 1;
12+
else if (array[i] % 2 == 0 && array[i] <= threshold) dp[i] = 1;
13+
else dp[i] = 0;
14+
maxLength = Math.max(maxLength, dp[i]);
15+
}
16+
return maxLength;
17+
}
18+
}

0 commit comments

Comments
 (0)