Works applications 2014笔试题(1)

来源:互联网 发布:java面试造假 编辑:程序博客网 时间:2024/06/16 23:54
<Implementation>
Implement the method "getMaxIntervalOverlapCount" in the class "Problem1".
int Problem1#getMaxIntervalOverlapCount(List<Interval> intervals)
<Specifications>
•  Return 0 if the argument is null or an empty list.
•  The argument (a list of “intervals”) must not contain null.
<Examples>
In Figure 1, two “intervals” are overlapped at most.  ⇒ Return “2” for this case.

In Figure 2, three “intervals” are overlapped at most. ⇒ Return “3” for this case.






<Examples of test>
The class “Problem1”, which you have created in this problem, is to be tested as following.
@Test
public void testProblem1Usage() {
Problem1 p = new Problem1();
// example: Figure 1
Interval interval1 = new Interval("08:00", "12:00");
Interval interval2 = new Interval("06:00", "09:00");
Interval interval3 = new Interval("11:00", "13:30");
List<Interval> figure1 = Arrays.asList(interval1, interval2, interval3);
assertThat(p.getMaxIntervalOverlapCount(figure1), is(2));
// example: Figure 2
List<Interval> figure2 = Arrays.asList(new Interval("09:00", "12:30"),
new Interval("06:00", "09:30"), new Interval("12:00", "14:30"),
new Interval("10:00", "10:30"), new Interval("11:00", "13:30"));
assertThat(p.getMaxIntervalOverlapCount(figure2), is(3));
}


下面是个人解答,做的一般,反正目前是没有收到面试通知


package jp.co.wap.exam;import java.util.Iterator;import java.util.List;import jp.co.wap.exam.lib.Interval;//This class is tested in testProblems.testProblem1Usage();public class Problem1 {public int getMaxIntervalOverlapCount(List<Interval> intervals) {// TODO: Implement this method.if( intervals == null || intervals.isEmpty())return 0;for( Object interval: intervals)if( interval == null)throw new IllegalArgumentException();    Iterator<Interval> it = intervals.iterator();    int position = 0;    int count;    //Use a string array to record the overlap intervals.    String positionRelation[] = new String[intervals.size()];    for( ; it.hasNext(); ){    count = 1;    Interval curInterval = (Interval) it.next();    for( Object interval: intervals){    Interval myInterval = (Interval) interval;    if( myInterval.getBeginMinuteUnit() <= curInterval.getEndMinuteUnit() &&    myInterval.getEndMinuteUnit() >= curInterval.getBeginMinuteUnit())    if(positionRelation[position] == null)    positionRelation[position] = String.valueOf(count);    else    positionRelation[position] += String.valueOf(count);    count ++;    }    position ++;    //This can show the overlap conditions.    //System.out.println(positionRelation[position]);    }        //count the max overlap of the records    int maxOverlap = 0;    int overlapRecord;    int countContain;    for( int i = 0; i < positionRelation.length; i++){    overlapRecord = 0;    for ( int j = 0; j < positionRelation.length; j++){    //Code beneath is to validate a piece of overlap record    //is contained by another record    countContain = 0;    for ( int k = 0; k < positionRelation[i].length(); k++)      if( positionRelation[j].contains(String.valueOf(positionRelation[i].charAt(k))))       countContain ++;        if(countContain == positionRelation[i].length())        overlapRecord ++;    }      //If a record is fully contained by another,      //and the count lager than length of the record      //it means the intervals in a record are overlapped.    if ( overlapRecord >= positionRelation[i].length() &&    maxOverlap < positionRelation[i].length())    maxOverlap = positionRelation[i].length();     }    return maxOverlap;}}

lib是已经提供好了的

package jp.co.wap.exam.lib;import java.util.regex.Matcher;import java.util.regex.Pattern;/*** This class represents interval.* YOU MUST NOT MODIFY THIS CLASS. (Use this as it is)* You do not have to submit this class. (Interval class is to be provided in our scoring system.)*/public class Interval {/*** It represents Time of Hour and Minute.*/private static class Time {final int hour;final int minute;public Time(int hour, int minute) {this.hour = hour;this.minute = minute;}@Overridepublic String toString() {return String.format("%02d:%02d", hour, minute);}@Overridepublic int hashCode() {return toString().hashCode();}@Overridepublic boolean equals(Object obj) {if (!(obj instanceof Time)) {return false;}Time other = (Time) obj;return (this.hour == other.hour && this.minute == other.minute);}}/** Initial point of interval. */private final Time begin;/** Terminal point of interval. */private final Time end;/** Create interval from begin time (string) and end time (string). */public Interval(String begin, String end) {this.begin = toTime(begin);this.end = toTime(end);}/** Convert time format (string) to Time structure. */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);}/** Get interval begin string.*/public String getBegin() {return this.begin.toString();}/** Get interval end string. */public String getEnd() {return this.end.toString();}/** Get interval begin hour. */public int getBeginHour() {return this.begin.hour;}/** Get interval begin minute. */public int getBeginMinute() {return this.begin.minute;}/** Get interval end hour. */public int getEndHour() {return this.end.hour;}/** Get interval end minute. */public int getEndMinute() {return this.end.minute;}/*** Get total time (minute) from "00:00" to initial point of interval.* For example, it returns 150 when initial point of interval is "02:30".**/public int getBeginMinuteUnit() {return getBeginHour() * 60 + getBeginMinute();}/*** Get total time (minute) from "00:00" to terminal point of interval.* For example, it returns 1440 when terminal point of interval is "24:00".**/public int getEndMinuteUnit() {return getEndHour() * 60 + getEndMinute();}/*** Get total time on interval.* That is, it returns getEndMinuteUnit() minus getBeginMinuteUnit().**/public int getIntervalMinute() {return getEndMinuteUnit() - getBeginMinuteUnit();}@Overridepublic int hashCode() {return toString().hashCode();}@Overridepublic 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));}@Overridepublic String toString() {return String.format("[%s-%s]", begin, end);}}






0 0
原创粉丝点击