Skip to content

Commit

Permalink
[Array] Add a solution to Exam Room
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Jul 3, 2018
1 parent 2e38095 commit 86355ec
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
51 changes: 51 additions & 0 deletions Array/ExamRoom.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Question Link: https://leetcode.com/problems/exam-room/
* Primary idea: Calculate and compare middle point between two taken seats.
*
* Time Complexity: O(n) for seat(), O(1) for leave(at:), Space Complexity: O(n)
*
*/

class ExamRoom {
var seats: [Int]

init(_ n: Int) {
seats = Array(repeating: 0, count: n)
}

func seat() -> Int {
var maxDistance = 0, maxIndex = 0, lastOne = -1

for (i, seat) in seats.enumerated() {
if seat == 1 {
if lastOne == -1 {
if maxDistance < i {
maxDistance = i
maxIndex = 0
}
} else {
if maxDistance < (i - lastOne) / 2 {
maxDistance = (i - lastOne) / 2
maxIndex = lastOne + (i - lastOne) / 2
}
}
}

lastOne = i
}

if lastOne != -1 {
if maxDistance < (seats.count - 1 - lastOne) / 2 {
maxIndex = seats.count - 1
}
}

seats[maxIndex] = 1

return maxIndex
}

func leave(_ seat: Int) {
seats[seat] = 0
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* [Microsoft](#microsoft)

## Progress
[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).
[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).


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

0 comments on commit 86355ec

Please sign in to comment.