2013WorksApplication笔试题之我见Problem2

题目的背景紧接题目2:

意思大意是给你一对工作。让你求一个人能工作的最大时间长度。

<Specifications>

1.Return the maximum time to work on a task when one worker takes it.

2.Time assignment unit shall be base on tasks.

3.The length of interval, time required to complete the task, is calculated by subtracting "end time" from "start time"(e.g if [12:00,13:00] is given,the length of the task is 60 min.

Return 0 if the argument is null or an empty list.

4.The argument(a list of "intervals") must not contain null.

5.The number of  "intervals" must not exceed 10,000.

下面是我的解法:因为我没有找到无法从逻辑上直接获取最大工作时间,所以采用搜索方法DFS. 当然写了两个减枝,1是最大时间工作量必然大于最大工作时长的一项工作。2.

如果最晚任务的endtime-currenttime+currentworkingtime<maxWoringtime的话可以结束当前搜索。

下面为代码:

public class Problem2 {
	public static int maxWorkingTime = 0;
	public static int end_time = 0;
	public static int longestWork = 0;
	
    /**
     * Get the MaxWorkingTime by using DFS.
     * @param intervals
     * @return
     */
	public int getMaxWorkingTime(List<Interval> intervals) {

		quickSortAccordingtoStart(intervals, 0, intervals.size() - 1);

		int temp_end = 0;
		int temp_interval = 0;
		for (int i = 0; i < intervals.size(); i++) {
			Interval temp = intervals.get(i);
			if (temp.getEndMinuteUnit() > temp_end)
				temp_end = temp.getEndMinuteUnit();

			if (temp.getIntervalMinute() > temp_interval)
				temp_interval = temp.getIntervalMinute();
		}
		end_time = temp_end;
		longestWork = temp_interval;
		maxWorkingTime = longestWork;
		
		SeekMaxWorkingTime(0, 0, intervals);

		return maxWorkingTime;
	}
    /** 
     * The method is a recursive way to find maxWorkingTime. And We can optimize this process by some
     * basic conclusion. For example.the maxWorkingTime have to be bigger than the work which is the
     * longest one in term of time.Also,the maxWorkingTime also have to be smaller than the number which
     * is the latest workEndMinutesunits minus the current beginMinuteUnit of works.Start means the
     * current index which is available to add in and do not overlap with the formers.
     * @param start
     * @param currentWorkingTime
     * @param a
     */
	public static void SeekMaxWorkingTime(int start, int currentWorkingTime,
			List<Interval> a) {
		int i, j;
		if (start >= a.size()) {
			if (currentWorkingTime > maxWorkingTime)
				maxWorkingTime = currentWorkingTime;
			return;
		}
		if (currentWorkingTime + end_time - a.get(start).getBeginMinuteUnit() < maxWorkingTime)
			return;
		for (i = start; i < a.size(); i++) {
			int workingtime = a.get(i).getEndMinuteUnit()
					- a.get(i).getBeginMinuteUnit();
			int curtime = a.get(i).getEndMinuteUnit();
			for (j = start + 1; j < a.size(); j++) {
				Interval temp = a.get(j);
				if (temp.getBeginMinuteUnit() > curtime)
					break;
			}
			SeekMaxWorkingTime(j, currentWorkingTime + workingtime, a);
		}

	}
    /**
     * The method is to sort the List of Intervals based on quick by judging the beginMinuteUnit of 
     * work.
     * @param a
     * @param start
     * @param end
     */
	public static void quickSortAccordingtoStart(List<Interval> a, int start,
			int end) {
		int i, j;
		i = start;
		j = end;
		if ((a == null) || (a.size() == 0))
			return;
		while (i < j) {
			while (i < j
					&& a.get(i).getBeginMinuteUnit() <= a.get(j)
							.getBeginMinuteUnit()) {
				j--;
			}
			if (i < j) {
				Interval temp = a.get(i);
				a.set(i, a.get(j));
				a.set(j, temp);
			}
			while (i < j
					&& a.get(i).getBeginMinuteUnit() < a.get(j)
							.getBeginMinuteUnit()) {
				i++;
			}
			if (i < j) {
				Interval temp = a.get(i);
				a.set(i, a.get(j));
				a.set(j, temp);
			}
		}
		if (i - start > 1) {
			quickSortAccordingtoStart(a, start, i - 1);
		}
		if (end - j > 1) {
			quickSortAccordingtoStart(a, j + 1, end);
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值