Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
public class Solution {
private static boolean shouldUserBeAsked(List<String> dates) {
Collections.reverse(dates);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime lastDateTime = LocalDateTime.parse(dates.get(0), formatter);
// Start with ones because there will always be at least one-day-sequence and at least one session.
int numberOfSessions = 1;
int daysInARow = 1;
for (String s : dates) {
LocalDateTime dateTime = LocalDateTime.parse(s, formatter);
// Calculate time difference in minutes.
if (dateTime.until(lastDateTime, ChronoUnit.MINUTES) > 30)
numberOfSessions += 1;
// Using day of the week because of the constant modulo.
int daysDifference = (lastDateTime.getDayOfWeek().getValue() - dateTime.getDayOfWeek().getValue()) % 7;
if (daysDifference == 1)
daysInARow += 1;
// User broke sequence so we can just stop.
if (daysDifference > 1)
break;
lastDateTime = dateTime;
}
return numberOfSessions >= 6 && daysInARow >= 3;
}
public static void main(String[] args) {
String[] A = new String[] { "2017-03-10 08:13:11", "2017-03-10 19:01:27", "2017-03-11 07:35:55", "2017-03-11 16:15:11", "2017-03-12 08:01:41", "2017-03-12 17:19:08" };
List<String> a = new ArrayList<>(Arrays.asList(A));
String[] B = new String[] { "2017-03-10 18:58:11", "2017-03-10 19:01:27", "2017-03-11 07:35:55", "2017-03-11 16:15:11", "2017-03-12 08:01:41", "2017-03-12 17:19:08" };
List<String> b = new ArrayList<>(Arrays.asList(B));
String[] C = new String[] { "2017-03-08 17:11:13", "2017-03-11 17:22:47", "2017-03-11 19:23:51", "2017-03-11 22:03:12", "2017-03-12 08:32:04", "2017-03-12 13:19:08", "2017-03-12 17:19:08" };
List<String> c = new ArrayList<>(Arrays.asList(C));
System.out.println(shouldUserBeAsked(a));
System.out.println(shouldUserBeAsked(b));
System.out.println(shouldUserBeAsked(c));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment