中级农民
- 积分
- 137
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-2-2
- 最后登录
- 1970-1-1
|
- import java.util.*;
- public class ParseCPULogFile {
-
- Stack<jobInformation> stack = new Stack<jobInformation>();
- //the key is the name of the job, and the list of the interval is the list of job time
- Map<String, List<interval>> map = new HashMap<String, List<interval>>();
- String currentJobName = "";
- int startTime = 0;
- public void add(String jobName, boolean start, int timeStamp){
- if(start){
- //stack is null push into stack. cut the original job
- if(stack.isEmpty()){
- startTime = timeStamp;
- currentJobName = jobName;
- }
- else{
- if(currentJobName.equals(jobName)){
- jobInformation job = new jobInformation(jobName, start, timeStamp);
- stack.push(job);
- }
- else{
- int endTimeStamp = stack.peek().timeStamp;
- addToMap(stack.peek().jobName, startTime, endTimeStamp);
- startTime = timeStamp;
- currentJobName = jobName;
- }
- }
- jobInformation job = new jobInformation(jobName, start, timeStamp);
- stack.push(job);
- }
- else{
- jobInformation originalJob= stack.pop();
- if(!stack.isEmpty() && stack.peek().jobName.equals(jobName)){
- }
- else{
- addToMap(jobName, startTime, timeStamp);
- if(!stack.isEmpty()){
- currentJobName = stack.peek().jobName;
- startTime = stack.peek().timeStamp;
- }
- }
- }
- }
- //print out the result
-
- public void addToMap(String jobName, int start, int end){
- interval newInterval = new interval(startTime, end);
- if(!map.containsKey(jobName)){
- List<interval> list = new LinkedList<interval>();
- map.put(jobName, list);
- }
- map.get(jobName).add(newInterval);
- }
-
- public String output(){
- return "";
- //for loop the print all the jobs.
- //print out all the jobs. So I have twenty five minutes to try the tackle with the proramming question, 10 minutes to deal with the fllow up question
- }
-
- //build the class to store the interval
- public class interval{
- int start;
- int end;
- interval(int start, int end){
- this.start = start;
- this.end = end;
- }
- }
- //build the jobInformation class
- public class jobInformation{
- String jobName;
- boolean start;
- int timeStamp;
- jobInformation(String jobName, boolean start, int timeStamp){
- this.jobName = jobName;
- this.start = start;
- this.timeStamp = timeStamp;
- }
- }
- }
复制代码 |
|