Topic 1:
It means one day from 00:00 to 24.00 (24.00 for the second day of 0.00). We cannot work more than 2 tasks at the same time, meaning that we can only do one job at a moment. And each job is a time period. For example, work 1 is
[08:00] to [12:00], work 2 is [06:00] to [09:00]. Now give you a day's work schedule. The maximum number of simultaneous work. Work 1 and 2, for example, work at the same time between 8 and 9 points. Note: [08:00,09:00] and [09.00,10:00] Two work is also overlapped because include "09:00"
Here's my solution.
public class Problem1 {public int getmaxintervaloverlapcount (list<interval> intervals) {int max_size = 1440; if (intervals = = null| |
Intervals.size () ==0) return 0;
Int[] A = new int[max_size];
Int[] B = new Int[intervals.size () * 2];
int begin;
int end;
int num = 0;
int maxoverlapcount = 0;
iterator<interval> Iterator = Intervals.iterator ();
while (Iterator.hasnext ()) {Interval t = iterator.next ();
Begin = T.getbeginminuteunit ();
End = T.getendminuteunit ();
AddArray (begin, end, a);
B[num] = T.getbeginminuteunit ();
B[num + 1] = T.getendminuteunit ();
num = num + 2;
}//The Maxoverlapcount can be found these points.
Maxoverlapcount = a[b[0]];
for (int i = 1; i < b.length; i++) {if (Maxoverlapcount < A[b[i]]) {maxoverlapcount = a[b[i]];
}} return maxoverlapcount; }/** * Set the array from the begin to the end to plus one with itself. It means how much works * would be take in thisTime. * <pre> * when a[i] = 0.
It means no works at this time.
* and when a[i] = x. It means x works to take, this time. * @author Jason * @param begin * @param end * @param A */private void AddArray (int begin, int end, I
Nt[] a) {for (int i = begin; I <= end; i++) a[i]++; }
}
My idea: The time unit is actually a division of 0-1440. Each work pair should have a line segment on the axis. At each point there is a segment on the record plus 1, so that is the point of the maximum value on the axis. And that point is bound to be the endpoint of a work segment.
Time Fragment class definition:
public class Interval {private static class time {final int hour;
final int minute;
Public time (int hour, int minute) {this.hour = hour;
This.minute = minute; } @Override public boolean equals (Object obj) {if (! (
obj instanceof time)) {return false;
} Time Other = (time) obj;
return (This.hour = = Other.hour && This.minute = = Other.minute);
} @Override public int hashcode () {return toString (). Hashcode ();
} @Override Public String toString () {return String.Format ("%02d:%02d", hour, minute);
}
};
private final time begin;
Private final time end;
Public Interval (string begin, String end) {This.begin = ToTime (begin);
This.end = ToTime (end);
} private static Time ToTime (String timeformatstring) {Pattern p = pattern.compile ("(\\d?\\d):([0-5]\\d]");
Matcher m = P.matcher (timeformatstring);
if (!m.find ()) {throw new IllegalArgumentException ("Invalid Time format"); } int hour = Integer.parseint(M.group (1));
int minute = Integer.parseint (M.group (2));
return new time (hour, minute);
} public String Getbegin () {return this.begin.toString ();
} public String Getend () {return this.end.toString ();
} public int Getbeginhour () {return this.begin.hour;
} public int Getbeginminute () {return this.begin.minute;
} public int Getendhour () {return this.end.hour;
} public int Getendminute () {return this.end.minute;
} public int Getbeginminuteunit () {return Getbeginhour () * + Getbeginminute ();
} public int Getendminuteunit () {return getendhour () *60+getendminute ();
} public int Getintervalminute () {return getendminuteunit ()-getbeginminuteunit (); } @Override public boolean equals (Object obj) {if (! (
obj instanceof Interval)) {return false;
} Interval other = (Interval) obj;
Return (This.begin.equals (other.begin)) && (This.end.equals (other.end));
} @Override public int hashcode () {return toString (). Hashcode (); } @Override Public String toString () {return String.Format ("[%s-%s]", begin,end);
}
}