2013WorksApplication笔试题之我见Problem1

本文介绍了一种用于计算在给定时间范围内最多可能同时进行的任务数量的算法。通过将一天的时间划分为1440分钟,并使用线段覆盖的方法来确定最多重叠任务的数量。这种方法能够有效地解决任务调度中关于时间重叠的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目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);
	}
	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值