Last active
December 7, 2015 09:31
-
-
Save hector6872/f6b40d7d91506802f424 to your computer and use it in GitHub Desktop.
findLongestStreak() method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IntDef({ | |
FIND_STREAK_LESS_THAN_ZERO, FIND_STREAK_LESS_EQUAL_TO_ZERO, FIND_STREAK_LESS_GREATER_ZERO | |
}) @Retention(RetentionPolicy.SOURCE) public @interface FIND_STREAK_OPERATOR { | |
} | |
public static final int FIND_STREAK_LESS_THAN_ZERO = 0; | |
public static final int FIND_STREAK_LESS_EQUAL_TO_ZERO = 1; | |
public static final int FIND_STREAK_LESS_GREATER_ZERO = 2; | |
public int findLongestStreak(float[] values, @FIND_STREAK_OPERATOR int operator) { | |
int longestStreak = 0; | |
int currentLongestStreak = 0; | |
for (int lap = 0; lap < 2; lap++) { | |
for (float f : values) { | |
if ((operator == FIND_STREAK_LESS_THAN_ZERO && f < 0) || | |
(operator == FIND_STREAK_LESS_EQUAL_TO_ZERO && f == 0) || | |
(operator == FIND_STREAK_LESS_GREATER_ZERO && f > 0)) { | |
currentLongestStreak++; | |
} else { | |
currentLongestStreak = 0; | |
} | |
if (currentLongestStreak > longestStreak) { | |
longestStreak = currentLongestStreak; | |
if (longestStreak > values.length) { | |
longestStreak = values.length; | |
} | |
} | |
} | |
} | |
return longestStreak; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment