Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitba67b77

Browse files
authored
Create 1396-design-underground-system.js
Adding solution for design-underground-system in JS.
1 parent9d668c6 commitba67b77

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
varUndergroundSystem=function(){
2+
this.stationSystem={};
3+
this.averageTime={};
4+
};
5+
6+
/**
7+
* https://leetcode.com/problems/design-underground-system
8+
* Time O(1) | Space O(1)
9+
*@param {number} id
10+
*@param {string} stationName
11+
*@param {number} t
12+
*@return {void}
13+
*/
14+
UndergroundSystem.prototype.checkIn=function(id,stationName,t){
15+
this.stationSystem[id]=[stationName,'',t,''];
16+
};
17+
18+
/** Time O(1) | Space O(1)
19+
*@param {number} id
20+
*@param {string} stationName
21+
*@param {number} t
22+
*@return {void}
23+
*/
24+
UndergroundSystem.prototype.checkOut=function(id,stationName,t){
25+
constuser=this.stationSystem[id];
26+
//1 is the position where we store end station
27+
// 3 is the position where we store end time
28+
user[1]=stationName;
29+
user[3]=t;
30+
console.log(user);
31+
conststationHash=`${user[0]}-${user[1]}`;
32+
// console.log(stationHash, this.stationHash);
33+
if(this.averageTime[stationHash]){
34+
this.averageTime[stationHash][0]+=1;
35+
this.averageTime[stationHash][1]+=user[3]-user[2];
36+
}else{
37+
this.averageTime[stationHash]=[];
38+
this.averageTime[stationHash][0]=1;
39+
this.averageTime[stationHash][1]=user[3]-user[2];
40+
}
41+
};
42+
43+
/** Time O(1) | Space O(1)
44+
*@param {string} startStation
45+
*@param {string} endStation
46+
*@return {number}
47+
*/
48+
UndergroundSystem.prototype.getAverageTime=function(startStation,endStation){
49+
// console.log(this.averageTime);
50+
const[rounds,totalHours]=this.averageTime[`${startStation}-${endStation}`];
51+
returntotalHours/rounds;
52+
};
53+
54+
/**
55+
* Your UndergroundSystem object will be instantiated and called as such:
56+
* var obj = new UndergroundSystem()
57+
* obj.checkIn(id,stationName,t)
58+
* obj.checkOut(id,stationName,t)
59+
* var param_3 = obj.getAverageTime(startStation,endStation)
60+
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp