Last active
June 23, 2020 14:01
-
-
Save ashkrit/836d96992e8d00627202c1680dc49cfa to your computer and use it in GitHub Desktop.
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
public class CustomerActivity { | |
private final int[] months = new int[12]; | |
public void record(LocalDate day) { | |
int monthOffSet = day.getMonthValue() - 1; | |
int monthValue = months[monthOffSet]; | |
// Set bit for day in 32 bit int and then OR(|) with month value to merge value | |
months[monthOffSet] = monthValue | 1 << (day.getDayOfMonth() - 1); | |
} | |
public int daysActive(Month month) { | |
int monthValue = months[month.ordinal()]; | |
return countBits(monthValue); | |
} | |
public boolean wasActive(LocalDate day) { | |
int monthOffSet = day.getMonthValue() - 1; | |
int monthValue = months[monthOffSet]; | |
// Set bit for day in 32 bit int and then AND(|) with month value to check if bit was set | |
return (monthValue & 1 << (day.getDayOfMonth() - 1)) > 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment