Skip to content

Commit

Permalink
Create 1396_Design_Underground_System.java
Browse files Browse the repository at this point in the history
  • Loading branch information
sumeetskd committed May 31, 2023
1 parent d77e70f commit 60dab17
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions May/Day 31/1396_Design_Underground_System.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class UndergroundSystem {
private Map<Integer, Pair<String, Integer>> checkIns = new HashMap<>();
private Map<Pair<String, String>, int[]> times = new HashMap<>();

public UndergroundSystem() {

}

public void checkIn(int id, String stationName, int t) {
checkIns.put(id, new Pair(stationName, t));
}

public void checkOut(int id, String stationName, int t) {
var startStation = checkIns.get(id).getKey();
var startTime = checkIns.get(id).getValue();
checkIns.remove(id);

var pair = new Pair(startStation, stationName);
var totalTime = times.containsKey(pair) ? times.get(pair)[0] : 0;
var dataPoints = times.containsKey(pair) ? times.get(pair)[1] : 0;

times.put(pair, new int[]{totalTime + t - startTime, dataPoints + 1});
}

public double getAverageTime(String startStation, String endStation) {
var pair = new Pair(startStation, endStation);
return (double) times.get(pair)[0] / times.get(pair)[1];
}
}

/**
* Your UndergroundSystem object will be instantiated and called as such:
* UndergroundSystem obj = new UndergroundSystem();
* obj.checkIn(id,stationName,t);
* obj.checkOut(id,stationName,t);
* double param_3 = obj.getAverageTime(startStation,endStation);
*/

0 comments on commit 60dab17

Please sign in to comment.