-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 1396_Design_Underground_System.java
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
*/ |