Works applications 2014笔试题(1)
来源:互联网 发布:java面试造假 编辑:程序博客网 时间:2024/06/16 23:54
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);}}
- Works applications 2014笔试题(1)
- Works applications 笔试题(2)
- Works applications 笔试题(3)
- Works Applications的笔试题
- Works Applications 面试笔试!
- 笔试——全排序算法(字典序法)——记Works Applications面试题
- Works Application 2017 笔试题
- Works Applications技术面试题
- Works Applications技术面试题
- Works Applications 面试题 2012
- Works System的笔试
- Works Application 2017 笔试题 [Exam1] Magic Cube
- Works Application 2017 笔试 [Exam2] Wireless Routers
- Maigc Cubes:2017 Works Application笔试
- works的题
- How Tomcat Works 1
- how tomcat works - 1
- How Tomcat Works 1
- 算法问题-八皇后
- 数据库--SQL--有就更新,没有就插入
- linux shell编程入门
- Linux驱动之设备模型(2)
- 韩睿(Henry Han)的VB.NET之旅
- Works applications 2014笔试题(1)
- C++中,函数名前的const和函数名后的const的区别
- 【ORACLE RAC】修改归档位置: FRA和其他位置
- 【二叉树是否对称】Symmetric Tree
- hdu4396 多状态spfa
- Construct Binary Tree from Preorder and Inorder Traversal
- ubuntu apache 服务器 文件下载
- MSTP域
- BAT 批处理脚本 教程