Cooked
BAN USERpublic boolean checkIfOverLap(int [] first, int [] second) {
if (first[1] < second[0] || second[1] < first[0]) return false;
return true;
}
public void findOverlaps(int [][] first, int [][] second) {
int i=0;
int j=0;
while (i < first.length && j < second.length) {
// check overlap
if (checkIfOverLap(first[i], second[j])) {
int start = Math.max(first[i][0], second[j][0]);
int end = Math.min(first[i][1], second[j][1]);
System.out.println("Interval intersect: " + start + " ," + end);
if (first[i][1] < second[j][0]) i++;
else j++;
} else {
// don't overlap
if (first[i][1] < second[j][0]) i++;
else j++;
}
}
}
import java.util.HashSet;
import java.util.Set;
public class ValidLotterySize {
public static void main(String[] args) {
System.out.println(isValidLotterySize("4938532894754", 0, new HashSet<>()));
System.out.println(isValidLotterySize("1634616512", 0, new HashSet<>()));
System.out.println(isValidLotterySize("1234567", 0, new HashSet<>()));
}
public static boolean isValidLotterySize(String input, int index, Set<Integer> unqiue) {
if (index == input.length()) {
return unqiue.size() == 7;
}
if (index < input.length() && unqiue.size() >= 7) {
return false;
}
// case 0:
int currentValue = Integer.parseInt(input.substring(index, index+1));
if (!unqiue.contains(currentValue)) {
Set<Integer> newUnique = new HashSet<>(unqiue);
newUnique.add(currentValue);
boolean validLotterySize = isValidLotterySize(input, index + 1, newUnique);
if (validLotterySize) {
return true;
}
}
// case 1:
if (index < input.length() -1) {
int currentValuetwo = Integer.parseInt(input.substring(index, index+2));
if (!unqiue.contains(currentValuetwo)) {
Set<Integer> newUnique = new HashSet<>(unqiue);
newUnique.add(currentValuetwo);
boolean validLotterySize = isValidLotterySize(input, index + 2, newUnique);
if (validLotterySize) {
return true;
}
}
}
return false;
}
}
void printDate(){
int yearInput = 2100;
int months = 12;
int days = 31;
int i ,j;
for ( i=1;i<=12;i++){
if( ( i <= 7 && i % 2 !=0) || ( i >=8 && i %2 ==0)){
//its 31 dates
days=31;
}
else if( (i!=2) && (( i <=7 && i%2 == 0) || (i>=9 && i%2!= 0))){
// its 30 days
days = 30;
}
else{
//its feb
days = 28;
if( ((yearInput % 4 == 0) && (yearInput % 100 !=0)) || (yearInput%400 == 0) )
days = 29;
}
for(j=1;j<=days;j++){
printf ("\nDay:%2d, Month:%2d , Year:%4d",j,i,yearInput);
}
}
}
#include<stdio.h>
#include<limits.h>
void printMaxOddMinEven(int a ){
int i = 0;
int maxOdd =INT_MAX;
int minEven = INT_MIN;
int inputs[10] = {3,5,10,234,124,11,201,100,20,2};
while(i!=10){
if(inputs[i]%2 == 0){
if(minEven == INT_MIN){
minEven = inputs[i];
}else if(minEven > inputs[i]){
minEven = inputs[i];
}
}else{
if(maxOdd==INT_MAX){
maxOdd=inputs[i];
}else if(maxOdd < inputs[i]){
maxOdd = inputs[i];
}
}
i++;
}
printf("\n MaxOdd:%d, MinEven:%d",maxOdd,minEven);
}
RepMariaHobbs, Consultant at Adobe
Hi, I am Maria Hobbs from NewYork.Teach career development courses for designated areas. Develop, evaluate and revise course materials ...
solve is using max substring problem.
- Cooked March 24, 2018