Skip to content

Commit 86355ec

Browse files
committed
[Array] Add a solution to Exam Room
1 parent 2e38095 commit 86355ec

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

Array/ExamRoom.swift

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/exam-room/
3+
* Primary idea: Calculate and compare middle point between two taken seats.
4+
*
5+
* Time Complexity: O(n) for seat(), O(1) for leave(at:), Space Complexity: O(n)
6+
*
7+
*/
8+
9+
class ExamRoom {
10+
var seats: [Int]
11+
12+
init(_ n: Int) {
13+
seats = Array(repeating: 0, count: n)
14+
}
15+
16+
func seat() -> Int {
17+
var maxDistance = 0, maxIndex = 0, lastOne = -1
18+
19+
for (i, seat) in seats.enumerated() {
20+
if seat == 1 {
21+
if lastOne == -1 {
22+
if maxDistance < i {
23+
maxDistance = i
24+
maxIndex = 0
25+
}
26+
} else {
27+
if maxDistance < (i - lastOne) / 2 {
28+
maxDistance = (i - lastOne) / 2
29+
maxIndex = lastOne + (i - lastOne) / 2
30+
}
31+
}
32+
}
33+
34+
lastOne = i
35+
}
36+
37+
if lastOne != -1 {
38+
if maxDistance < (seats.count - 1 - lastOne) / 2 {
39+
maxIndex = seats.count - 1
40+
}
41+
}
42+
43+
seats[maxIndex] = 1
44+
45+
return maxIndex
46+
}
47+
48+
func leave(_ seat: Int) {
49+
seats[seat] = 0
50+
}
51+
}

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* [Microsoft](#microsoft)
2929

3030
## Progress
31-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 260 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
31+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 261 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3232

3333

3434
## Array
@@ -56,6 +56,7 @@
5656
[Summary Ranges](https://leetcode.com/problems/summary-ranges/)| [Swift](./Array/SummaryRanges.swift)| Medium| O(n)| O(n)|
5757
[Asteroid Collision](https://leetcode.com/problems/asteroid-collision/)| [Swift](./Array/AsteroidCollision.swift)| Medium| O(n)| O(n)|
5858
[Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person/)| [Swift](./Array/MaximizeDistanceToClosestPerson.swift)| Easy| O(n)| O(1)|
59+
[Exam Room](https://leetcode.com/problems/exam-room/)| [Swift](./Array/ExamRoom.swift)| Medium| O(n)| O(n)|
5960
[Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/)| [Swift](./Array/ShortestWordDistance.swift)| Easy| O(n)| O(1)|
6061
[Shortest Word Distance III](https://leetcode.com/problems/shortest-word-distance-iii/)| [Swift](./Array/ShortestWordDistanceIII.swift)| Medium| O(n)| O(1)|
6162
[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)| [Swift](./Array/MinimumSizeSubarraySum.swift)| Medium| O(n)| O(1)|

0 commit comments

Comments
 (0)