Parenting Partnering Returns - Google CodeJam 2020 Qualification Round Problem Solution
Cameron and Jamie's kid is almost 3 years old! However, even though the child is more independent now, scheduling kid activities and domestic necessities is still a challenge for the couple.
Cameron and Jamie have a list of N activities to take care of during the day. Each activity happens during a specified interval during the day. They need to assign each activity to one of them, so that neither of them is responsible for two activities that overlap. An activity that ends at time t is not considered to overlap with another activity that starts at time t.
For example, suppose that Jamie and Cameron need to cover 3 activities: one running from 18:00 to 20:00, another from 19:00 to 21:00 and another from 22:00 to 23:00. One possibility would be for Jamie to cover the activity running from 19:00 to 21:00, with Cameron covering the other two. Another valid schedule would be for Cameron to cover the activity from 18:00 to 20:00 and Jamie to cover the other two. Notice that the first two activities overlap in the time between 19:00 and 20:00, so it is impossible to assign both of those activities to the same partner.
Given the starting and ending times of each activity, find any schedule that does not require the same person to cover overlapping activities, or say that it is impossible.
The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a line containing a single integer N, the number of activities to assign. Then, N more lines follow. The i-th of these lines (counting starting from 1) contains two integers Si and Ei. The i-th activity starts exactly Si minutes after midnight and ends exactly Ei minutes after midnight.
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is IMPOSSIBLE if there is no valid schedule according to the above rules, or a string of exactly N characters otherwise. The i-th character in y must be C if the i-th activity is assigned to Cameron in your proposed schedule, and J if it is assigned to Jamie.
1 ≤ T ≤ 100. 0 ≤ Si< Ei ≤ 24 × 60. 2 ≤ N ≤ 1000.
Input: 4 3 360 480 420 540 600 660 3 0 1440 1 3 2 4 5 99 150 1 100 100 301 2 5 150 250 2 0 720 720 1440 Output: Case #1: CJC Case #2: IMPOSSIBLE Case #3: JCCJJ Case #4: CC
This is kind of job scheduling problem and thus firstly we need to sort the intervals based on starting time.
After sorting, we basically has two person to assign the jobs without overlapping. But since after sorting the position of the jobs will be changed so, we need to track their original indexes for final output.
WE have two person to assign jobs , Jaime as J and Cameron as C
We would declare two stacks namely Stack J and Stack C for them respectively.
Also, result will be our output which is a character array of length equal to number of jobs.
We will assign job based on their sorted order.
We will first try to assign job to J, if it's not possible then will try to assign job to C, if that's also not possible then it's impossible to assign jobs.
If job is possible to assign, we will push the job to the respective stack and we will mark the original index of the job with respective name ('C' or 'J' depending on which stack we assigned)
To keep track of original job index we create a structure like below:
Then sort the input list based on start time. For details refer to the comparator function in my code.
Let the sorted list be jobs with length n
Remember jobs.index may not be 0 as we have sorted the original list and this will return the original index from the input. (this the purpose of the structure we created).
Dry run with example: