-
Notifications
You must be signed in to change notification settings - Fork 0
/
UndergroundSystem.js
114 lines (103 loc) · 2.67 KB
/
UndergroundSystem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//From 1396 in LeetCode
class UndergroundSystem {
constructor() {
this.riders = {};
this.times = {};
}
}
/**
* @param {number} id
* @param {string} stationName
* @param {number} t
* @return {void}
*/
UndergroundSystem.prototype.checkIn = function(id, stationName, t) {
this.riders[id] = [stationName, t];
};
/**
* @param {number} id
* @param {string} stationName
* @param {number} t
* @return {void}
*/
UndergroundSystem.prototype.checkOut = function(id, stationName, t) {
let checkIn = this.riders[id];
let startStation = checkIn[0];
let startTime = checkIn[1];
if (this.times[`${startStation}${stationName}`]) {
this.times[`${startStation}${stationName}`].push(t - startTime);
} else {
this.times[`${startStation}${stationName}`] = [t - startTime];
}
};
/**
* @param {string} startStation
* @param {string} endStation
* @return {number}
*/
UndergroundSystem.prototype.getAverageTime = function(
startStation,
endStation,
) {
let times = this.times[`${startStation}${endStation}`];
return times.reduce((acc, curr) => acc + curr) / times.length;
};
/**
* Your UndergroundSystem object will be instantiated and called as such:
* var obj = new UndergroundSystem()
* obj.checkIn(id,stationName,t)
* obj.checkOut(id,stationName,t)
* var param_3 = obj.getAverageTime(startStation,endStation)
*/
//less memory
class UndergroundSystemII {
constructor() {
this.riders = {};
this.times = {};
}
}
/**
* @param {number} id
* @param {string} stationName
* @param {number} t
* @return {void}
*/
UndergroundSystem.prototype.checkIn = function(id, stationName, t) {
this.riders[id] = [stationName, t];
};
/**
* @param {number} id
* @param {string} stationName
* @param {number} t
* @return {void}
*/
UndergroundSystem.prototype.checkOut = function(id, stationName, t) {
let checkIn = this.riders[id];
let startStation = checkIn[0];
let startTime = checkIn[1];
if (this.times[`${startStation}${stationName}`]) {
this.times[`${startStation}${stationName}`][0] += t - startTime;
this.times[`${startStation}${stationName}`][1]++;
} else {
this.times[`${startStation}${stationName}`] = [t - startTime, 1];
}
};
/**
* @param {string} startStation
* @param {string} endStation
* @return {number}
*/
UndergroundSystem.prototype.getAverageTime = function(
startStation,
endStation,
) {
let times = this.times[`${startStation}${endStation}`];
return times[0] / times[1];
};
/**
* Your UndergroundSystem object will be instantiated and called as such:
* var obj = new UndergroundSystem()
* obj.checkIn(id,stationName,t)
* obj.checkOut(id,stationName,t)
* var param_3 = obj.getAverageTime(startStation,endStation)
*/