Given a list of bad numbers and lo and hi range, find the biggest streak of good numbers.
class Solution {
public int driver() {
int[] bad = new int[]{1,15,27};
int lo = 27;
int hi = 30;
return bigStreak(bad, lo, hi);
}
private int bigStreak(int[] bad, int lo, int hi) {
int max = 0;
int i = 0;
for(; i<bad.length-1; i++) {
int curr = 0;
if (hi < bad[i]) {
curr = hi - lo + 1;
if(curr > max) max = curr;
break;
} else if (hi == bad[i]) {
curr = hi - lo;
if (curr > max) max = curr;
break;
} else if (hi > bad[i] && hi <= bad[i + 1]) {
if (lo < bad[i]) {
int s1 = bad[i] - 1 - lo + 1;
int s2 = hi - (bad[i] + 1) + 1;
if (hi == bad[i + 1]) --s2;
curr = Math.max(s1, s2);
} else if (lo == bad[i]) {
curr = hi - (bad[i] + 1) + 1;
if (hi == bad[i + 1]) --curr;
} else if (lo > bad[i]) {
curr = hi - lo + 1;
if (hi == bad[i + 1]) --curr;
}
if (curr > max) max = curr;
break;
} else if (hi > bad[i + 1]){
if (lo <= bad[i]) {
int s1 = bad[i] - lo;
int s2 = bad[i + 1] - bad[i] - 1;
curr = Math.max(s1, s2);
lo = bad[i + 1] + 1;
} else if (lo > bad[i] && lo <= bad[i + 1]) {
curr = bad[i + 1] - lo;
lo = bad[i + 1] + 1;
} else if ( lo > bad[i + 1]) {
;
}
if (curr > max) max = curr;
}
}
if ( i == bad.length - 1) {
int curr = hi - lo + 1;
if ( curr > max ) max = curr;
}
return max;
}
}
public int driver() {
int[] bad = new int[]{1,15,27};
int lo = 27;
int hi = 30;
return bigStreak(bad, lo, hi);
}
private int bigStreak(int[] bad, int lo, int hi) {
int max = 0;
int i = 0;
for(; i<bad.length-1; i++) {
int curr = 0;
if (hi < bad[i]) {
curr = hi - lo + 1;
if(curr > max) max = curr;
break;
} else if (hi == bad[i]) {
curr = hi - lo;
if (curr > max) max = curr;
break;
} else if (hi > bad[i] && hi <= bad[i + 1]) {
if (lo < bad[i]) {
int s1 = bad[i] - 1 - lo + 1;
int s2 = hi - (bad[i] + 1) + 1;
if (hi == bad[i + 1]) --s2;
curr = Math.max(s1, s2);
} else if (lo == bad[i]) {
curr = hi - (bad[i] + 1) + 1;
if (hi == bad[i + 1]) --curr;
} else if (lo > bad[i]) {
curr = hi - lo + 1;
if (hi == bad[i + 1]) --curr;
}
if (curr > max) max = curr;
break;
} else if (hi > bad[i + 1]){
if (lo <= bad[i]) {
int s1 = bad[i] - lo;
int s2 = bad[i + 1] - bad[i] - 1;
curr = Math.max(s1, s2);
lo = bad[i + 1] + 1;
} else if (lo > bad[i] && lo <= bad[i + 1]) {
curr = bad[i + 1] - lo;
lo = bad[i + 1] + 1;
} else if ( lo > bad[i + 1]) {
;
}
if (curr > max) max = curr;
}
}
if ( i == bad.length - 1) {
int curr = hi - lo + 1;
if ( curr > max ) max = curr;
}
return max;
}
}
No comments:
Post a Comment