I was successfully able to solve the problem post the competition. For
everyone's benefit i'd like to point out the following common mistakes that
people are doing, one of which I didn't think mattered, but unfortunately
it did.
1. You must sort the activities by start time before trying to assign them.
This is something I didn't think mattered, but was unfortunately the reason
I got WA.
2. You must output the assignments in the correct order as was provided in
the input, since we need to sort the activities, you must therefore ensure
you keep a reference to the original order to ensure the answer you return
matches that.
On Tuesday, April 7, 2020 at 9:50:31 PM UTC+5:30, samuel jawahar wrote:
>
> Hello sir,please let me know for which test case it is failing
> please review my code
> Regards
> Samuel
>
>
>
>
> ===========================
> import java.io.BufferedReader;
> import java.io.IOException;
> import java.io.InputStreamReader;
> import java.util.ArrayList;
>
> public class Solution {
> private static boolean isOverLapping(ArrayList<Activity> activityList,
> Activity activity) {
> int len = activityList.size();
> Activity aux = null;
> for (int i = 0; i < len; i++) {
> aux = activityList.get(i);
> if ((aux.s == activity.s) && (aux.e == activity.e)) {
> return true;
> }
>
> if ((aux.s <= activity.s) && (aux.e >= activity.e)) {
> return true;
> }
>
> if ((aux.s <= activity.s) && (aux.e >= activity.e)) {
> return true;
> }
> if ((activity.s <= aux.s) && (activity.e >= aux.e)) {
> return true;
> }
> if (aux.s < activity.s) {
> if (aux.e > activity.s) {
> return true;
> }
> }
> if (activity.s < aux.s) {
> if (activity.e > aux.s) {
> return true;
> }
> }
> }
> return false;
> }
>
> public static void main(String[] args) {
> // TODO Auto-generated method stub
> BufferedReader br = new BufferedReader(new
> InputStreamReader(System.in));
> try {
> int t = Integer.parseInt(br.readLine());
> int n = 0, s = 0, e = 0;
> String data[] = null;
> StringBuilder result = new StringBuilder();
> ArrayList<Activity> cameron = new ArrayList<Activity>();
> ArrayList<Activity> jamie = new ArrayList<Activity>();
> for (int i = 1; i <= t; i++) {
> cameron.clear();
> jamie.clear();
> n = Integer.parseInt(br.readLine());
> result = result.delete(0, result.capacity());
> for (int j = 0; j < n; j++) {
> if (result.toString().trim().equals("IMPOSSIBLE")) {
> String aux = br.readLine();
> continue;
> }
> data = br.readLine().split(" ");
> s = Integer.parseInt(data[0]);
> e = Integer.parseInt(data[1]);
> Activity activity = new Activity(s, e);
>
> if (!isOverLapping(cameron, activity)) {
> cameron.add(activity);
> result.append("C");
> } else {
> if (!isOverLapping(jamie, activity)) {
> jamie.add(activity);
> result.append("J");
> } else {
> result = result.delete(0, result.capacity());
> result.append("IMPOSSIBLE");
> }
> }
> }
> System.out.println("Case #" + i + ": " +
> result.toString());
> result = result.delete(0, result.capacity());
> cameron.clear();
> jamie.clear();
> }
>
> } catch (NumberFormatException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
> }
>
> }
>
> class Activity {
> int s = 0, e = 0;
>
> Activity(int s, int e) {
> this.s = s;
> this.e = e;
> }
>
> public String toString() {
> return s + ":" + e;
> }
> }
>
--
You received this message because you are subscribed to the Google Groups
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/5ba797d5-a352-4cc3-8e2d-104adb7b546b%40googlegroups.com.