LeetCode 1226: The Dining Philosophers Solution
Master LeetCode problem 1226 (The Dining Philosophers), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1226. The Dining Philosophers
Problem Explanation
Explanation:
This problem is a classic example of the Dining Philosophers problem in concurrency. We need to implement a solution that avoids deadlocks and ensures mutual exclusion when philosophers are trying to pick up forks to eat.
To solve this problem, we can implement a solution using semaphores or mutex locks to represent the forks. Each philosopher can only eat if they can acquire both the left and right forks. We can also use a mutex lock to protect the critical section where a philosopher picks up both forks to ensure mutual exclusion.
The algorithm can be implemented using the following steps:
- Initialize an array of semaphores or mutex locks to represent the forks.
- Implement a function
wantsToEat()that corresponds to the actions a philosopher can take - picking up left fork, picking up right fork, eating, putting down left fork, and putting down right fork. - Use mutex locks to protect the critical section where a philosopher picks up both forks to ensure mutual exclusion.
- Implement the logic for each philosopher to alternate between thinking and eating based on the availability of forks.
The time complexity of this solution is O(1) for each operation, as acquiring and releasing a semaphore or mutex lock is a constant-time operation. The space complexity is O(n) for maintaining the semaphores or mutex locks.
: :
Solution Code
import java.util.concurrent.Semaphore;
class DiningPhilosophers {
private Semaphore[] forks;
public DiningPhilosophers() {
this.forks = new Semaphore[5];
for (int i = 0; i < 5; i++) {
this.forks[i] = new Semaphore(1);
}
}
public void wantsToEat(int philosopher, Runnable pickLeftFork, Runnable pickRightFork, Runnable eat, Runnable putLeftFork, Runnable putRightFork) throws InterruptedException {
int leftFork = philosopher;
int rightFork = (philosopher + 1) % 5;
if (philosopher % 2 == 0) {
forks[leftFork].acquire();
pickLeftFork.run();
forks[rightFork].acquire();
pickRightFork.run();
} else {
forks[rightFork].acquire();
pickRightFork.run();
forks[leftFork].acquire();
pickLeftFork.run();
}
eat.run();
forks[leftFork].release();
putLeftFork.run();
forks[rightFork].release();
putRightFork.run();
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1226 (The Dining Philosophers)?
This page provides optimized solutions for LeetCode problem 1226 (The Dining Philosophers) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1226 (The Dining Philosophers)?
The time complexity for LeetCode 1226 (The Dining Philosophers) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1226 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1226 in Java, C++, or Python.