Skip to content

Instantly share code, notes, and snippets.

@mayonesa
Created February 6, 2022 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayonesa/7d374a06ac75c19156f0813861a5c92f to your computer and use it in GitHub Desktop.
Save mayonesa/7d374a06ac75c19156f0813861a5c92f to your computer and use it in GitHub Desktop.
Find the longest period of time when there are no ongoing meetings
import math.max
/*
James is a businessman. He is on a tight schedule this week. The week starts
on Monday at 00:00 and ends on Sunday at 24:00. His schedule consists of M
meetings he needs to take part in. Each of them will take place in a period of
time, beginning and ending on the same day (there are no two ongoing meetings
at the same time). James is very tired, thus he needs to find the longest
possible time slot to sleep. In other words, he wants to find the longest
period of time when there are no ongoing meetings. The sleeping break can
begin and end on different days and should begin and end in the same week.
You are given a string containing M lines. Each line is a substring
representing one meeting in the schedule, in the format "Ddd hh:mm-hh:mm".
"Ddd" is a three-letter abbreviation for the day of the week when the meeting
takes place: "Mon" (Monday), "Tue", "Wed", "Thu", "Fri", "Sat", "Sun".
"hh:mm-hh:mm" means the beginning time and the ending time of the meeting
(from 00:00 to 24:00 inclusive).
The given times represent exact moments of time. So, there are exactly five
minutes between 13:40 and 13:45.
For example, given a string:
"Sun 10:00-20:00
Fri 05:00-10:00
Fri 16:30-23:50
Sat 10:00-24:00
Sun 01:00-04:00
Sat 02:00-06:00
Tue 03:30-18:15
Tue 19:00-20:00
Wed 04:25-15:14
Wed 15:14-22:40
Thu 00:00-23:59
Mon 05:00-13:00
Mon 15:00-21:00"
The longest time slot when James can sleep is 505 minutes, since James can
sleep from Tuesday 20:00 to Wednesday 04:25, which gives 8 hours and 25
minutes = 505 minutes.
Also, for a string:
"Mon 01:00-23:00
Tue 01:00-23:00
Wed 01:00-23:00
Thu 01:00-23:00
Fri 01:00-23:00
Sat 01:00-23:00
Sun 01:00-21:00"
James should sleep on Sunday from 21:00 to 24:00 (180 minutes).
Write a function:
object Solution { def solution(s: String): Int }
that, given a string S representing the schedule, returns the length of the
longest time slot when James can sleep (in minutes).
Assume that:
● M is an integer within the range [1..100];
● Each line of the input string is in the format "Ddd hh:mm-hh:mm" and
lines are separated with newline characters;
● There cannot be two ongoing meetings at any time;
● Each meeting lasts at least 1 minute.
In your solution, focus on correctness. The performance of your solution will
not be the focus of the assessment.
*/
object Week {
def largestGapInMinutes(s: String): Int = {
val startFakeMeeting = new Meeting("Mon 00:00-00:00")
val finalGap = new Meeting("Sun 24:00-24:00")
val sortedMeetings = startFakeMeeting +: s.linesIterator.map(new Meeting(_)).toSeq.sortBy { meeting =>
(meeting.dow, meeting.start)
} :+ finalGap
(1 until sortedMeetings.size).foldLeft(0) { case (largestGapInMinutes, i) =>
val previousMeeting = sortedMeetings(i - 1)
val meeting = sortedMeetings(i)
val gap = meeting.gapInMinutes(previousMeeting)
max(largestGapInMinutes, gap)
}
}
}
import Meeting._
import java.time.DayOfWeek
import java.util.Date
import java.util.concurrent.TimeUnit
import TimeUnit.{MINUTES => Minutes}
import TimeUnit.{DAYS => Days}
import TimeUnit.{MILLISECONDS => Milliseconds}
class Meeting(slot: String) {
private val (startStr, endRaw) = slot.splitAt(DashI)
private val (dowShort, startTimeStrRaw) = startStr.splitAt(DowI)
private val startTimeStr = startTimeStrRaw.tail
private val endStr = endRaw.tail
val dow: DayOfWeek = DowFromShort(dowShort)
val start: Date = TimeFormat.parse(startTimeStr)
val end: Date = TimeFormat.parse(endStr)
def gapInMinutes(previous: Meeting): Int = {
def diffInMinutes(f: Meeting => Long, previousF: Meeting => Long, timeUnit: TimeUnit) =
Minutes.convert(f(this) - previousF(previous), timeUnit).toInt
val dayDiffF: Meeting => Long = _.dow.getValue
val dayDiffInMinutes = diffInMinutes(dayDiffF, dayDiffF, Days)
val timeDiffInMinutes = diffInMinutes(_.start.getTime, _.end.getTime, Milliseconds)
dayDiffInMinutes + timeDiffInMinutes
}
override def toString: String = slot
}
import java.text.SimpleDateFormat
object Meeting {
private val DashI = 9
private val DowI = 3
private val TimeFormat = new SimpleDateFormat("HH:mm")
private val DowFromShort = DayOfWeek.values.map { dow =>
val allCapsShort = dow.name.take(3)
allCapsShort.head + allCapsShort.drop(1).toLowerCase -> dow
}.toMap
}
import org.scalatest.flatspec.AnyFlatSpec
class WeekSpec extends AnyFlatSpec {
"nominal sched" should "work" in {
val meetings = """Sun 10:00-20:00
|Fri 05:00-10:00
|Fri 16:30-23:50
|Sat 10:00-24:00
|Sun 01:00-04:00
|Sat 02:00-06:00
|Tue 03:30-18:15
|Tue 19:00-20:00
|Wed 04:25-15:14
|Wed 15:14-22:40
|Thu 00:00-23:59
|Mon 05:00-13:00
|Mon 15:00-21:00""".stripMargin
assert(Week.largestGapInMinutes(meetings) === 505)
}
"last gap (it is the largest)" should "be chosen" in {
val meetings = """Mon 01:00-23:00
|Tue 01:00-23:00
|Wed 01:00-23:00
|Thu 01:00-23:00
|Fri 01:00-23:00
|Sat 01:00-23:00
|Sun 01:00-21:00""".stripMargin
assert(Week.largestGapInMinutes(meetings) === 180)
}
"initial gap (it is the largest)" should "be chosen" in {
val meetings = """Tue 01:00-23:00
|Wed 01:00-23:00
|Thu 01:00-23:00
|Fri 01:00-23:00
|Sat 01:00-23:00
|Sun 01:00-21:00""".stripMargin
assert(Week.largestGapInMinutes(meetings) === 1500)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment