Skip to content

Commit 6845f78

Browse files
add 1396
1 parent 7815a8f commit 6845f78

File tree

7 files changed

+145
-4
lines changed

7 files changed

+145
-4
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -693,4 +693,5 @@ LeetCode
693693
|1391|[Check if There is a Valid Path in a Grid](https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid/)|c|[c++](./src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.cpp)|[python](./src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.py)|[go](./src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.go)|[js](./src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.js)|[java](./src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.java)|Medium|
694694
|1392|[Longest Happy Prefix](https://leetcode.com/problems/longest-happy-prefix/)|c|[c++](./src/1392-Longest-Happy-Prefix/1392.cpp)|[python](./src/1392-Longest-Happy-Prefix/1392.py)|[go](./src/1392-Longest-Happy-Prefix/1392.go)|[js](./src/1392-Longest-Happy-Prefix/1392.js)|[java](./src/1392-Longest-Happy-Prefix/1392.java)|Hard|
695695
|1394|[Find Lucky Integer in an Array](https://leetcode.com/problems/find-lucky-integer-in-an-array/)|c|[c++](./src/1394-Find-Lucky-Integer-in-an-Array/1394.cpp)|[python](./src/1394-Find-Lucky-Integer-in-an-Array/1394.py)|[go](./src/1394-Find-Lucky-Integer-in-an-Array/1394.go)|[js](./src/1394-Find-Lucky-Integer-in-an-Array/1394.js)|[java](./src/1394-Find-Lucky-Integer-in-an-Array/1394.java)|Easy|
696-
|1395|[Count Number of Teams](https://leetcode.com/problems/count-number-of-teams/)|c|[c++](./src/1395-Count-Number-of-Teams/1395.cpp)|[python](./src/1395-Count-Number-of-Teams/1395.py)|[go](./src/1395-Count-Number-of-Teams/1395.go)|[js](./src/1395-Count-Number-of-Teams/1395.js)|[java](./src/1395-Count-Number-of-Teams/1395.java)|Medium|
696+
|1395|[Count Number of Teams](https://leetcode.com/problems/count-number-of-teams/)|c|[c++](./src/1395-Count-Number-of-Teams/1395.cpp)|[python](./src/1395-Count-Number-of-Teams/1395.py)|[go](./src/1395-Count-Number-of-Teams/1395.go)|[js](./src/1395-Count-Number-of-Teams/1395.js)|[java](./src/1395-Count-Number-of-Teams/1395.java)|Medium|
697+
|1396|[Design Underground System](https://leetcode.com/problems/design-underground-system/)|c|[c++](./src/1396-Design-Underground-System/1396.cpp)|[python](./src/1396-Design-Underground-System/1396.py)|[go](./src/1396-Design-Underground-System/1396.go)|[js](./src/1396-Design-Underground-System/1396.js)|[java](./src/1396-Design-Underground-System/1396.java)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class UndergroundSystem {
2+
public:
3+
void checkIn(int id, string startStation, int timeIn) {
4+
check_in[id] = {startStation, timeIn};
5+
}
6+
7+
void checkOut(int id, string endStation, int timeOut) {
8+
const auto &[startStation, timeIn] = check_in[id];
9+
auto &[avg, cnt] = check_out[startStation + ">" + endStation];
10+
avg += timeOut - timeIn;
11+
++cnt;
12+
}
13+
14+
double getAverageTime(string startStation, string endStation) {
15+
auto [avg, cnt] = check_out[startStation + ">" + endStation];
16+
return (double)avg / cnt;
17+
}
18+
private:
19+
unordered_map<int, pair<string, int>> check_in;
20+
unordered_map<string, pair<int, int>> check_out;
21+
};
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
type UndergroundSystem struct {
2+
check_in map[int]in
3+
check_out map[string][2]int
4+
}
5+
6+
type in struct {
7+
station string
8+
time int
9+
}
10+
11+
func Constructor() UndergroundSystem {
12+
return UndergroundSystem{check_in: make(map[int]in), check_out: make(map[string][2]int)}
13+
}
14+
15+
16+
func (this *UndergroundSystem) CheckIn(id int, stationName string, t int) {
17+
this.check_in[id] = in{station: stationName, time: t}
18+
}
19+
20+
21+
func (this *UndergroundSystem) CheckOut(id int, stationName string, t int) {
22+
start := this.check_in[id]
23+
name := start.station + ">" + stationName
24+
cur := this.check_out[name]
25+
this.check_out[name] = [2]int{cur[0] + t - start.time, cur[1] + 1}
26+
}
27+
28+
29+
func (this *UndergroundSystem) GetAverageTime(startStation string, endStation string) float64 {
30+
name := startStation + ">" + endStation
31+
return float64(this.check_out[name][0]) / float64(this.check_out[name][1])
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class UndergroundSystem {
2+
3+
HashMap<String, Pair<Integer, Integer>> check_out = new HashMap();
4+
HashMap<Integer, Pair<String, Integer>> check_in = new HashMap();
5+
6+
public UndergroundSystem() {}
7+
8+
public void checkIn(int id, String stationName, int t) {
9+
check_in.put(id, new Pair(stationName, t));
10+
}
11+
12+
public void checkOut(int id, String stationName, int t) {
13+
Pair<String, Integer> checkIn = check_in.get(id);
14+
String name = checkIn.getKey() + ">" + stationName;
15+
int totalTime = t - checkIn.getValue();
16+
Pair<Integer, Integer> checkout = check_out.getOrDefault(name, new Pair(0, 0));
17+
check_out.put(name, new Pair<>(checkout.getKey() + totalTime, checkout.getValue() + 1));
18+
}
19+
20+
public double getAverageTime(String startStation, String endStation) {
21+
String name = startStation + ">" + endStation;
22+
Pair<Integer, Integer> checkout = check_out.get(name);
23+
return (double) checkout.getKey() / checkout.getValue();
24+
}
25+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
var UndergroundSystem = function() {
3+
this.check_in = new Map();
4+
this.check_out = new Map();
5+
};
6+
7+
/**
8+
* @param {number} id
9+
* @param {string} stationName
10+
* @param {number} t
11+
* @return {void}
12+
*/
13+
UndergroundSystem.prototype.checkIn = function(id, stationName, t) {
14+
this.check_in.set(id, [stationName, t]);
15+
};
16+
17+
/**
18+
* @param {number} id
19+
* @param {string} stationName
20+
* @param {number} t
21+
* @return {void}
22+
*/
23+
UndergroundSystem.prototype.checkOut = function(id, stationName, t) {
24+
let [startName, timeIn] = this.check_in.get(id);
25+
let name = startName + ">" + stationName;
26+
if (!this.check_out.has(name)) this.check_out.set(name, [0, 0]);
27+
let [avg, cnt] = this.check_out.get(name);
28+
this.check_out.set(name, [avg + t - timeIn, cnt + 1]);
29+
};
30+
31+
/**
32+
* @param {string} startStation
33+
* @param {string} endStation
34+
* @return {number}
35+
*/
36+
UndergroundSystem.prototype.getAverageTime = function(startStation, endStation) {
37+
let name = startStation + ">" + endStation;
38+
return this.check_out.get(name)[0] / this.check_out.get(name)[1];
39+
};
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class UndergroundSystem:
2+
3+
def __init__(self):
4+
self.check_in = collections.defaultdict(list)
5+
self.check_out = collections.defaultdict(list)
6+
7+
8+
def checkIn(self, id: int, stationName: str, t: int) -> None:
9+
self.check_in[id] = [stationName, t]
10+
11+
12+
def checkOut(self, id: int, stationName: str, t: int) -> None:
13+
startName, timeIn = self.check_in[id]
14+
name = startName + ">" + stationName
15+
if not self.check_out[name]:
16+
self.check_out[name] = [0, 0]
17+
self.check_out[name][0] += t - timeIn
18+
self.check_out[name][1] += 1
19+
20+
21+
def getAverageTime(self, startStation: str, endStation: str) -> float:
22+
name = startStation + ">" + endStation
23+
return self.check_out[name][0] / self.check_out[name][1]

src/addProb.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import os, bisect
33

44
# 题目名称
5-
name = "Count Number of Teams"
6-
ID = 1395
7-
url = "https://leetcode.com/problems/count-number-of-teams/"
5+
name = "Design Underground System"
6+
ID = 1396
7+
url = "https://leetcode.com/problems/design-underground-system/"
88
difficult = "Medium"
99
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']
1010

0 commit comments

Comments
 (0)