题目1:
意思为一天从00:00到24.00(24.00为第二天的0.00)。我们不能在同一时间工作任务超过2件,意思就是我们在一时间只能做一项工作。 而每一项工作是一个时间段。比如工作1是
[08:00]到[12:00],工作2是[06:00]到[09:00].现在给你一个天的工作安排。求最大的同时工作数。比如工作1和工作2就是在8点到9点之间是同时工作。注:[08:00,09:00]与[09.00,10:00]两个工作也是overlapped because include "09:00"
下面是我的解法
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
* will be take in this time.
* <pre>
* when a[i] = 0. it means no works at this time.
* and when a[i] = x. it means x works will take in this time.
* @author jason
* @param begin
* @param end
* @param a
*/
private void addArray(int begin, int end, int[] a) {
for (int i = begin; i <= end; i++)
a[i]++;
}
}
我的想法:时间Unit其实就是0-1440的一个分部。每一个工作对应该数轴上一条线段。每一个点上存在一条线段就记录加1,这样即是求该数轴上最大值的点。并且该点必然为某个工作的线段端点。
时间片段类定义:
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() * 60 + 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);
}
}