question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
print-foobar-alternately | Easy to understand java solution | easy-to-understand-java-solution-by-ctap-ijtl | \nclass FooBar {\n private int n;\n int bar = 0;\n\n public FooBar(int n) {\n this.n = n;\n }\n\n public void foo(Runnable printFoo) throw | ctapal | NORMAL | 2021-03-08T04:17:19.032804+00:00 | 2021-03-08T04:18:06.224586+00:00 | 463 | false | ```\nclass FooBar {\n private int n;\n int bar = 0;\n\n public FooBar(int n) {\n this.n = n;\n }\n\n public void foo(Runnable printFoo) throws InterruptedException {\n \n \n for (int i = 0; i < n; i++) {\n synchronized(this) {\n if(bar == ... | 4 | 1 | ['Java'] | 0 |
print-foobar-alternately | Using semaphore java | using-semaphore-java-by-parthanemo123-ew2k | class FooBar {\n private int n;\n\n Semaphore s1 = new Semaphore(1);\n Semaphore s2 = new Semaphore(0);\n \n public FooBar(int n) {\n this | parthanemo123 | NORMAL | 2021-02-22T11:49:25.467732+00:00 | 2021-02-22T11:49:25.467822+00:00 | 273 | false | class FooBar {\n private int n;\n\n Semaphore s1 = new Semaphore(1);\n Semaphore s2 = new Semaphore(0);\n \n public FooBar(int n) {\n this.n = n;\n }\n\n public void foo(Runnable printFoo) throws InterruptedException {\n \n for (int i = 0; i < n; i++) {\n s1.acquire(... | 4 | 0 | [] | 1 |
print-foobar-alternately | [Java] A few Easy-to-understand Solutions with Explanation | java-a-few-easy-to-understand-solutions-l9cx0 | 1. Using Semaphore\nA Semaphore manages a set of virtual permits; the initial number of permits is passed to the Semaphore constructor. Activities can acqui | isaacjumac | NORMAL | 2020-07-29T10:50:45.710768+00:00 | 2020-07-29T11:15:03.271467+00:00 | 277 | false | ## 1. Using Semaphore\nA `Semaphore` manages a set of virtual permits; the initial number of permits is passed to the Semaphore constructor. Activities can `acquire` permits (as long as some remain) and `release` permits when they are done with them. If no permit is available, acquire blocks until one is (or until ... | 4 | 0 | [] | 0 |
print-foobar-alternately | Python 3 solution using threading.Condition() | python-3-solution-using-threadingconditi-2tv3 | The class FooBar is equipped with a threading.Condition() and a flag variable last_printed which is initialized as None. The foo() method waits until last_print | khpeek | NORMAL | 2019-08-19T00:02:18.577090+00:00 | 2019-08-19T00:02:18.577122+00:00 | 229 | false | The class `FooBar` is equipped with a `threading.Condition()` and a flag variable `last_printed` which is initialized as `None`. The `foo()` method waits until `last_printed` is either `None` or `\'bar\'`, while the `bar()` method waits until `last_printed` is `\'foo\'` (cf. https://docs.python.org/3/library/threading.... | 4 | 0 | [] | 1 |
print-foobar-alternately | Python2 clear | python2-clear-by-my_little_airport-9e42 | \nimport threading\nclass FooBar(object):\n def __init__(self, n):\n self.n = n\n self.f = threading.Semaphore()\n self.b = threading.Se | my_little_airport | NORMAL | 2019-07-12T01:49:30.732060+00:00 | 2019-07-12T01:49:30.732095+00:00 | 721 | false | ```\nimport threading\nclass FooBar(object):\n def __init__(self, n):\n self.n = n\n self.f = threading.Semaphore()\n self.b = threading.Semaphore()\n self.b.acquire()\n\n def foo(self, printFoo):\n """\n :type printFoo: method\n :rtype: void\n """\n ... | 4 | 0 | [] | 2 |
print-foobar-alternately | 📥 | Simple chanel solution | 100% | simple-chanel-solution-100-by-makarkanan-1xtk | \ntype FooBar struct {\n\tn int\n ch1 chan struct{}\n ch2 chan struct{}\n}\n\nfunc NewFooBar(n int) *FooBar {\n\treturn &FooBar{\n n: n,\n c | makarkananov | NORMAL | 2024-06-04T19:23:00.132395+00:00 | 2024-06-04T19:23:00.132423+00:00 | 690 | false | ```\ntype FooBar struct {\n\tn int\n ch1 chan struct{}\n ch2 chan struct{}\n}\n\nfunc NewFooBar(n int) *FooBar {\n\treturn &FooBar{\n n: n,\n ch1: make(chan struct{}, 1),\n ch2: make(chan struct{}, 1),\n }\n}\n\nfunc (fb *FooBar) Foo(printFoo func()) {\n\tfor i := 0; i < fb.n; i++ {\n ... | 3 | 0 | ['Concurrency', 'Go'] | 0 |
print-foobar-alternately | C# AutoResetEvent | c-autoresetevent-by-abicah-ac89 | 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 | abicah | NORMAL | 2023-03-02T13:57:21.591644+00:00 | 2023-03-02T13:57:21.591707+00:00 | 261 | false | # 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)$$ --... | 3 | 0 | ['C#'] | 0 |
print-foobar-alternately | 🔥 All Language in Leetcode Solution with Mutex | all-language-in-leetcode-solution-with-m-8chn | C++\n\n#include<semaphore.h>\n\nclass FooBar {\nprivate:\n int n;\n sem_t F, B;\n\npublic:\n FooBar(int n) {\n this->n = n;\n sem_init(&F | joenix | NORMAL | 2022-05-27T00:46:53.463406+00:00 | 2022-05-27T00:47:44.026322+00:00 | 950 | false | ***C++***\n```\n#include<semaphore.h>\n\nclass FooBar {\nprivate:\n int n;\n sem_t F, B;\n\npublic:\n FooBar(int n) {\n this->n = n;\n sem_init(&F, 0, 0);\n sem_init(&B, 0, 1);\n }\n\n void foo(function<void()> printFoo) {\n for (int i = 0; i < n; i++) {\n sem_wait(... | 3 | 0 | ['C', 'Python', 'Java', 'Python3'] | 0 |
print-foobar-alternately | [Java] Solution using Semaphore | java-solution-using-semaphore-by-imohann-raas | Using Semaphore, we want two threads to run n times. Each time, the thread \'foo\' will acquire the semaphore and the print foo, then the other thread \'bar\' w | imohannad | NORMAL | 2021-04-17T08:25:46.451633+00:00 | 2021-04-17T08:27:06.305473+00:00 | 220 | false | Using Semaphore, we want two threads to run n times. Each time, the thread \'foo\' will acquire the semaphore and the print foo, then the other thread \'bar\' would acquire the semaphore and print \'bar\'. Once we do this n times, both threads will end at the same time. \n\n\n\n\'\'\'\nclass FooBar \n{\n\n\tprivate int... | 3 | 0 | [] | 0 |
print-foobar-alternately | [Java] Just volatile | java-just-volatile-by-jokersun-0w9w | \nclass FooBar {\n \n\tprivate final int n;\n \n private volatile boolean flag;\n\n public FooBar(int n) {\n this.flag = false;\n this | jokersun | NORMAL | 2021-02-15T12:13:32.286425+00:00 | 2021-02-15T12:13:32.286450+00:00 | 215 | false | ```\nclass FooBar {\n \n\tprivate final int n;\n \n private volatile boolean flag;\n\n public FooBar(int n) {\n this.flag = false;\n this.n = n;\n }\n\n public void foo(Runnable printFoo) throws InterruptedException {\n for (int i = 0; i < n; i++) {\n while(flag){\n ... | 3 | 0 | [] | 0 |
print-foobar-alternately | Java | ReentrantLock with Condition | java-reentrantlock-with-condition-by-one-yqc0 | \nclass FooBar {\n private int n;\n \n Lock lock = new ReentrantLock();\n Condition fooCondition = lock.newCondition();\n volatile boolean isFoo | onesubhadip | NORMAL | 2020-09-17T11:03:07.101610+00:00 | 2020-09-17T11:03:07.101655+00:00 | 184 | false | ```\nclass FooBar {\n private int n;\n \n Lock lock = new ReentrantLock();\n Condition fooCondition = lock.newCondition();\n volatile boolean isFoo = true;\n\n public FooBar(int n) {\n this.n = n;\n }\n\n public void foo(Runnable printFoo) throws InterruptedException {\n \n ... | 3 | 0 | [] | 2 |
print-foobar-alternately | C++ mutex and condition_variable | c-mutex-and-condition_variable-by-ayilma-xzfr | Semaphore implementation with mutex and condition variable from stack overflow. For the people who don\'t want to use posix semaphores.\n\nclass Semaphore {\npu | ayilmaz81 | NORMAL | 2020-08-26T16:25:41.692687+00:00 | 2020-08-26T16:25:41.692735+00:00 | 629 | false | Semaphore implementation with mutex and condition variable from stack overflow. For the people who don\'t want to use posix semaphores.\n```\nclass Semaphore {\npublic:\n Semaphore (bool towait_)\n : towait(towait_) {}\n\n inline void notify()\n {\n std::unique_lock<std::mutex> lock(mtx);\n ... | 3 | 0 | ['C'] | 1 |
print-foobar-alternately | C# Using Interlocked and SpinWait faster than 100.00% less than 85.61% memory | c-using-interlocked-and-spinwait-faster-bjnf9 | Runtime: 44 ms\nMemory Usage: 16.7 MB\n\n\nusing System.Threading;\npublic class FooBar\n{\n private readonly int n;\n volatile int foo = 0;\n volatile | arslang | NORMAL | 2020-08-16T14:55:39.650189+00:00 | 2020-08-16T15:04:56.814545+00:00 | 408 | false | Runtime: 44 ms\nMemory Usage: 16.7 MB\n\n```\nusing System.Threading;\npublic class FooBar\n{\n private readonly int n;\n volatile int foo = 0;\n volatile int bar = 1;\n SpinWait spinWait = new SpinWait();\n\n public FooBar(int n)\n {\n this.n = n;\n }\n\n public void Foo(Action printFoo)... | 3 | 0 | [] | 3 |
print-foobar-alternately | Using simple semaphores in Java | using-simple-semaphores-in-java-by-naman-jljj | \nclass FooBar {\n private int n;\n private Semaphore foo_s = new Semaphore(1);\n private Semaphore bar_s = new Semaphore(0); \n \n\n public FooB | namandeept | NORMAL | 2020-03-18T12:51:18.279529+00:00 | 2020-03-18T12:51:18.279563+00:00 | 133 | false | ```\nclass FooBar {\n private int n;\n private Semaphore foo_s = new Semaphore(1);\n private Semaphore bar_s = new Semaphore(0); \n \n\n public FooBar(int n) {\n this.n = n;\n }\n\n public void foo(Runnable printFoo) throws InterruptedException {\n \n for (int i = 0; i < n; i++) ... | 3 | 0 | [] | 1 |
print-foobar-alternately | Easy C# Solution with 2 AutoResetEvents | easy-c-solution-with-2-autoresetevents-b-mdti | \nusing System.Threading; \n\npublic class FooBar\n {\n private int n;\n private readonly AutoResetEvent _fooEventWaitHandle;\n priva | maxpushkarev | NORMAL | 2020-01-11T06:00:34.973935+00:00 | 2020-01-11T06:54:54.729369+00:00 | 246 | false | ```\nusing System.Threading; \n\npublic class FooBar\n {\n private int n;\n private readonly AutoResetEvent _fooEventWaitHandle;\n private readonly AutoResetEvent _barEventWaitHandle;\n\n public FooBar(int n)\n {\n this.n = n;\n _fooEventWaitHandle = new Au... | 3 | 0 | [] | 2 |
print-foobar-alternately | Simple Java Solution with two Semaphores, beat 100% | simple-java-solution-with-two-semaphores-wvu9 | \nclass FooBar {\n private int n;\n \n private Semaphore fooSem;\n \n private Semaphore barSem;\n\n public FooBar(int n) {\n this.n = n | yizeliu | NORMAL | 2019-12-27T15:13:02.236746+00:00 | 2019-12-27T15:13:02.236799+00:00 | 308 | false | ```\nclass FooBar {\n private int n;\n \n private Semaphore fooSem;\n \n private Semaphore barSem;\n\n public FooBar(int n) {\n this.n = n;\n this.fooSem = new Semaphore(1);\n this.barSem = new Semaphore(0);\n }\n\n public void foo(Runnable printFoo) throws InterruptedExcept... | 3 | 0 | [] | 0 |
print-foobar-alternately | C# 56ms Using Semaphore | c-56ms-using-semaphore-by-deleted_user-3ri2 | \nusing System;\nusing System.Threading;\n\npublic class FooBar {\n private int n;\n private Semaphore semFoo;\n private Semaphore semBar;\n \n\ | deleted_user | NORMAL | 2019-11-21T07:12:44.370338+00:00 | 2019-11-21T07:12:44.370371+00:00 | 168 | false | ```\nusing System;\nusing System.Threading;\n\npublic class FooBar {\n private int n;\n private Semaphore semFoo;\n private Semaphore semBar;\n \n\n public FooBar(int n) {\n this.n = n;\n this.semFoo = new Semaphore(initialCount: 0, maximumCount: 1);\n this.semBar = new Semaphore... | 3 | 0 | [] | 0 |
print-foobar-alternately | C++ Semaphore solution | c-semaphore-solution-by-plateofpasta-jlzi | c++\n#include <semaphore.h> // from POSIX\n\nclass FooBar \n{\npublic:\n FooBar(int n) \n {\n this->n = n;\n sem_init(&foo_, 0, 1);\n sem_init(&bar_, | plateofpasta | NORMAL | 2019-11-09T06:05:14.876364+00:00 | 2019-11-09T16:54:03.699112+00:00 | 885 | false | ```c++\n#include <semaphore.h> // from POSIX\n\nclass FooBar \n{\npublic:\n FooBar(int n) \n {\n this->n = n;\n sem_init(&foo_, 0, 1);\n sem_init(&bar_, 0, 0);\n }\n \n ~FooBar()\n {\n sem_destroy(&foo_); \n sem_destroy(&bar_); \n }\n\n void foo(function<void()> printFoo) \n {\n for (int i = ... | 3 | 0 | ['C'] | 0 |
print-foobar-alternately | Simple java solution with explanation | simple-java-solution-with-explanation-by-7qr3 | Simple Java solution with two Semaphores.\n\nimport java.util.concurrent.*;\nclass FooBar {\n private int n;\n private Semaphore one, two; //Declaring two | malex | NORMAL | 2019-08-27T18:29:41.971836+00:00 | 2019-08-27T18:29:41.971868+00:00 | 423 | false | Simple Java solution with two Semaphores.\n```\nimport java.util.concurrent.*;\nclass FooBar {\n private int n;\n private Semaphore one, two; //Declaring two Semaphores.\n\n public FooBar(int n) {\n this.n = n;\n one = new Semaphore(1); //First semaphore initialized with one permit available\n ... | 3 | 0 | [] | 0 |
print-foobar-alternately | Simple Python Solution | simple-python-solution-by-simonkocurek-nsda | python\nfrom threading import Lock\n\nclass FooBar:\n def __init__(self, n):\n self.n = n\n self.foo_lock = Lock()\n self.bar_lock = Loc | simonkocurek | NORMAL | 2019-07-25T18:58:17.357211+00:00 | 2019-07-25T18:58:17.357246+00:00 | 393 | false | ```python\nfrom threading import Lock\n\nclass FooBar:\n def __init__(self, n):\n self.n = n\n self.foo_lock = Lock()\n self.bar_lock = Lock()\n \n # Don\'t start with bar\n self.bar_lock.acquire()\n\n\n def foo(self, printFoo: \'Callable[[], None]\') -> None:\n \n... | 3 | 1 | [] | 2 |
print-foobar-alternately | posix thread C solution | posix-thread-c-solution-by-xuchen321-rcvx | the similar problem like 1114,using posix thread tools to solve this\n\n#include <pthread.h>\n\n/*\n 1.we need two thread do not interrupt with each other\n | xuchen321 | NORMAL | 2019-07-15T00:12:28.339793+00:00 | 2019-07-15T00:12:28.339833+00:00 | 349 | false | the similar problem like 1114,using posix thread tools to solve this\n```\n#include <pthread.h>\n\n/*\n 1.we need two thread do not interrupt with each other\n means when one thread is outputing, the other one don\'t do that\n 2.we have the preference in 1\'s basis, one need to do first \n ====> for mutual exclus... | 3 | 0 | [] | 1 |
print-foobar-alternately | [Cpp] Mutex solution | cpp-mutex-solution-by-insanecoder-bw5p | ```\nclass FooBar {\n mutex m1,m2;\nprivate:\n int n;\n\npublic:\n FooBar(int n) {\n this->n = n;\n\t\tm1.lock();\n }\n\n void foo(functio | insanecoder | NORMAL | 2019-07-13T12:26:27.278713+00:00 | 2019-07-13T16:49:25.967377+00:00 | 1,174 | false | ```\nclass FooBar {\n mutex m1,m2;\nprivate:\n int n;\n\npublic:\n FooBar(int n) {\n this->n = n;\n\t\tm1.lock();\n }\n\n void foo(function<void()> printFoo) {\n \n for (int i = 0; i < n; i++) {\n \n \t// printFoo() outputs "foo". Do not change or remove this line.\... | 3 | 0 | ['C', 'C++'] | 5 |
print-foobar-alternately | [C++] Using std::condition_variable | c-using-stdcondition_variable-by-mhelven-ggt7 | C++\nclass FooBar {\nprivate:\n int n;\n bool fooTime;\n std::mutex mtx;\n std::condition_variable cv;\n\npublic:\n FooBar( | mhelvens | NORMAL | 2019-07-12T19:44:52.621051+00:00 | 2019-07-17T07:02:54.431174+00:00 | 1,117 | false | ```C++\nclass FooBar {\nprivate:\n int n;\n bool fooTime;\n std::mutex mtx;\n std::condition_variable cv;\n\npublic:\n FooBar(int n) : n(n), fooTime(true) {}\n \n void setFooTime(bool ft, std::unique_lock<std::mutex>& lock) {\n fooTime = ft;\n lock.unlock();\n cv.notify_o... | 3 | 0 | ['C'] | 3 |
print-foobar-alternately | Python Codintion | python-codintion-by-alexfvo-kilp | python\nfrom threading import Condition\nfrom typing import Callable\n\n\nclass FooBar:\n def __init__(self, n):\n self.n = n\n self.cv = Condi | alexfvo | NORMAL | 2019-07-12T08:52:18.287956+00:00 | 2019-07-12T08:52:18.287989+00:00 | 330 | false | ```python\nfrom threading import Condition\nfrom typing import Callable\n\n\nclass FooBar:\n def __init__(self, n):\n self.n = n\n self.cv = Condition()\n self.printed_foo = False\n\n def foo(self, printFoo: \'Callable[[], None]\') -> None:\n\n for i in range(self.n):\n # pr... | 3 | 0 | [] | 0 |
print-foobar-alternately | Easy Beginner Friendly Solution 😊 | easy-beginner-friendly-solution-by-praga-kppy | Code | pragadeeshxenocrate | NORMAL | 2025-01-24T11:40:54.826971+00:00 | 2025-01-24T11:40:54.826971+00:00 | 726 | false |
# Code
```java []
class FooBar {
private int n;
boolean flag = true; // set flag to alternate between foo and bar
public FooBar(int n) {
this.n = n;
}
public synchronized void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) {
wh... | 2 | 0 | ['Java'] | 1 |
print-foobar-alternately | Simple & clean solution in C++ beats 82%!!!! | simple-clean-solution-in-c-beats-82-by-r-japo | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nUsing 2 semaphores\n- s | RacheliBoyer | NORMAL | 2024-08-05T11:42:38.054519+00:00 | 2024-08-05T11:42:38.054564+00:00 | 43 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsing 2 semaphores\n- smph1 starts at 1: It allows the foo method to run first. When foo is called, it prints "foo" and releases smph2.\n- smph2 starts at 0: It blocks... | 2 | 0 | ['Concurrency', 'C++'] | 0 |
print-foobar-alternately | The most simple solution in C++ beats 82%!! | the-most-simple-solution-in-c-beats-82-b-77vp | Intuition\n Describe your first thoughts o/ how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nUsing 2 semaphores.\n- | RacheliBoyer | NORMAL | 2024-08-05T11:40:03.008613+00:00 | 2024-08-05T11:40:03.008636+00:00 | 31 | false | # Intuition\n<!-- Describe your first thoughts o/ how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsing 2 semaphores.\n- smph1 starts at 1: It allows the foo method to run first. When foo is called, it prints "foo" and releases smph2.\n- smph2 starts at 0: It block... | 2 | 0 | ['Concurrency', 'C++'] | 0 |
print-foobar-alternately | C++ EASY AND CLEAN CODE | c-easy-and-clean-code-by-arpiii_7474-tnlp | Code\n\nclass FooBar {\nprivate:\n int n;\n mutex m1,m2;\npublic:\n FooBar(int n) {\n this->n = n;\n m2.lock();\n }\n void foo(func | arpiii_7474 | NORMAL | 2023-09-29T13:18:10.367261+00:00 | 2023-09-29T13:18:10.367280+00:00 | 418 | false | # Code\n```\nclass FooBar {\nprivate:\n int n;\n mutex m1,m2;\npublic:\n FooBar(int n) {\n this->n = n;\n m2.lock();\n }\n void foo(function<void()> printFoo) {\n for (int i = 0; i < n; i++) {\n m1.lock();\n \tprintFoo();\n m2.unlock();\n }\n }\... | 2 | 0 | ['C++'] | 0 |
print-foobar-alternately | [Beats 100%] Using 2 semaphores - easy to understand | beats-100-using-2-semaphores-easy-to-und-s82n | Full code with test suite\nhttps://github.com/Freeze777/SDE-Interviewer-Notes/blob/main/LeetCodeJava/src/main/java/leetcode/concurrency/FooBar.java\n\n# Code\n\ | freeze_francis | NORMAL | 2023-09-01T17:17:13.479793+00:00 | 2023-09-01T17:17:13.479811+00:00 | 73 | false | # Full code with test suite\nhttps://github.com/Freeze777/SDE-Interviewer-Notes/blob/main/LeetCodeJava/src/main/java/leetcode/concurrency/FooBar.java\n\n# Code\n```\nclass FooBar {\n private final int n;\n private final Semaphore foo = new Semaphore(1);\n private final Semaphore bar = new Semaphore(0);\n\n ... | 2 | 0 | ['Java'] | 0 |
print-foobar-alternately | Python with two Events | python-with-two-events-by-trpaslik-rrvr | Intuition\nTwo events: "ready for foo" and "ready for bar". \n\n# Approach\nCreate two events. Just set and clear each in proper moment.\nSee the comments in th | trpaslik | NORMAL | 2022-12-28T22:44:13.207379+00:00 | 2022-12-28T22:44:45.668028+00:00 | 744 | false | # Intuition\nTwo events: "ready for foo" and "ready for bar". \n\n# Approach\nCreate two events. Just set and clear each in proper moment.\nSee the comments in the code.\n\nIf you like it, plese up-vote. Thank you!\n\n\n# Code\n```\nfrom threading import Event, Barrier\n\nclass FooBar:\n def __init__(self, n):\n ... | 2 | 0 | ['Concurrency', 'Python', 'Python3'] | 1 |
print-foobar-alternately | Python with one Barrier | python-with-one-barrier-by-trpaslik-0s0k | Intuition\nOne Barrier should be enough to synch each iteration\n\n# Approach\nUse single barrier to sync AFTER foo and BEFORE the bar in each iteration on both | trpaslik | NORMAL | 2022-12-28T22:38:10.332539+00:00 | 2022-12-28T22:38:10.332569+00:00 | 1,014 | false | # Intuition\nOne Barrier should be enough to synch each iteration\n\n# Approach\nUse single barrier to sync AFTER foo and BEFORE the bar in each iteration on both threads.\n\nIf you like it, plese up-vote. Thank you!\n\n\n# Code\n```\nfrom threading import Event, Barrier\n\nclass FooBar:\n def __init__(self, n):\n ... | 2 | 1 | ['Concurrency', 'Python', 'Python3'] | 2 |
print-foobar-alternately | Java Solution | java-solution-by-rishi_rajj-egin | \nclass FooBar {\n private int n;\n boolean l = true;\n\n public FooBar(int n) {\n this.n = n;\n }\n\n public synchronized void foo(Runnab | rishi_rajj | NORMAL | 2022-09-19T06:11:45.355363+00:00 | 2022-09-19T06:11:45.355401+00:00 | 1,282 | false | ```\nclass FooBar {\n private int n;\n boolean l = true;\n\n public FooBar(int n) {\n this.n = n;\n }\n\n public synchronized void foo(Runnable printFoo) throws InterruptedException {\n \n for (int i = 0; i < n; i++) {\n while(!l)\n {\n wait();\n ... | 2 | 0 | [] | 1 |
print-foobar-alternately | Java | Beat 80% simple semaphore solution | java-beat-80-simple-semaphore-solution-b-b7sg | \nprivate int n;\n private Semaphore sem1;\n private Semaphore sem2;\n public FooBar(int n) {\n this.n = n;\n //as foo print need happen | enjoy209 | NORMAL | 2022-09-05T02:26:32.320479+00:00 | 2022-09-05T02:26:32.320528+00:00 | 924 | false | ```\nprivate int n;\n private Semaphore sem1;\n private Semaphore sem2;\n public FooBar(int n) {\n this.n = n;\n //as foo print need happen first, thus Semaphore need to be 1.\n sem1 = new Semaphore(1);\n sem2 = new Semaphore(0);\n }\n\n public void foo(Runnable printFoo) thro... | 2 | 0 | ['Java'] | 0 |
print-foobar-alternately | Simple JAVA semaphore solution | simple-java-semaphore-solution-by-db16-cnt8 | \nclass FooBar {\n private int n;\n Semaphore fooSemaphore;\n Semaphore barSemaphore;\n\n public FooBar(int n) {\n this.n = n;\n fooSe | db16 | NORMAL | 2022-05-06T18:57:30.113640+00:00 | 2022-05-06T18:57:30.113684+00:00 | 237 | false | ```\nclass FooBar {\n private int n;\n Semaphore fooSemaphore;\n Semaphore barSemaphore;\n\n public FooBar(int n) {\n this.n = n;\n fooSemaphore = new Semaphore(1);\n barSemaphore = new Semaphore(0);\n }\n\n public void foo(Runnable printFoo) throws InterruptedException {\n ... | 2 | 0 | [] | 0 |
print-foobar-alternately | Java synchronized version | java-synchronized-version-by-albertguosg-v01s | \nclass FooBar {\n private int n;\n private Object lock;\n private int count;\n\n public FooBar(int n) {\n this.n = n;\n this.lock = n | albertguosgp | NORMAL | 2022-01-27T22:43:39.806649+00:00 | 2022-01-27T22:43:39.806677+00:00 | 289 | false | ```\nclass FooBar {\n private int n;\n private Object lock;\n private int count;\n\n public FooBar(int n) {\n this.n = n;\n this.lock = new Object();\n this.count = 1;\n }\n\n public void foo(Runnable printFoo) throws InterruptedException {\n for (int i = 0; i < n; i++) {\n... | 2 | 0 | [] | 1 |
print-foobar-alternately | [C#] AutoResetEvent solution | c-autoresetevent-solution-by-basmus-87za | \nusing System;\nusing System.Threading;\n\npublic class FooBar \n{\n private int n;\n\n private AutoResetEvent fooEvent = new AutoResetEvent(true);\n | basmus | NORMAL | 2022-01-20T19:52:20.017021+00:00 | 2022-01-20T19:52:54.934675+00:00 | 99 | false | ```\nusing System;\nusing System.Threading;\n\npublic class FooBar \n{\n private int n;\n\n private AutoResetEvent fooEvent = new AutoResetEvent(true);\n private AutoResetEvent barEvent = new AutoResetEvent(false);\n \n public FooBar(int n) \n {\n this.n = n;\n }\n\n public void Foo(Actio... | 2 | 0 | [] | 0 |
print-foobar-alternately | SImple Java Solution | simple-java-solution-by-aruslans-nzmx | \nclass FooBar {\n private int n;\n private volatile boolean isFoo = true;\n\n public FooBar(int n) {\n this.n = n;\n }\n\n public void fo | aruslans | NORMAL | 2021-12-15T20:56:39.045358+00:00 | 2021-12-15T20:56:39.045390+00:00 | 269 | false | ```\nclass FooBar {\n private int n;\n private volatile boolean isFoo = true;\n\n public FooBar(int n) {\n this.n = n;\n }\n\n public void foo(Runnable printFoo) throws InterruptedException {\n for (int i = 0; i < n; i++) {\n while (!isFoo);\n \n \t// printFoo.r... | 2 | 0 | [] | 2 |
print-foobar-alternately | Java ReentrantLock + Condition + Volatile | java-reentrantlock-condition-volatile-by-tjom | Please check my solution, simple to understand~ (faster than 99.43%, less than 96.24% memory)\nJava\nimport java.util.*;\n\nclass FooBar {\n \n private i | arthas-wu | NORMAL | 2021-11-28T05:15:36.696476+00:00 | 2021-12-02T13:55:35.189423+00:00 | 305 | false | Please check my solution, simple to understand~ (faster than 99.43%, less than 96.24% memory)\n```Java\nimport java.util.*;\n\nclass FooBar {\n \n private int n;\n private volatile boolean invokeFoo;\n private final Lock lock = new ReentrantLock();\n private Condition fooC = lock.newCondition();\n pr... | 2 | 0 | ['Java'] | 2 |
print-foobar-alternately | [Java] Simple solution using synchronised and wait | java-simple-solution-using-synchronised-wrvkw | \nclass FooBar {\n private int n;\n\n public FooBar(int n) {\n this.n = n;\n }\n\n public void foo(Runnable printFoo) throws InterruptedExcep | phoenix_007 | NORMAL | 2021-05-31T12:25:40.957631+00:00 | 2021-05-31T12:25:40.957662+00:00 | 286 | false | ```\nclass FooBar {\n private int n;\n\n public FooBar(int n) {\n this.n = n;\n }\n\n public void foo(Runnable printFoo) throws InterruptedException {\n \n \n for (int i = 0; i < n; i++) {\n \n \t// printFoo.run() outputs "foo". Do not change or remove this line.... | 2 | 1 | [] | 1 |
print-foobar-alternately | Java very easy Semaphore Solution | java-very-easy-semaphore-solution-by-sha-vkh4 | class FooBar {\n private int n;\n\n public FooBar(int n) {\n this.n = n;\n }\n \n Semaphore sem1 = new Semaphore(0);\n Semaphore sem2 = | shadersy | NORMAL | 2021-04-08T12:54:56.120391+00:00 | 2021-04-08T12:54:56.120425+00:00 | 215 | false | class FooBar {\n private int n;\n\n public FooBar(int n) {\n this.n = n;\n }\n \n Semaphore sem1 = new Semaphore(0);\n Semaphore sem2 = new Semaphore(0);\n\n public void foo(Runnable printFoo) throws InterruptedException {\n \n for (int i = 0; i < n; i++) {\n \n ... | 2 | 1 | [] | 0 |
print-foobar-alternately | C++ Ultra Simple Solution with Mutexes | c-ultra-simple-solution-with-mutexes-by-gmv2n | \nclass FooBar {\nprivate:\n int n;\n \n std::mutex foo_gate;\n std::mutex bar_gate;\n\npublic:\n FooBar(int new_n) : n(new_n) {\n bar_gat | chaflan | NORMAL | 2021-03-13T00:06:47.884596+00:00 | 2021-03-13T04:45:53.577814+00:00 | 201 | false | ```\nclass FooBar {\nprivate:\n int n;\n \n std::mutex foo_gate;\n std::mutex bar_gate;\n\npublic:\n FooBar(int new_n) : n(new_n) {\n bar_gate.lock();\n }\n\n void foo(function<void()> printFoo) {\n for (int i = 0; i < n; i++) {\n foo_gate.lock();\n \tprintFoo();\n ... | 2 | 0 | [] | 0 |
print-foobar-alternately | C# using AutoResetEvent. Clean codes | c-using-autoresetevent-clean-codes-by-wm-df2o | \nusing System.Threading;\n\npublic class FooBar {\n private int n;\n private AutoResetEvent printFooEvent;\n private AutoResetEvent printBarEvent;\n | wmjbandara | NORMAL | 2021-03-06T07:25:38.948764+00:00 | 2021-03-06T07:25:38.948809+00:00 | 98 | false | ```\nusing System.Threading;\n\npublic class FooBar {\n private int n;\n private AutoResetEvent printFooEvent;\n private AutoResetEvent printBarEvent;\n public FooBar(int n) {\n printFooEvent = new AutoResetEvent(true);\n printBarEvent = new AutoResetEvent(false);\n this.n = n;\n }\n... | 2 | 0 | [] | 0 |
print-foobar-alternately | Double Semaphore Solution | double-semaphore-solution-by-gbajorin-dqjk | \nimport java.util.concurrent.Semaphore;\n\n\nclass FooBar {\n private int n;\n private static Semaphore foo = new Semaphore(0);\n private static Semap | gbajorin | NORMAL | 2021-01-15T05:12:23.886729+00:00 | 2021-01-15T05:12:23.886770+00:00 | 130 | false | ```\nimport java.util.concurrent.Semaphore;\n\n\nclass FooBar {\n private int n;\n private static Semaphore foo = new Semaphore(0);\n private static Semaphore bar = new Semaphore(0);\n public FooBar(int n) {\n this.n = n;\n }\n \n\n public void foo(Runnable printFoo) throws InterruptedExcept... | 2 | 0 | [] | 0 |
print-foobar-alternately | Java old school wait/notify solution | java-old-school-waitnotify-solution-by-a-593y | counter variable can be replaced with boolean type but if you want to track the number of invocation it is worth keeping int\n\nclass FooBar {\n private int | aleksgladun4 | NORMAL | 2021-01-13T08:06:36.450887+00:00 | 2021-01-13T08:06:36.450914+00:00 | 225 | false | `counter` variable can be replaced with `boolean` type but if you want to track the number of invocation it is worth keeping `int`\n```\nclass FooBar {\n private int n;\n private volatile int counter = 1;\n\n public FooBar(int n) {\n this.n = n;\n }\n\n\n public void foo(Runnable printFoo) throws ... | 2 | 1 | [] | 0 |
print-foobar-alternately | Java semaphore solution - easy to understand | java-semaphore-solution-easy-to-understa-w9yz | \nclass FooBar {\n private int n;\n private Semaphore sem1, sem2;\n\n public FooBar(int n) {\n this.n = n;\n this.sem1 = new Semaphore(1) | tankztc | NORMAL | 2020-12-24T04:01:25.074302+00:00 | 2020-12-24T04:01:25.074344+00:00 | 182 | false | ```\nclass FooBar {\n private int n;\n private Semaphore sem1, sem2;\n\n public FooBar(int n) {\n this.n = n;\n this.sem1 = new Semaphore(1);\n this.sem2 = new Semaphore(0);\n }\n\n public void foo(Runnable printFoo) throws InterruptedException {\n \n for (int i = 0; i ... | 2 | 0 | [] | 0 |
print-foobar-alternately | Java Solution with simple atomic boolean | java-solution-with-simple-atomic-boolean-qerw | Simple AtomicBoolean. However, while loops will eat out CPU cycles.\n\nclass FooBar {\n private int n;\n private AtomicBoolean acquire;\n \n public | farruh | NORMAL | 2020-11-28T06:01:01.816551+00:00 | 2020-11-28T06:01:01.816582+00:00 | 168 | false | Simple AtomicBoolean. However, while loops will eat out CPU cycles.\n```\nclass FooBar {\n private int n;\n private AtomicBoolean acquire;\n \n public FooBar(int n) {\n this.n = n;\n acquire = new AtomicBoolean(true);\n\n }\n\n public void foo(Runnable printFoo) throws InterruptedExcept... | 2 | 0 | [] | 1 |
print-foobar-alternately | C++ RAII Styled Locking 12ms faster than 99.60 | c-raii-styled-locking-12ms-faster-than-9-fc6a | We only use cond notify one to reduce contention grabbing the mutex lock. Makes use of raii locking. \n\nOther alternatives:\n- use a binary sempahore for signa | advra | NORMAL | 2020-11-27T01:23:24.820430+00:00 | 2020-11-28T21:37:23.187838+00:00 | 116 | false | We only use cond notify one to reduce contention grabbing the mutex lock. Makes use of raii locking. \n\nOther alternatives:\n- use a binary sempahore for signaling. \n- without mutex and cond you can do spin lock with atomic bool\n\n```\nclass FooBar {\nprivate:\n int n;\n mutex m;\n condition_variable cond;\... | 2 | 0 | [] | 0 |
print-foobar-alternately | Java Solution using only shared global variable.(Using Yield) | java-solution-using-only-shared-global-v-jl8z | \nclass FooBar {\n private int n;\n Boolean flag;\n\n public FooBar(int n) {\n this.n = n;\n flag = true;\n }\n\n public void foo(R | theBeardGuy | NORMAL | 2020-08-14T08:11:39.951996+00:00 | 2020-08-14T08:11:39.952036+00:00 | 109 | false | ```\nclass FooBar {\n private int n;\n Boolean flag;\n\n public FooBar(int n) {\n this.n = n;\n flag = true;\n }\n\n public void foo(Runnable printFoo) throws InterruptedException {\n \n for (int i = 0; i < n; i++) {\n while(flag!=true)\n Thread... | 2 | 0 | [] | 0 |
print-foobar-alternately | C++ condition variable | c-condition-variable-by-leolounz-liw3 | class FooBar {\nprivate:\n int n;\n std::mutex m;\n std::condition_variable cv;\n bool flag = false;\n\npublic:\n FooBar(int n) {\n this-> | leolounz | NORMAL | 2020-07-04T10:18:06.712949+00:00 | 2020-07-04T10:18:06.712997+00:00 | 196 | false | class FooBar {\nprivate:\n int n;\n std::mutex m;\n std::condition_variable cv;\n bool flag = false;\n\npublic:\n FooBar(int n) {\n this->n = n;\n }\n\n void foo(function<void()> printFoo) {\n \n for (int i = 0; i < n; i++) {\n \n \t// printFoo() outputs "foo"... | 2 | 0 | [] | 1 |
print-foobar-alternately | C# faster than 90.67%, less then 100% Mem | c-faster-than-9067-less-then-100-mem-by-t1fys | Runtime: 52 ms\nMemory Usage: 16.8 MB\n```\npublic class FooBar {\n private int n;\n\n private System.Threading.AutoResetEvent fooReady;\n private Syst | ve7545 | NORMAL | 2020-05-31T18:16:50.950043+00:00 | 2020-05-31T18:17:29.935556+00:00 | 236 | false | Runtime: 52 ms\nMemory Usage: 16.8 MB\n```\npublic class FooBar {\n private int n;\n\n private System.Threading.AutoResetEvent fooReady;\n private System.Threading.AutoResetEvent barReady;\n \n public FooBar(int n) {\n this.n = n;\n fooReady = new System.Threading.AutoResetEvent(true);\n ... | 2 | 0 | [] | 0 |
print-foobar-alternately | Java Volatile flag and Thread.sleep(1) to skip TLE | java-volatile-flag-and-threadsleep1-to-s-9k1p | \nclass FooBar {\n private int n;\n private volatile boolean flag = true;\n\n \n public FooBar(int n) {\n this.n = n;\n }\n\n public vo | mastercaca | NORMAL | 2020-05-18T15:28:56.550965+00:00 | 2020-05-19T00:18:16.954112+00:00 | 131 | false | ```\nclass FooBar {\n private int n;\n private volatile boolean flag = true;\n\n \n public FooBar(int n) {\n this.n = n;\n }\n\n public void foo(Runnable printFoo) throws InterruptedException {\n \n for (int i = 0; i < n; i++) {\n while(!flag){\n // w/o t... | 2 | 0 | [] | 0 |
print-foobar-alternately | Synchronized With Wait & Notify [JAVA] | synchronized-with-wait-notify-java-by-dv-n1ou | \nclass FooBar {\n private int n;\n boolean fooPrinted = false;\n public FooBar(int n) {\n this.n = n;\n }\n\n public synchronized void fo | dvvalabs | NORMAL | 2020-04-28T08:17:56.623661+00:00 | 2020-04-28T08:17:56.623715+00:00 | 353 | false | ```\nclass FooBar {\n private int n;\n boolean fooPrinted = false;\n public FooBar(int n) {\n this.n = n;\n }\n\n public synchronized void foo(Runnable printFoo) throws InterruptedException {\n for (int i = 0; i < n; i++) {\n if(fooPrinted) this.wait();\n printFoo.run(... | 2 | 1 | ['Java'] | 1 |
print-foobar-alternately | C : simply sema | c-simply-sema-by-universalcoder12-dz2t | \ntypedef struct {\n sem_t fsema, bsema;\n int n;\n} FooBar;\n\nFooBar* fooBarCreate(int n) {\n FooBar* obj = (FooBar*) malloc(sizeof(FooBar));\n se | universalcoder12 | NORMAL | 2020-03-25T05:54:16.655697+00:00 | 2020-03-25T05:54:58.676813+00:00 | 289 | false | ```\ntypedef struct {\n sem_t fsema, bsema;\n int n;\n} FooBar;\n\nFooBar* fooBarCreate(int n) {\n FooBar* obj = (FooBar*) malloc(sizeof(FooBar));\n sem_init(&obj->fsema, 0, 1);\n sem_init(&obj->bsema, 0, 0);\n obj->n = n;\n return obj;\n}\n\nvoid foo(FooBar* obj) {\n for (int i = 0; i < obj->n;... | 2 | 0 | ['C'] | 0 |
print-foobar-alternately | python, classic Semaphore based solution explained | python-classic-semaphore-based-solution-aa7pl | class FooBar:\n\n def __init__(self, n):\n self.n = n\n self.foo_lock = threading.Semaphore(1)\n self.bar_lock = threading.Semaphore(0)\ | rmoskalenko | NORMAL | 2020-03-17T23:34:42.743437+00:00 | 2020-03-17T23:35:42.451586+00:00 | 149 | false | ```class FooBar:\n\n def __init__(self, n):\n self.n = n\n self.foo_lock = threading.Semaphore(1)\n self.bar_lock = threading.Semaphore(0)\n\n def foo(self, printFoo):\n for i in range(self.n):\n self.foo_lock.acquire()\n printFoo()\n self.bar_lock.rele... | 2 | 0 | [] | 1 |
print-foobar-alternately | Easy C++ using condition variable and two atomic counters [16 ms, 99.78%] | easy-c-using-condition-variable-and-two-hdole | Each thread increases a counter of how many times it has printed.\nEach thread also checks the other thread\'s counter to see whether it is high enough for this | ahcox | NORMAL | 2020-01-21T23:20:05.256831+00:00 | 2020-01-21T23:20:05.256882+00:00 | 1,189 | false | Each thread increases a counter of how many times it has printed.\nEach thread also checks the other thread\'s counter to see whether it is high enough for this thread to go ahead and print another time. If it isn\'t high enough, the thread waits until it is signalled by the other thread through their shared condition ... | 2 | 0 | ['C'] | 3 |
print-foobar-alternately | c++ semaphore | c-semaphore-by-tensor-flower-vut2 | \n#include<semaphore.h>\nclass FooBar {\nprivate:\n int n;\n\tsem_t s1,s2;\n\npublic:\n FooBar(int n) {\n this->n = n;\n sem_init(&s1,0,0);\ | tensor-flower | NORMAL | 2019-12-19T01:17:19.045425+00:00 | 2019-12-19T01:28:43.936872+00:00 | 368 | false | ```\n#include<semaphore.h>\nclass FooBar {\nprivate:\n int n;\n\tsem_t s1,s2;\n\npublic:\n FooBar(int n) {\n this->n = n;\n sem_init(&s1,0,0);\n sem_init(&s2,0,1);\n }\n\n void foo(function<void()> printFoo) {\n \n for (int i = 0; i < n; i++) {\n sem_wait(&s2);\... | 2 | 0 | ['C'] | 0 |
print-foobar-alternately | python solution with threading.Events | python-solution-with-threadingevents-by-3t34g | Simple Python solution:\n\n\nimport threading\nclass FooBar(object):\n def __init__(self, n):\n self.n = n\n self.foo_event = threading.Event() | vkaplarevic | NORMAL | 2019-12-09T21:21:17.432617+00:00 | 2019-12-09T21:21:17.432650+00:00 | 169 | false | Simple Python solution:\n\n```\nimport threading\nclass FooBar(object):\n def __init__(self, n):\n self.n = n\n self.foo_event = threading.Event()\n self.bar_event = threading.Event()\n\n\n def foo(self, printFoo):\n """\n :type printFoo: method\n :rtype: void\n ""... | 2 | 1 | [] | 1 |
print-foobar-alternately | Java Solution - BlockingQueue-s | java-solution-blockingqueue-s-by-akazlou-dy32 | Using 2 separate blocking queues each of capacity of 1. \n\n\nimport java.util.concurrent.ArrayBlockingQueue;\nimport java.util.concurrent.BlockingQueue;\n\ncla | akazlou | NORMAL | 2019-11-27T12:21:12.326116+00:00 | 2019-11-27T12:21:23.304744+00:00 | 435 | false | Using 2 separate blocking queues each of capacity of 1. \n\n```\nimport java.util.concurrent.ArrayBlockingQueue;\nimport java.util.concurrent.BlockingQueue;\n\nclass FooBar {\n private int n;\n final BlockingQueue<Boolean> queue1 = new ArrayBlockingQueue<>(1);\n final BlockingQueue<Boolean> queue2 = new ArrayB... | 2 | 0 | ['Java'] | 0 |
print-foobar-alternately | Go | go-by-npx88-fx0g | go\t\npackage main\n\nimport (\n\t"fmt"\n\t"time"\n)\n\nvar (\n\tsignal = make(chan struct{})\n)\n\nfunc foo(n int) {\n\tfor i := 0; i < n; i++ {\n\t\tfmt.Print | npx88 | NORMAL | 2019-10-10T16:48:49.309694+00:00 | 2019-10-10T16:48:49.309743+00:00 | 271 | false | ```go\t\npackage main\n\nimport (\n\t"fmt"\n\t"time"\n)\n\nvar (\n\tsignal = make(chan struct{})\n)\n\nfunc foo(n int) {\n\tfor i := 0; i < n; i++ {\n\t\tfmt.Print("foo")\n\t\tsignal <- struct{}{}\n\t}\n}\n\nfunc bar(n int) {\n\tfor i := 0; i < n; i++ {\n\t\t<-signal\n\t\tfmt.Print("bar")\n\n\t}\n}\n\nfunc main() {\n\t... | 2 | 1 | [] | 2 |
print-foobar-alternately | Java Solution beats 100% time and space | java-solution-beats-100-time-and-space-b-00mk | Make each thread wait on a mutex using the state of a counter.\n\nclass FooBar {\n private int n; \n volatile int ctr;\n \n Object mutex;\n \n | shreytrivedi007 | NORMAL | 2019-09-13T01:03:23.589254+00:00 | 2019-09-13T01:05:03.364568+00:00 | 555 | false | Make each thread wait on a mutex using the state of a counter.\n```\nclass FooBar {\n private int n; \n volatile int ctr;\n \n Object mutex;\n \n public FooBar(int n) {\n this.n = n;\n ctr=1;\n \n mutex = new Object();\n }\n\n public void foo(Runnable printFoo) thr... | 2 | 0 | ['Java'] | 1 |
print-foobar-alternately | 0 ms 🔥Intuitive | Two Mutex Only | C++ | C | 0-ms-beats-10000-users-two-mutex-only-c-6tb4a | 🚀 Approach
We start with two mutexes (m1 and m2).
m1 is open (unlocked) because foo gets to go first.
m2 is locked because bar needs to wait its turn.
Thre | nitinranjan | NORMAL | 2025-01-28T21:22:09.956454+00:00 | 2025-01-28T21:25:43.774678+00:00 | 546 | false | # 🚀 Approach
1. We start with two mutexes (`m1` and `m2`).
- `m1` is open (unlocked) because `foo` gets to go first.
- `m2` is locked because `bar` needs to wait its turn.
2. Thread 1 (`foo`):
- It grabs `m1`, prints `"foo"`, and then signals (unlocks) `m2` to let `bar` take over.
3. Thread 2 (`bar`):
... | 1 | 0 | ['C', 'Concurrency', 'C++'] | 1 |
print-foobar-alternately | Intuitive | Simple | intuitive-simple-by-richardleee-9n4l | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | RichardLeee | NORMAL | 2024-12-29T22:13:19.619568+00:00 | 2024-12-29T22:13:19.619568+00:00 | 336 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 1 | 0 | ['Java'] | 0 |
print-foobar-alternately | Go - 3 solutions (Channels, Mutex and Atomic Operations) | go-3-solutions-channels-mutex-and-atomic-9fot | Channels vs Mutex vs Atomic Operations\n\nChannels : When goroutines needs to communicate.\n\nMutex : When goroutines needs shared variable.\n\nAtomic operation | fnf47 | NORMAL | 2024-11-14T16:11:34.070892+00:00 | 2024-11-14T16:11:34.070935+00:00 | 202 | false | # Channels vs Mutex vs Atomic Operations\n\nChannels : When goroutines needs to communicate.\n\nMutex : When goroutines needs shared variable.\n\nAtomic operations : When it\'s known that lock will be quickly released and acquired. So your process remains in CPU (wasting CPU cycle) instead of getting removed back and f... | 1 | 0 | ['Concurrency', 'Go'] | 1 |
print-foobar-alternately | C++ CV + Mutex | c-cv-mutex-by-breakbadsp-dm0v | \n\n# Code\n\nclass FooBar {\nprivate:\n int n;\n\npublic:\n FooBar(int n) {\n this->n = n;\n }\n\n void foo(function<void()> printFoo) {\n | breakbadsp | NORMAL | 2024-07-16T11:54:54.305763+00:00 | 2024-07-16T11:54:54.305794+00:00 | 547 | false | \n\n# Code\n```\nclass FooBar {\nprivate:\n int n;\n\npublic:\n FooBar(int n) {\n this->n = n;\n }\n\n void foo(function<void()> printFoo) {\n \n for (int i = 0; i < n; i++) {\n {\n std::unique_lock lg(mut);\n while(foo_done)\n ... | 1 | 0 | ['C++'] | 1 |
print-foobar-alternately | Solution | solution-by-suraj_singh_rajput-2rdx | 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 | Suraj_singh_rajput | NORMAL | 2023-12-23T14:52:34.567612+00:00 | 2023-12-23T14:52:34.567629+00:00 | 2 | false | # 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)$$ --... | 1 | 0 | ['Java'] | 0 |
print-foobar-alternately | Solution | solution-by-suraj_singh_rajput-8y56 | 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 | Suraj_singh_rajput | NORMAL | 2023-12-22T07:32:00.731235+00:00 | 2023-12-22T07:32:00.731272+00:00 | 3 | false | # 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)$$ --... | 1 | 0 | ['Java'] | 0 |
print-foobar-alternately | ✅Using Mutex ✅|| Lock || 🏆Easy to understand 🏆 | using-mutex-lock-easy-to-understand-by-r-37l8 | \n\n# Code\n\nclass FooBar {\nprivate:\n int n;\n std:: mutex m ;\n std:: condition_variable cv;\n bool turn;\npublic:\n FooBar(int n) {\n | rajh_3399 | NORMAL | 2023-10-12T10:56:14.018416+00:00 | 2023-10-12T10:56:14.018443+00:00 | 886 | false | \n\n# Code\n```\nclass FooBar {\nprivate:\n int n;\n std:: mutex m ;\n std:: condition_variable cv;\n bool turn;\npublic:\n FooBar(int n) {\n this->n = n;\n turn = 0 ;\n }\n\n void foo(function<void()> printFoo) {\n \n for (int i = 0; i < n; i++) {\n std:: uni... | 1 | 0 | ['C++'] | 1 |
print-foobar-alternately | [JAVA] CAS + volatile | java-cas-volatile-by-datou12138-rycp | Intuition\n Describe your first thoughts on how to solve this problem. \nUsing CAS + volatile\n# Approach\n Describe your approach to solving the problem. \n\n# | datou12138 | NORMAL | 2023-08-30T12:30:57.168701+00:00 | 2023-08-30T12:30:57.168741+00:00 | 6 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUsing CAS + volatile\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 he... | 1 | 0 | ['Java'] | 0 |
print-foobar-alternately | C# solution using Monitor.Wait() and Monitor.PulseAll() | c-solution-using-monitorwait-and-monitor-s107 | Intuition\nThread Signalling\n\n# Code\n\nusing System.Threading;\n\npublic class FooBar {\n private int n;\n\n private readonly object mutex = new object | deto | NORMAL | 2023-07-06T11:21:53.044177+00:00 | 2023-07-06T11:21:53.044205+00:00 | 225 | false | # Intuition\nThread Signalling\n\n# Code\n```\nusing System.Threading;\n\npublic class FooBar {\n private int n;\n\n private readonly object mutex = new object();\n private bool foo = false;\n private bool bar = true;\n\n public FooBar(int n) {\n this.n = n;\n }\n\n public void Foo(Action pr... | 1 | 0 | ['C#'] | 0 |
print-foobar-alternately | C++ | 2 Codes | Trick | conditional variable | | c-2-codes-trick-conditional-variable-by-mzgs1 | Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition is to use a condition_variable and a boolean flag (isFooTurn) to determin | animeshgurjar | NORMAL | 2023-06-15T17:55:07.439813+00:00 | 2023-06-30T12:31:53.227087+00:00 | 1,187 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition is to use a **condition_variable** and a **boolean** **flag** (**isFooTurn**) to determine which thread\'s turn it is. The threads acquire a unique lock on a mutex before performing their operations. If it\'s not their turn,... | 1 | 0 | ['C++'] | 3 |
print-foobar-alternately | C#, Semaphores, faster than 100% | c-semaphores-faster-than-100-by-bikbov-3ejs | Code\n\nusing System.Threading;\npublic class FooBar \n{\n private Semaphore fooSem, barSem; \n private int n;\n public FooBar(int n) \n {\n | bikbov | NORMAL | 2023-01-22T19:38:37.747304+00:00 | 2023-01-22T19:38:37.747336+00:00 | 792 | false | # Code\n```\nusing System.Threading;\npublic class FooBar \n{\n private Semaphore fooSem, barSem; \n private int n;\n public FooBar(int n) \n {\n this.n = n;\n fooSem = new Semaphore(1, 1);\n barSem = new Semaphore(0, 1);\n }\n\n public void Foo(Action printFoo) \n {\n f... | 1 | 0 | ['C#'] | 1 |
print-foobar-alternately | C++ (atomic bool w/ yield) | c-atomic-bool-w-yield-by-dakre-e336 | \nclass FooBar {\nprivate:\n atomic<bool> alt;\n int n;\n\npublic:\n FooBar(int n) {\n this->n = n;\n alt = true;\n }\n\n void foo( | dakre | NORMAL | 2022-08-02T15:42:36.606534+00:00 | 2022-08-02T15:42:36.606565+00:00 | 347 | false | ```\nclass FooBar {\nprivate:\n atomic<bool> alt;\n int n;\n\npublic:\n FooBar(int n) {\n this->n = n;\n alt = true;\n }\n\n void foo(function<void()> printFoo) {\n for (int i = 0; i < n; i++) {\n while (!alt) this_thread::yield();\n printFoo();\n alt... | 1 | 0 | ['C'] | 0 |
print-foobar-alternately | java solution using concurrency | java-solution-using-concurrency-by-sachi-51oe | class FooBar {\n private int n;\n Semaphore foo;\n Semaphore bar;\n public FooBar(int n) {\n this.n = n;\n foo=new Semaphore(1);\n | sachinsingh1451 | NORMAL | 2022-05-28T10:02:56.259347+00:00 | 2022-05-28T10:02:56.259390+00:00 | 233 | false | class FooBar {\n private int n;\n Semaphore foo;\n Semaphore bar;\n public FooBar(int n) {\n this.n = n;\n foo=new Semaphore(1);\n bar =new Semaphore (0);\n }\n\n public void foo(Runnable printFoo) throws InterruptedException {\n \n for (int i = 0; i < n; i++) {\n... | 1 | 0 | ['Java'] | 0 |
print-foobar-alternately | C++ || SEMAPHORE || EXTREMELY SHORT ANSWER || EXPLANATION GIVEN | c-semaphore-extremely-short-answer-expla-ej3x | \n#include <semaphore.h>\nclass FooBar {\nprivate:\n int n;\n sem_t f;\n sem_t b;\n\npublic:\n FooBar(int n) {\n this->n = n;\n sem_in | AlexHewJW | NORMAL | 2022-05-23T15:57:25.938076+00:00 | 2022-05-23T15:57:25.938104+00:00 | 160 | false | ```\n#include <semaphore.h>\nclass FooBar {\nprivate:\n int n;\n sem_t f;\n sem_t b;\n\npublic:\n FooBar(int n) {\n this->n = n;\n sem_init(&f, 0, 1); //foo goes first, so this is set to true\n sem_init(&b, 0, 0); //bar comes later, so init as false.\n }\n\n void foo(function<void... | 1 | 0 | ['C'] | 0 |
minimum-score-triangulation-of-polygon | C++ with picture | c-with-picture-by-votrubac-08h6 | Intuition\nIf we pick a side of our polygon, it can form n - 2 triangles. Each such triangle breaks our polygon into 2 sub-polygons. We can analyze n - 2 triang | votrubac | NORMAL | 2019-05-05T04:54:33.525793+00:00 | 2022-01-03T17:11:42.571742+00:00 | 22,500 | false | # Intuition\nIf we pick a side of our polygon, it can form ```n - 2``` triangles. Each such triangle breaks our polygon into 2 sub-polygons. We can analyze ```n - 2``` triangles, and get the minimum score for sub-polygons using the recursion.\n\nT... | 611 | 0 | [] | 56 |
minimum-score-triangulation-of-polygon | Python In-depth Explanation DP For Beginners | python-in-depth-explanation-dp-for-begin-gur8 | First, I feel like this was a hard problem. This problem is very similar to Burst Ballons (https://leetcode.com/problems/burst-balloons/), which is labeled as a | rosemelon | NORMAL | 2019-05-18T02:11:36.797672+00:00 | 2019-05-18T02:28:01.977453+00:00 | 5,954 | false | First, I feel like this was a hard problem. This problem is very similar to Burst Ballons (https://leetcode.com/problems/burst-balloons/), which is labeled as a hard. They are almost the exact same problem, so I don\'t understand why this is medium. Now, moving on to the explanation. \n\nThe problem asks what is the mi... | 188 | 1 | [] | 12 |
minimum-score-triangulation-of-polygon | [Java/C++/Python] DP | javacpython-dp-by-lee215-z29z | Intuition\n\nThe connected two points in polygon shares one common edge,\nthese two points must be one and only one triangles.\n\n\n\n## Explanation\n\ndp[i][j] | lee215 | NORMAL | 2019-05-05T04:10:01.628050+00:00 | 2019-05-06T02:17:06.750368+00:00 | 17,140 | false | ## **Intuition**\n\nThe connected two points in polygon shares one common edge,\nthese two points must be one and only one triangles.\n\n<br>\n\n## **Explanation**\n\n`dp[i][j]` means the minimum score to triangulate `A[i] ~ A[j]`,\nwhile there is edge connect `A[i]` and `A[j]`.\n\nWe enumerate all points `A[k]` with `... | 186 | 8 | [] | 25 |
minimum-score-triangulation-of-polygon | [C++/Python] O(n^3) DP explanation + diagrams | cpython-on3-dp-explanation-diagrams-by-k-pvxg | Explanation: \n\nIn any valid triangulation, every edge of the polygon must be a part of exactly one triangle:\n If it werent part of a triangle, the polygon wo | karutz | NORMAL | 2019-05-05T04:57:00.219269+00:00 | 2020-09-10T12:26:26.974406+00:00 | 4,891 | false | **Explanation**: \n\nIn any valid triangulation, every edge of the polygon must be a part of exactly one triangle:\n* If it werent part of a triangle, the polygon wouldnt be fully triangulated.\n* If it were part of more than one triangle, those triangles would have to be overlapping.\n\nConsider the edge between the f... | 77 | 1 | [] | 9 |
minimum-score-triangulation-of-polygon | Problem Pattern | Matrix Chain Multiplication | problem-pattern-matrix-chain-multiplicat-jaod | If you have seen Balloon Burst and this problem, not able to find the solution .\nJust read the following pattern carefully .\n\nThese both are the child proble | reverenc | NORMAL | 2021-10-11T05:53:54.682698+00:00 | 2021-10-11T06:40:00.581472+00:00 | 40,027 | false | If you have seen Balloon Burst and this problem, not able to find the solution .\nJust read the following pattern carefully .\n\nThese both are the child problem of MCM .\n\nProblem : If a chain of matrices is given, we have to find the minimum number of the correct sequence of matrices to multiply.\n\n\nThe problem is... | 68 | 0 | [] | 4 |
minimum-score-triangulation-of-polygon | Intuitive Java Solution With Explanation | intuitive-java-solution-with-explanation-obzk | Few observations needed before discussing the approach.\n First observe that we only stop drawing inner edges, when we cannot form any more triangels. Obviously | naveen_kothamasu | NORMAL | 2019-08-21T04:07:45.032609+00:00 | 2019-08-21T04:08:44.364189+00:00 | 1,981 | false | Few observations needed before discussing the approach.\n* First observe that we only stop drawing inner edges, when we cannot form any more triangels. Obviously every such triangle has 1 edge from the given polygon or 2 edges. That means, the stop condition is all edges from the polygon are covered.\n* Also observe t... | 39 | 0 | [] | 6 |
minimum-score-triangulation-of-polygon | Dynamic programming - clear solution | dynamic-programming-clear-solution-by-ha-1uan | Intuition and Algorithm\nThis problem is similar to: https://www.geeksforgeeks.org/minimum-cost-polygon-triangulation/\nWe can solved it with Dynamic programmin | hamlet_fiis | NORMAL | 2019-05-05T04:00:36.732804+00:00 | 2019-05-08T02:32:34.702766+00:00 | 2,970 | false | **Intuition and Algorithm**\nThis problem is similar to: https://www.geeksforgeeks.org/minimum-cost-polygon-triangulation/\nWe can solved it with Dynamic programming.\n<code>DP(pos1,pos2)</code> = min cost of triangulation of vertices from pos1 to pos2\n<code>if (pos2-pos1<2) return 0;</code> //its not possible to get ... | 25 | 3 | [] | 3 |
minimum-score-triangulation-of-polygon | One More Graphical Explanation | one-more-graphical-explanation-by-coolgk-8eku | Firstly, we know the answer is\n\nanswer = min(combination 1, combination 2, ..., combination n)\n\nthe first step is calculate the first combination (triangle | coolgk | NORMAL | 2020-10-29T23:54:41.723982+00:00 | 2020-10-30T00:09:38.812170+00:00 | 805 | false | Firstly, we know the answer is\n\n**answer = min(combination 1, combination 2, ..., combination n)**\n\nthe first step is calculate the first combination (triangle combinations/triangulation)\n\n**If we pick an edge, ANY edge, this edge MUST be used by all the possible combinations/triangulations, because all combinati... | 19 | 0 | [] | 3 |
minimum-score-triangulation-of-polygon | Java memoization DP solution | java-memoization-dp-solution-by-tankztc-6wpp | \nclass Solution {\n public int minScoreTriangulation(int[] A) {\n return dfs(A, 0, A.length - 1, new Integer[A.length][A.length]);\n }\n \n | tankztc | NORMAL | 2019-05-05T05:14:10.966383+00:00 | 2019-05-11T18:55:27.505879+00:00 | 1,255 | false | ```\nclass Solution {\n public int minScoreTriangulation(int[] A) {\n return dfs(A, 0, A.length - 1, new Integer[A.length][A.length]);\n }\n \n int dfs(int[] A, int i, int j, Integer[][] memo) {\n if (j < i + 2) return 0; // base case: you can\'t form a triangle with less than 3 points\n ... | 16 | 2 | [] | 2 |
minimum-score-triangulation-of-polygon | C++ simple memoization | c-simple-memoization-by-deleted_user-6s3a | \nclass Solution {\npublic:\n int minimum_area(vector<int> &A,int si,int ei,vector<vector<int>>&dp){\n if(si+1==ei) return 0;\n \n if(dp | deleted_user | NORMAL | 2020-08-10T14:18:20.647729+00:00 | 2020-08-10T14:18:20.647776+00:00 | 1,692 | false | ```\nclass Solution {\npublic:\n int minimum_area(vector<int> &A,int si,int ei,vector<vector<int>>&dp){\n if(si+1==ei) return 0;\n \n if(dp[si][ei]!=0) return dp[si][ei];\n \n int ans=INT_MAX;\n for(int cut=si+1;cut<ei;cut++){\n \n int leftAns=minimum_a... | 14 | 0 | ['Dynamic Programming', 'Memoization', 'C', 'C++'] | 1 |
minimum-score-triangulation-of-polygon | EASY | C++ | Matrix Chain Multiplication | Memoization | Aditya Verma Solution | easy-c-matrix-chain-multiplication-memoi-1ei3 | Kindly Upvote if you find this useful\n\nThis question is smiliar to Matrix Chain Multiplication\n\nclass Solution {\npublic:\n int dp[1002][1002];\n int | stepheneliazer18 | NORMAL | 2022-01-16T16:02:22.621171+00:00 | 2022-01-16T16:02:22.621215+00:00 | 482 | false | **Kindly Upvote if you find this useful**\n\nThis question is smiliar to Matrix Chain Multiplication\n```\nclass Solution {\npublic:\n int dp[1002][1002];\n int solve(vector<int> arr, int i,int j){\n if(i>=j) return 0;\n \n if(dp[i][j]!=-1) return dp[i][j];\n \n int ans = INT_M... | 8 | 0 | [] | 0 |
minimum-score-triangulation-of-polygon | Java - Recursive -> Memoization -> 2D Bottom Up | java-recursive-memoization-2d-bottom-up-rky3i | \n- If we currently have a range of numbers from indices \'i -> k\'\n\t- Then the two numbers at \'i\' and \'k\' can be the corners of two vertices\n\t\t- Then | david2999999 | NORMAL | 2020-09-30T16:19:30.389908+00:00 | 2020-09-30T16:19:30.389993+00:00 | 575 | false | ```\n- If we currently have a range of numbers from indices \'i -> k\'\n\t- Then the two numbers at \'i\' and \'k\' can be the corners of two vertices\n\t\t- Then the last corner \'j\' can be a number from indices \'i + 1 -> k - 1\'\n\t- We can then continue to build triangles using the indices from \'i -> j\' and \'j ... | 8 | 0 | [] | 1 |
minimum-score-triangulation-of-polygon | 2 Approaches || Recursion+Memoization(Top-Down) & Tabulation(Bottom-Up) || Beats 100% | 2-approaches-recursionmemoizationtop-dow-ahb4 | Intuition: \nThe problem is a classic example of dynamic programming on sequences. The key observation is that any triangulation of the polygon can be divided i | prayashbhuria931 | NORMAL | 2024-08-05T17:06:25.220782+00:00 | 2024-08-17T06:09:49.976985+00:00 | 311 | false | # Intuition: \nThe problem is a classic example of dynamic programming on sequences. The key observation is that any triangulation of the polygon can be divided into smaller triangulations. By solving smaller subproblems and using their results to build up the solution for larger problems, we can efficiently find the m... | 5 | 0 | ['Recursion', 'Memoization', 'C++'] | 2 |
minimum-score-triangulation-of-polygon | Easy C++ Solution using Memoization | easy-c-solution-using-memoization-by-200-3e5w | \n# Approach\nThe problem involves finding the minimum score for triangulating a polygon formed by the given values. Each value represents the vertices of the p | 2005115 | NORMAL | 2023-07-11T14:19:19.021860+00:00 | 2023-07-11T14:19:19.021877+00:00 | 357 | false | \n# Approach\nThe problem involves finding the minimum score for triangulating a polygon formed by the given values. Each value represents the vertices of the polygon.\n\nDefine a helper function solve that takes the vector of values, the indices i and j representing the range of values to consider, and the memoization... | 5 | 0 | ['Dynamic Programming', 'C++'] | 0 |
minimum-score-triangulation-of-polygon | [C++] Clean and concise DP solution | O(n^3) | c-clean-and-concise-dp-solution-on3-by-s-bfll | Please upvote if it helps!\n\nclass Solution {\npublic:\n int func(vector<int>& a, int i, int j, vector<vector<int>>& dp)\n {\n if(j<=(i+1))\n | somurogers | NORMAL | 2021-06-23T03:11:22.812243+00:00 | 2021-06-23T03:11:22.812291+00:00 | 603 | false | Please upvote if it helps!\n```\nclass Solution {\npublic:\n int func(vector<int>& a, int i, int j, vector<vector<int>>& dp)\n {\n if(j<=(i+1))\n return 0;\n if(dp[i][j]!=-1)\n return dp[i][j];\n \n int ans=INT_MAX;\n for(int k=i+1;k<j;k++)\n {\n ... | 5 | 0 | ['Dynamic Programming', 'C', 'C++'] | 1 |
minimum-score-triangulation-of-polygon | Java | Recursive | Memoization | Divde and conquer | (2 ms) with clear explanation. | java-recursive-memoization-divde-and-con-wlw7 | class Solution {\n \n Integer[][] dp ;\n \n public int minScoreTriangulation(int[] A) {\n dp= new Integer[A.length][A.length];\n retur | saikumar112 | NORMAL | 2021-02-06T23:22:44.043178+00:00 | 2021-02-06T23:22:44.043211+00:00 | 306 | false | class Solution {\n \n Integer[][] dp ;\n \n public int minScoreTriangulation(int[] A) {\n dp= new Integer[A.length][A.length];\n return helper(A, 0, A.length-1);\n }\n \n private int helper(int[] nums, int left, int right){\n \n // base condition when we have sides less ... | 5 | 0 | [] | 0 |
minimum-score-triangulation-of-polygon | Java Top Down solution, Beats 99.56% | java-top-down-solution-beats-9956-by-iro-t2t7 | This is an example of dynamic programming problem of finding the answer for left part and right part and then combining the solutions.\n\nhttps://leetcode.com/d | ironman04 | NORMAL | 2020-04-26T00:33:01.472051+00:00 | 2020-04-26T00:36:03.840703+00:00 | 353 | false | This is an example of dynamic programming problem of finding the answer for left part and right part and then combining the solutions.\n\nhttps://leetcode.com/discuss/general-discussion/458695/dynamic-programming-patterns#Merging-Intervals\n\n```\nclass Solution {\n public int minScoreTriangulation(int[] A) {\n ... | 5 | 0 | [] | 0 |
minimum-score-triangulation-of-polygon | 3 Methods || easy to understand || 2D DP | 3-methods-easy-to-understand-2d-dp-by-sa-ca1y | PLEASE READ FULL EXPLANATION AND UPVOTE IF YOU UNDERSTAND THE SOLUTIONS\n# Intuition\nIn the question it is given that the array values is in clockwise directio | sanjay_soni | NORMAL | 2023-03-08T06:29:51.310754+00:00 | 2023-03-08T06:29:51.310795+00:00 | 607 | false | PLEASE READ FULL EXPLANATION AND UPVOTE IF YOU UNDERSTAND THE SOLUTIONS\n# Intuition\nIn the question it is given that the array values is in clockwise direction it means first element and last element are adjacent\n\n# Approach\n1. take first = i and last = j index as base\n2. now we have last - first indexes left for... | 4 | 0 | ['C++'] | 1 |
minimum-score-triangulation-of-polygon | Java - Easiest Solution [both approaches ] | java-easiest-solution-both-approaches-by-rykc | Hi Family,\n\nI put the proper commen in the code Please have a look and if you liked the code Please upvote.\n\n\n// Top To Bottom\n\n\nclass Solution {\n i | singhmohit9718 | NORMAL | 2022-09-16T14:18:06.723807+00:00 | 2022-09-16T14:34:28.429172+00:00 | 588 | false | Hi Family,\n\nI put the proper commen in the code Please have a look and if you liked the code Please upvote.\n\n\n// Top To Bottom\n```\n\nclass Solution {\n int dp[][];\n public int minScoreTriangulation(int[] values) {\n int n = values.length;\n dp = new int[n][n];\n for (int i=0;i<n;i++) ... | 4 | 0 | ['Dynamic Programming', 'Memoization', 'Java'] | 0 |
minimum-score-triangulation-of-polygon | C++||DP||MCM-Pattern||Easy&Understandale | cdpmcm-patterneasyunderstandale-by-princ-2y45 | \nclass Solution {\npublic:\n int solve(vector<int>&values,int i,int j,vector<vector<int>>&dp){\n if(i>=j)\n return 0;\n if(dp[i][j] | prince_725 | NORMAL | 2022-06-26T11:22:09.872085+00:00 | 2022-06-26T11:22:09.872126+00:00 | 644 | false | ```\nclass Solution {\npublic:\n int solve(vector<int>&values,int i,int j,vector<vector<int>>&dp){\n if(i>=j)\n return 0;\n if(dp[i][j]!=-1){\n return dp[i][j];\n } \n int minm = INT_MAX;\n \n for(int k=i;k<j;k++){\n int left = (dp[i][k]!=-1)... | 4 | 0 | ['Dynamic Programming', 'C'] | 0 |
minimum-score-triangulation-of-polygon | C++ Solution || Gap Strategy with explanation | c-solution-gap-strategy-with-explanation-0n9l | In order to solve this problem we will use the Gap Strategy of dynamic programming.\n1. In this strategy we use a 2D array of n x n and then we assign some mean | thanoschild | NORMAL | 2022-02-25T09:11:25.161952+00:00 | 2022-02-25T09:11:25.161979+00:00 | 356 | false | In order to solve this problem we will use the Gap Strategy of dynamic programming.\n1. In this strategy we use a 2D array of n x n and then we assign some meaning to the cells of the 2D array.\n2. Then we fill the matrix diagonally, moving from smaller problems to the bigger problem.\n\n**Approach**\n\n>**->** In the ... | 4 | 0 | ['Dynamic Programming', 'C'] | 0 |
minimum-score-triangulation-of-polygon | Java+DP(MCM Variation)+ with Time-Space Analysis | javadpmcm-variation-with-time-space-anal-1rvd | Time Complexity: O(n^3) since we use 2 nested for loops enclosed with a k(i -> j) loop for every dp[i][j]. \nSpace Complexity: O(n^2) Since, we are using an n | ikanishk__ | NORMAL | 2022-02-19T14:20:11.280718+00:00 | 2022-04-05T08:33:55.168215+00:00 | 568 | false | **Time Complexity:** O(n^3) since we use 2 nested for loops enclosed with a k(i -> j) loop for every dp[i][j]. \n**Space Complexity:** O(n^2) Since, we are using an n * n matrix.\n```\nclass Solution {\n public int minScoreTriangulation(int[] arr) {\n int[][] dp = new int[arr.length][arr.length];\n f... | 4 | 0 | ['Dynamic Programming', 'Java'] | 1 |
minimum-score-triangulation-of-polygon | MCM Pattern | Little Variation | C++ Concise Solution | mcm-pattern-little-variation-c-concise-s-d870 | class Solution {\npublic:\n \n int helper(vector& values, int i,int j,vector>&dp)\n {\n // initialization \n if(i>=j) return 0;\n | thechildwholovesyou | NORMAL | 2021-09-25T04:56:46.350570+00:00 | 2021-09-25T04:56:46.350603+00:00 | 162 | false | class Solution {\npublic:\n \n int helper(vector<int>& values, int i,int j,vector<vector<int>>&dp)\n {\n // initialization \n if(i>=j) return 0;\n if(i+1==j) return 0; // when we cannot make a triange \n // imagine the case when index paased are 0,1 or 2,3 and so on \n \n ... | 4 | 0 | [] | 1 |
minimum-score-triangulation-of-polygon | The tricky point: find a way to divide the problem | the-tricky-point-find-a-way-to-divide-th-1966 | Foremost, I want to say this problem is not good for interview because it depends a tricky insights about how to divide the problem. For the record, I\'ve never | luanjunyi | NORMAL | 2021-01-19T20:31:23.161530+00:00 | 2021-01-19T20:33:11.548281+00:00 | 182 | false | Foremost, I want to say this problem is not good for interview because it depends a tricky insights about how to divide the problem. For the record, I\'ve never encountered this hard problems in any top tier company(FB, Google) from L3 to L6 interviews(coding is hardest at L3 and L4).\n\nThere are multiple ways to divi... | 4 | 0 | [] | 1 |
minimum-score-triangulation-of-polygon | C++ Top Down Faster than 100% | c-top-down-faster-than-100-by-guptaprati-44v2 | \nclass Solution {\npublic:\n int dp[105][105];\n int minScore(vector<int>& A, int i, int k) {\n if( k < i + 2)\n return 0;\n \n | guptapratik104 | NORMAL | 2020-12-03T17:45:16.837582+00:00 | 2020-12-03T17:45:16.837628+00:00 | 224 | false | ```\nclass Solution {\npublic:\n int dp[105][105];\n int minScore(vector<int>& A, int i, int k) {\n if( k < i + 2)\n return 0;\n \n if(dp[i][k] != -1)\n return dp[i][k];\n \n int minm = INT_MAX;\n \n for(int j = i+1; j < k; j++) {\n ... | 4 | 0 | [] | 0 |
minimum-score-triangulation-of-polygon | Top-down and bottom-up DP | top-down-and-bottom-up-dp-by-xing_yi-5h8k | \nclass Solution:\n def minScoreTriangulation(self, A: List[int]) -> int:\n L = len(A)\n memo = []\n for i in range(L):\n mem | xing_yi | NORMAL | 2020-09-07T04:54:26.172873+00:00 | 2020-09-07T04:54:26.172917+00:00 | 236 | false | ```\nclass Solution:\n def minScoreTriangulation(self, A: List[int]) -> int:\n L = len(A)\n memo = []\n for i in range(L):\n memo.append([-1 for _ in range(L)])\n \n def dp(start, end):\n if end - start < 2:\n return 0\n if end - ... | 4 | 0 | [] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.