MediumNo. 1226
The Dining Philosophers — LeetCode 1226 Python Solution
- Topics
- Concurrency
- 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
- Time
- O(n)
- Space · auxiliary
- O(1) to O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 1114 | Print in Order | Easy |
| 1115 | Print FooBar Alternately | Medium |
| 1116 | Print Zero Even Odd | Medium |
| 1117 | Building H2O | Medium |
| 0057 | Insert Interval | Medium |
| 0192 | Word Frequency | Medium |
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.