Comment

Comment by FT_intern

The most logical and clear answer for me (assuming I didn't already screw up by making assumptions about the data format, problem definition, etc.) is:

1. Figure out which appointment starts first

2. Check if first_appointment.end > second_appointment.start

So:

    boolean AreAppointmentsConflicting(int start1, int start2, int end1, int end2) {
      // first and second refer to the start time of the appointments.
      // The first appointment is the one with the earlier start time.
      int first_appointment_end, second_appointment_start;
      if (start1 > start2) {
          first_appointment_end = end2;
          second_appointment_start = start1;
      } else if (start1 < start2) {
          first_appointment_end = end1;
          second_appointment_start = start2;
      } else { // same start time
        return true;
      }
      return first_appointment_end > second_appointment_start;
    }
That can be shortened to:

   return (start1 > start2 && end2 > start1) || (start1 < start2 && end1 > start2) || start1 == start2;
Or shortened even further in https://news.ycombinator.com/item?id=14641485

The third answer would get massive upvotes for brevity on LeetCode. The first answer would be preferable for readability in an actual code base.

parent FT_intern 9 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton