Skip to content

Practice The Dining Philosophers

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1226
LeetCode Problem

The Dining Philosophers

Problem Statement

Five philosophers numbered 0 through 4 sit clockwise around a table, with one shared fork between each neighboring pair. A philosopher must hold both adjacent forks before eating and release both afterward; no fork may be held by two philosophers simultaneously. Implement wantsToEat(philosopher, pickLeftFork, pickRightFork, eat, putLeftFork, putRightFork), using the provided callbacks to acquire each named fork, eat, and release the forks. Five threads share the object. Calls for the same philosopher may overlap. Coordinate access so that every philosopher can continue eating and thinking without starvation. A philosopher is considered to be thinking while not requesting to eat.

Source: The Dining Philosophers on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 1
Output
[[4,2,1],[4,1,1],[0,1,1],[2,2,1],[2,1,1],[2,0,3],[2,1,2],[2,2,2],[4,0,3],[4,1,2],[0,2,1],[4,2,2],[3,2,1],[3,1,1],[0,0,3],[0,1,2],[0,2,2],[1,2,1],[1,1,1],[3,0,3],[3,1,2],[3,2,2],[1,0,3],[1,1,2],[1,2,2]]

Constraints

1 <= n <= 60

Related LeetCode Problems

Practice more problems similar to The Dining Philosophers.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.