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

Commit68f9343

Browse files
authored
Merge pull requestneetcode-gh#2582 from aadil42/patch-54
2 parents185d63a +c9690d2 commit68f9343

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// https://leetcode.com/problems/design-underground-system/
2+
classUndergroundSystem{
3+
constructor(){
4+
this.stationSystem={};
5+
this.averageTime={};
6+
}
7+
8+
/**
9+
* Time O(1) | Space O(1)
10+
* Records the check-in time and station for a user.
11+
*@param {number} id - User ID
12+
*@param {string} stationName - Check-in station name
13+
*@param {number} t - Check-in time
14+
*@return {void}
15+
*/
16+
checkIn(id,stationName,t){
17+
this.stationSystem[id]=[stationName,'',t,''];
18+
}
19+
20+
/**
21+
* Time O(1) | Space O(1)
22+
* Records the check-out time and station for a user, and calculates the average time.
23+
*@param {number} id - User ID
24+
*@param {string} stationName - Check-out station name
25+
*@param {number} t - Check-out time
26+
*@return {void}
27+
*/
28+
checkOut(id,stationName,t){
29+
constuser=this.stationSystem[id];
30+
user[1]=stationName;
31+
user[3]=t;
32+
conststationHash=`${user[0]}-${user[1]}`;
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+
/**
44+
* Time O(1) | Space O(1)
45+
* Returns the average time taken to travel between two stations.
46+
*@param {string} startStation - Start station name
47+
*@param {string} endStation - End station name
48+
*@return {number} - Average time in hours
49+
*/
50+
getAverageTime(startStation,endStation){
51+
const[rounds,totalHours]=this.averageTime[`${startStation}-${endStation}`];
52+
returntotalHours/rounds;
53+
}
54+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp