Easy Semaphore + Mutex || Love Babbar Solution || Best Approach
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Semaphore\n{\n    public:\n    Semaphore() {}\n    Semaphore(int c) : count(c) {}\n\n    void setCount(int a)\n    {\n        count=a;\n    }\n    inline void signal()\n    {\n        std::unique_lock<std::mutex> lock(m);\n        count++;\n        if(count<=0)\n        cv.notify_one();\n    }\n    inline void wait()\n    {\n        std::unique_lock<std::mutex> lock(m);\n        count--;\n        while(count<0)\n        {\n            cv.wait(lock);\n        }\n    }\n\n    private:\n    std::mutex m;\n    std::condition_variable cv;\n    int count;\n};\n\nclass DiningPhilosophers {\n    Semaphore fork[5];\n    std::mutex m,l;\npublic:\n    DiningPhilosophers() {\n        for(int i=0;i<5;i++)\n        {\n            fork[i].setCount(1);\n        }\n    }\n\n    void wantsToEat(int philosopher,\n                    function<void()> pickLeftFork,\n                    function<void()> pickRightFork,\n                    function<void()> eat,\n                    function<void()> putLeftFork,\n                    function<void()> putRightFork) {\n\t\t{\n            std::lock_guard<std::mutex>lock(m);\n            fork[(philosopher+1)%5].wait();\n            fork[philosopher].wait();\n        }\n        pickLeftFork();\n        pickRightFork();\n\n        eat();\n\n        putLeftFork();\n        fork[(philosopher+1)%5].signal();\n        putRightFork();\n        fork[philosopher].signal();\n    }\n};\n```
['C++']