Semaphores || Beats 100% || Python
Intuition : Semaphores\n\n\n\n# Code\n\nimport threading\n\nclass DiningPhilosophers(object):\n\n    def __init__(self):\n        self.forks = [threading.Semaph
# Intuition : Semaphores\n![image.png](https://assets.leetcode.com/users/images/d3a76ba7-6661-4ba6-9878-7c971ddf485f_1706811358.545665.png)\n\n\n# Code\n```\nimport threading\n\nclass DiningPhilosophers(object):\n\n    def __init__(self):\n        self.forks = [threading.Semaphore() for _ in range(5)]\n\n    def wantsToEat(self, i, pickLeftFork, pickRightFork, eat, putLeftFork, putRightFork):\n        n = 5\n        left_fork = self.forks[i]\n        right_fork = self.forks[(i + 1) % n]\n\n        if i == 0:\n            left_fork, right_fork = right_fork, left_fork\n\n        left_fork.acquire()\n        right_fork.acquire()\n        pickLeftFork()\n        pickRightFork()\n        eat()\n        putLeftFork()\n        putRightFork()\n        left_fork.release()\n        right_fork.release()\n\n```
['Python']