java Solution using Maps and lists, beats 100% of Java submissions


  • 0
    V

    Well, the solution is pretty intuitive. Please let me know if we can improve the inRange function or any other part.

    class TimeStamp{
    	int year,month,day;
    	int hours,minutes,seconds;
    
    	TimeStamp(String timestamp){
    		String s[] = timestamp.split(":");
    		year = Integer.parseInt(s[0]);
    		month = Integer.parseInt(s[1]);
    		day = Integer.parseInt(s[2]);
    		hours = Integer.parseInt(s[3]);
    		minutes = Integer.parseInt(s[4]);
    		seconds = Integer.parseInt(s[5]);
    	}
    
    	public int compare(int x,int y){
    		if(x > y) return 1;
    		if(x < y) return -1;
    		return 0;
    	}
    
    	public boolean inRange(TimeStamp s,TimeStamp e,int LIMIT){
    		int[] st = new int[]{0,0,0,0,0,0};
    		int[] en = new int[]{0,0,0,0,0,0};
    
    		st[0] = compare(this.year,s.year);
    		en[0] = compare(this.year,e.year);
    
    		st[1] = compare(this.month,s.month);
    		en[1] = compare(this.month,e.month);
    
    		st[2] = compare(this.day,s.day);
    		en[2] = compare(this.day,e.day);
    
    		st[3] = compare(this.hours,s.hours);
    		en[3] = compare(this.hours,e.hours);
    
    		st[4] = compare(this.minutes,s.minutes);
    		en[4] = compare(this.minutes,e.minutes);
    
    		st[5] = compare(this.seconds,s.seconds);
    		en[5] = compare(this.seconds,e.seconds);
    		
    
    		for(int i=0;i<=LIMIT;++i){
    		    if(st[i] == 1) break;
    			if(st[i] == -1) return false;
    		}
    
    		for(int i=0;i<=LIMIT;++i){
    		    if(en[i] == -1) break;
    			if(en[i] == 1) return false;
    		}
    
    		return true;
    	}
    }
    
    public class LogSystem {
    	Map<TimeStamp,Integer> map = new HashMap<TimeStamp,Integer>();
    	Map<String,Integer> gra_map = new HashMap<String,Integer>();
    	Map<String,TimeStamp> t = new HashMap<String,TimeStamp>();
    	List<String> timestamps = new ArrayList<String>();
        public LogSystem() {
            gra_map.put("Year",0);
            gra_map.put("Month",1);
            gra_map.put("Day",2);
            gra_map.put("Hour",3);
            gra_map.put("Minute",4);
            gra_map.put("Second",5);
        }
        
        public void put(int id, String timestamp) {
            TimeStamp newt = new TimeStamp(timestamp);
            map.put(newt,id);
            t.put(timestamp,newt);
            timestamps.add(timestamp);
        }
        
        public List<Integer> retrieve(String s, String e, String gra) {
        	List<Integer> IDs = new ArrayList<Integer>();
            int len = timestamps.size();
            TimeStamp start = new TimeStamp(s);
            TimeStamp end = new TimeStamp(e);
    
            for(int i=0;i<len;++i){
            	TimeStamp curr = t.get(timestamps.get(i));
            	if(curr.inRange(start,end,gra_map.get(gra))){
            		IDs.add(map.get(curr));
            	}
            }
    
            return IDs;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.