The Dining Philosophers — LeetCode 1226 Python Solution
MediumConcurrency
- Problem
- #1226
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Five silent philosophers sit at a round table with bowls of spaghetti. Forks are placed between each pair of adjacent philosophers.
Example
- Input
- n = 1
- Output
- [[3,2,1],[3,1,1],[3,0,3],[3,1,2],[3,2,2],[4,2,1],[4,1,1],[2,2,1],[2,1,1],[1,2,1],[2,0,3],[2,1,2],[2,2,2],[4,0,3],[4,1,2],[4,2,2],[1,1,1],[1,0,3],[1,1,2],[1,2,2],[0,1,1],[0,2,1],[0,0,3],[0,1,2],[0,2,2]]
- Explanation
- n is the number of times each philosopher will call the function.
Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1) to O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 1226. The Dining Philosophers?
- LeetCode 1226. The Dining Philosophers is rated Medium on LeetCode.
- What topics does LeetCode 1226. The Dining Philosophers cover?
- LeetCode 1226. The Dining Philosophers is tagged Concurrency on LeetCode.