Skip to content

Commit d43bdac

Browse files
committed
leetcode
1 parent a946ddb commit d43bdac

File tree

4 files changed

+224
-0
lines changed

4 files changed

+224
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
3+
-* 1396. Design Underground System *-
4+
5+
An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another.
6+
7+
Implement the UndergroundSystem class:
8+
9+
void checkIn(int id, string stationName, int t)
10+
A customer with a card ID equal to id, checks in at the station stationName at time t.
11+
A customer can only be checked into one place at a time.
12+
void checkOut(int id, string stationName, int t)
13+
A customer with a card ID equal to id, checks out from the station stationName at time t.
14+
double getAverageTime(string startStation, string endStation)
15+
Returns the average time it takes to travel from startStation to endStation.
16+
The average time is computed from all the previous traveling times from startStation to endStation that happened directly, meaning a check in at startStation followed by a check out from endStation.
17+
The time it takes to travel from startStation to endStation may be different from the time it takes to travel from endStation to startStation.
18+
There will be at least one customer that has traveled from startStation to endStation before getAverageTime is called.
19+
You may assume all calls to the checkIn and checkOut methods are consistent. If a customer checks in at time t1 then checks out at time t2, then t1 < t2. All events happen in chronological order.
20+
21+
22+
23+
Example 1:
24+
25+
Input
26+
["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"]
27+
[[],[45,"Leyton",3],[32,"Paradise",8],[27,"Leyton",10],[45,"Waterloo",15],[27,"Waterloo",20],[32,"Cambridge",22],["Paradise","Cambridge"],["Leyton","Waterloo"],[10,"Leyton",24],["Leyton","Waterloo"],[10,"Waterloo",38],["Leyton","Waterloo"]]
28+
29+
Output
30+
[null,null,null,null,null,null,null,14.00000,11.00000,null,11.00000,null,12.00000]
31+
32+
Explanation
33+
UndergroundSystem undergroundSystem = new UndergroundSystem();
34+
undergroundSystem.checkIn(45, "Leyton", 3);
35+
undergroundSystem.checkIn(32, "Paradise", 8);
36+
undergroundSystem.checkIn(27, "Leyton", 10);
37+
undergroundSystem.checkOut(45, "Waterloo", 15); // Customer 45 "Leyton" -> "Waterloo" in 15-3 = 12
38+
undergroundSystem.checkOut(27, "Waterloo", 20); // Customer 27 "Leyton" -> "Waterloo" in 20-10 = 10
39+
undergroundSystem.checkOut(32, "Cambridge", 22); // Customer 32 "Paradise" -> "Cambridge" in 22-8 = 14
40+
undergroundSystem.getAverageTime("Paradise", "Cambridge"); // return 14.00000. One trip "Paradise" -> "Cambridge", (14) / 1 = 14
41+
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 11.00000. Two trips "Leyton" -> "Waterloo", (10 + 12) / 2 = 11
42+
undergroundSystem.checkIn(10, "Leyton", 24);
43+
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 11.00000
44+
undergroundSystem.checkOut(10, "Waterloo", 38); // Customer 10 "Leyton" -> "Waterloo" in 38-24 = 14
45+
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 12.00000. Three trips "Leyton" -> "Waterloo", (10 + 12 + 14) / 3 = 12
46+
Example 2:
47+
48+
Input
49+
["UndergroundSystem","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime"]
50+
[[],[10,"Leyton",3],[10,"Paradise",8],["Leyton","Paradise"],[5,"Leyton",10],[5,"Paradise",16],["Leyton","Paradise"],[2,"Leyton",21],[2,"Paradise",30],["Leyton","Paradise"]]
51+
52+
Output
53+
[null,null,null,5.00000,null,null,5.50000,null,null,6.66667]
54+
55+
Explanation
56+
UndergroundSystem undergroundSystem = new UndergroundSystem();
57+
undergroundSystem.checkIn(10, "Leyton", 3);
58+
undergroundSystem.checkOut(10, "Paradise", 8); // Customer 10 "Leyton" -> "Paradise" in 8-3 = 5
59+
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.00000, (5) / 1 = 5
60+
undergroundSystem.checkIn(5, "Leyton", 10);
61+
undergroundSystem.checkOut(5, "Paradise", 16); // Customer 5 "Leyton" -> "Paradise" in 16-10 = 6
62+
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.50000, (5 + 6) / 2 = 5.5
63+
undergroundSystem.checkIn(2, "Leyton", 21);
64+
undergroundSystem.checkOut(2, "Paradise", 30); // Customer 2 "Leyton" -> "Paradise" in 30-21 = 9
65+
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 6.66667, (5 + 6 + 9) / 3 = 6.66667
66+
67+
68+
Constraints:
69+
70+
1 <= id, t <= 106
71+
1 <= stationName.length, startStation.length, endStation.length <= 10
72+
All strings consist of uppercase and lowercase English letters and digits.
73+
There will be at most 2 * 104 calls in total to checkIn, checkOut, and getAverageTime.
74+
Answers within 10-5 of the actual value will be accepted.
75+
76+
*/
77+
78+
import 'dart:collection';
79+
80+
class UndergroundSystem {
81+
int stationCount = 0;
82+
HashMap<String, int> stationId = HashMap();
83+
HashMap<int, List<int>> checkIns = HashMap();
84+
HashMap<int, List<int>> timings = HashMap();
85+
86+
UndergroundSystem() {
87+
stationCount = 0;
88+
stationId.clear();
89+
checkIns.clear();
90+
timings.clear();
91+
}
92+
93+
void checkIn(int id, String stationName, int t) {
94+
if (!stationId.containsKey(stationName)) {
95+
stationId[stationName] = stationCount;
96+
stationCount++;
97+
}
98+
checkIns[id] = [stationId[stationName] ?? 0, t];
99+
}
100+
101+
void checkOut(int id, String stationName, int t) {
102+
if (!stationId.containsKey(stationName)) {
103+
stationId[stationName] = stationCount;
104+
stationCount++;
105+
}
106+
if (checkIns.containsKey(id)) {
107+
final startDetail = checkIns[id]!;
108+
checkIns.remove(id);
109+
final routeHash = startDetail[0] * 10000 + stationId[stationName]!;
110+
final travelTime = t - startDetail[1];
111+
if (travelTime <= 0) return;
112+
timings.putIfAbsent(routeHash, () => [0, 0]);
113+
timings[routeHash]![0] += travelTime;
114+
timings[routeHash]![1]++;
115+
}
116+
}
117+
118+
double getAverageTime(String startStation, String endStation) {
119+
final routeHash = stationId[startStation]! * 10000 + stationId[endStation]!;
120+
final startDetail = timings[routeHash]!;
121+
return startDetail[0] / startDetail[1];
122+
}
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type UndergroundSystem struct {
8+
stationCount int
9+
stationId map[string]int
10+
checkIns map[int][2]int
11+
timings map[int][2]int
12+
}
13+
14+
func Constructor() UndergroundSystem {
15+
return UndergroundSystem{
16+
stationCount: 0,
17+
stationId: make(map[string]int),
18+
checkIns: make(map[int][2]int),
19+
timings: make(map[int][2]int),
20+
}
21+
}
22+
23+
func (us *UndergroundSystem) CheckIn(id int, stationName string, t int) {
24+
if _, ok := us.stationId[stationName]; !ok {
25+
us.stationId[stationName] = us.stationCount
26+
us.stationCount++
27+
}
28+
us.checkIns[id] = [2]int{us.stationId[stationName], t}
29+
}
30+
31+
func (us *UndergroundSystem) CheckOut(id int, stationName string, t int) {
32+
if _, ok := us.stationId[stationName]; !ok {
33+
us.stationId[stationName] = us.stationCount
34+
us.stationCount++
35+
}
36+
if startDetail, ok := us.checkIns[id]; ok {
37+
delete(us.checkIns, id)
38+
routeHash := startDetail[0]*10000 + us.stationId[stationName]
39+
travelTime := t - startDetail[1]
40+
if travelTime <= 0 {
41+
return
42+
}
43+
if timing, ok := us.timings[routeHash]; ok {
44+
timing[0] += travelTime
45+
timing[1]++
46+
us.timings[routeHash] = timing
47+
} else {
48+
us.timings[routeHash] = [2]int{travelTime, 1}
49+
}
50+
}
51+
}
52+
53+
func (us *UndergroundSystem) GetAverageTime(startStation string, endStation string) float64 {
54+
routeHash := us.stationId[startStation]*10000 + us.stationId[endStation]
55+
startDetail := us.timings[routeHash]
56+
return float64(startDetail[0]) / float64(startDetail[1])
57+
}
58+
59+
60+
61+
/*
62+
63+
64+
type UndergroundSystem struct {
65+
66+
}
67+
68+
69+
func Constructor() UndergroundSystem {
70+
71+
}
72+
73+
74+
func (this *UndergroundSystem) CheckIn(id int, stationName string, t int) {
75+
76+
}
77+
78+
79+
func (this *UndergroundSystem) CheckOut(id int, stationName string, t int) {
80+
81+
}
82+
83+
84+
func (this *UndergroundSystem) GetAverageTime(startStation string, endStation string) float64 {
85+
86+
}
87+
88+
*/
89+
90+
/*
91+
* Your UndergroundSystem object will be instantiated and called as such:
92+
* obj := Constructor();
93+
* obj.CheckIn(id,stationName,t);
94+
* obj.CheckOut(id,stationName,t);
95+
* param_3 := obj.GetAverageTime(startStation,endStation);
96+
*/
97+
98+
99+

DesignUndergroundSystem/design_underground_system.md

Whitespace-only changes.

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ This repo contain leetcode solution using DART and GO programming language. Most
215215
- [**1472.** Design Browser History](DesignBrowserHistory/design_browser_history.dart)
216216
- [**347.** Top K Frequent Elements](TopKFrequentElements/top_k_frequent_elements.dart)
217217
- [**837.** New 21 Game](New21Game/new_21_game.dart)
218+
- [**705.**. Design HashSet](DesignHashSet/design_hashSet.dart)
219+
- [**1396.** Design Underground System](DesignUndergroundSystem/design_underground_system.dart)
218220

219221
## Reach me via
220222

0 commit comments

Comments
 (0)