MediumNo. 1226

The Dining Philosophers — LeetCode 1226 Python Solution

Topics
Concurrency
Reading time
2 min

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

Related problems
No.ProblemDifficulty
1114Print in OrderEasy
1115Print FooBar AlternatelyMedium
1116Print Zero Even OddMedium
1117Building H2OMedium
0057Insert IntervalMedium
0192Word FrequencyMedium

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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.