Skip to content

Instantly share code, notes, and snippets.

@hrules6872
Last active December 7, 2015 09:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrules6872/f6b40d7d91506802f424 to your computer and use it in GitHub Desktop.
Save hrules6872/f6b40d7d91506802f424 to your computer and use it in GitHub Desktop.
findLongestStreak() method
@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