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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
max-increase-to-keep-city-skyline | [c++] very easy soln, beats 98% in runtime | c-very-easy-soln-beats-98-in-runtime-by-q7p92 | Approach\n Describe your approach to solving the problem. \n- Find the row and col maximum for each row and col respectively.\n- In order to keep the skyline un | vanshdhawan60 | NORMAL | 2023-09-16T09:37:31.176560+00:00 | 2023-09-16T09:37:31.176578+00:00 | 13 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\n- Find the row and col maximum for each row and col respectively.\n- In order to keep the skyline unchanged, we set the height of each building such that it doesn\'t exceed the max row/col limit when viewed from either side.\n- To do this, we set its ... | 1 | 0 | ['Array', 'Matrix', 'C++'] | 0 |
max-increase-to-keep-city-skyline | || in java | in-java-by-2manas1-9zuz | 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 | 2manas1 | NORMAL | 2023-08-08T22:23:00.696696+00:00 | 2023-08-08T22:23:00.696766+00:00 | 453 | 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 |
max-increase-to-keep-city-skyline | EASY SOLUTION JAVA || 100% BEATS || GREEDY | easy-solution-java-100-beats-greedy-by-v-o3t0 | Approach\n Describe your approach to solving the problem. \nBy greedy on height, maximum of each row and column are finded and stored on the array rowMax and co | vijayanvishnu | NORMAL | 2023-07-31T13:00:20.737725+00:00 | 2023-07-31T13:00:20.737745+00:00 | 11 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\nBy greedy on height, maximum of each row and column are finded and stored on the array <strong>rowMax</strong> and <strong>colMax</strong>.Difference of minimum(<strong>rowMax</strong>,<strong>colMax</strong>) and current element the maximum possibili... | 1 | 0 | ['Java'] | 0 |
max-increase-to-keep-city-skyline | C++ easy solution beats 93% users | c-easy-solution-beats-93-users-by-mandar-mgg7 | \n\n# Code\n\nclass Solution {\npublic:\n int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {\n int n=grid.size();\n int sum=0;\n | Mandar_0 | NORMAL | 2023-07-24T18:13:14.782416+00:00 | 2023-07-24T18:13:14.782434+00:00 | 12 | false | \n\n# Code\n```\nclass Solution {\npublic:\n int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {\n int n=grid.size();\n int sum=0;\n vector<int> row;\n vector<int> col;\n\n\n\n for(int i=0;i<n;i++){\n int rowMax=INT_MIN;\n for(int j=0;j<n;j++){\n ... | 1 | 0 | ['C++'] | 0 |
max-increase-to-keep-city-skyline | Simple Using Priority Queue With Hindi Explanation|| Max Heap || Beginner Friendly ✅✅🔥 | simple-using-priority-queue-with-hindi-e-xpo3 | Intuition\n Describe your first thoughts on how to solve this problem. \nAgar hum dhyan se dekhen to hume pata chalega ki har element ki final value wahi hogi j | yxsh14 | NORMAL | 2023-07-10T15:02:18.335490+00:00 | 2023-07-10T15:02:18.335511+00:00 | 152 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAgar hum dhyan se dekhen to hume pata chalega ki har element ki final value wahi hogi jo us row & column k maximum elements main comparatively choti value hai. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nHum i... | 1 | 0 | ['Heap (Priority Queue)', 'C++'] | 0 |
max-increase-to-keep-city-skyline | Simple Using Priority Queue With Hindi Explanation|| Max Heap || Beginner Friendly ✅✅🔥 | simple-using-priority-queue-with-hindi-e-x12o | Intuition\n Describe your first thoughts on how to solve this problem. \nAgar hum dhyan se dekhen to hume pata chalega ki har element ki final value wahi hogi j | yxsh14 | NORMAL | 2023-07-10T15:02:16.145335+00:00 | 2023-07-10T15:02:16.145353+00:00 | 52 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAgar hum dhyan se dekhen to hume pata chalega ki har element ki final value wahi hogi jo us row & column k maximum elements main comparatively choti value hai. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nHum i... | 1 | 0 | ['Heap (Priority Queue)', 'C++'] | 0 |
max-increase-to-keep-city-skyline | Easy and Fast Solution ✅ || 0ms - 100% beats ✅ || Fully Explained ✅ | easy-and-fast-solution-0ms-100-beats-ful-7mnc | Approach\n1. Initialize two arrays, rowMax and colMax, of size n to store the maximum height in each row and column, respectively.\n2. Traverse the grid and upd | akobirswe | NORMAL | 2023-07-06T07:45:44.313893+00:00 | 2023-07-06T07:45:44.313918+00:00 | 107 | false | # Approach\n1. Initialize two arrays, `rowMax` and `colMax`, of size `n` to store the maximum height in each row and column, respectively.\n2. Traverse the grid and update the `rowMax` array by finding the maximum height in each row.\n3. Traverse the grid again, but this time update the `colMax` array by finding the ma... | 1 | 0 | ['Array', 'Greedy', 'Matrix', 'Java', 'C#'] | 0 |
max-increase-to-keep-city-skyline | Swift solution O(n^2) | swift-solution-on2-by-user0031y-ew7j | 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 | user0031y | NORMAL | 2023-05-02T16:54:20.705187+00:00 | 2023-05-02T16:54:20.705226+00:00 | 15 | 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 | ['Swift'] | 0 |
max-increase-to-keep-city-skyline | Solution | solution-by-deleted_user-luqy | C++ []\nclass Solution {\npublic:\n int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {\n vector<int>rowMax;\n vector<int>colMax;\n | deleted_user | NORMAL | 2023-05-01T00:09:48.732845+00:00 | 2023-05-01T01:23:08.742312+00:00 | 865 | false | ```C++ []\nclass Solution {\npublic:\n int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {\n vector<int>rowMax;\n vector<int>colMax;\n int maxi = -1;\n for(int i = 0; i<grid.size(); i++){\n maxi = -1;\n for(int j = 0; j < grid.size(); j++){\n ma... | 1 | 0 | ['C++', 'Java', 'Python3'] | 2 |
max-increase-to-keep-city-skyline | simple and clear c++ soulution for beginners | simple-and-clear-c-soulution-for-beginne-1h7g | \n# Code\n\nclass Solution {\npublic:\n int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {\n vector<int> west(grid.size(),0);\n vector< | 7_aditi | NORMAL | 2023-03-15T21:27:31.946813+00:00 | 2023-04-01T08:25:16.929116+00:00 | 16 | false | \n# Code\n```\nclass Solution {\npublic:\n int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {\n vector<int> west(grid.size(),0);\n vector<int> south(grid[0].size(),0);\n int k=0;\n for(int i=0;i<grid.size();i++)\n {\n int max=0;\n for(int j=0;j<grid[0]... | 1 | 0 | ['C++'] | 0 |
max-increase-to-keep-city-skyline | substract every element from min(that row max , that col max) | substract-every-element-from-minthat-row-5239 | \nclass Solution {\npublic:\n int maxIncreaseKeepingSkyline(vector<vector<int>>& g) {\n \n int n = g.size();\n int m = g[0].size();\n | mr_stark | NORMAL | 2023-02-26T09:25:53.745673+00:00 | 2023-02-26T09:25:53.745716+00:00 | 565 | false | ```\nclass Solution {\npublic:\n int maxIncreaseKeepingSkyline(vector<vector<int>>& g) {\n \n int n = g.size();\n int m = g[0].size();\n \n \n vector<int> c(n);\n vector<int> r(m);\n \n \n for(int i = 0;i<n;i++)\n {\n int mx = IN... | 1 | 0 | [] | 0 |
max-increase-to-keep-city-skyline | 807. Max Increase to Keep City Skyline | 807-max-increase-to-keep-city-skyline-by-19wp | Intuition\n Describe your first thoughts on how to solve this problem. \n- First Understand the views from different sides of the nn Matrix.\n- Observe carefull | soumyajit_2021 | NORMAL | 2023-02-22T17:05:53.912623+00:00 | 2023-02-22T18:38:17.292461+00:00 | 20 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- First Understand the views from different sides of the n*n Matrix.\n- Observe carefully that if a small height building placed in between two big height buildings then there is no worries we can increse the height of the small building ... | 1 | 0 | ['Array', 'Greedy', 'Matrix', 'Java'] | 0 |
max-increase-to-keep-city-skyline | [Accepted] Swift | accepted-swift-by-vasilisiniak-98wp | \nclass Solution {\n func maxIncreaseKeepingSkyline(_ grid: [[Int]]) -> Int {\n \n let h = grid.map { $0.max()! }\n let w = grid.indices | vasilisiniak | NORMAL | 2023-02-14T08:32:12.403064+00:00 | 2023-02-14T08:32:12.403125+00:00 | 15 | false | ```\nclass Solution {\n func maxIncreaseKeepingSkyline(_ grid: [[Int]]) -> Int {\n \n let h = grid.map { $0.max()! }\n let w = grid.indices.map { i in grid.map({ $0[i] }).max()! }\n \n return grid\n .indices\n .flatMap { i in grid[i].indices.map { [i, $0] } }\... | 1 | 0 | ['Swift'] | 1 |
max-increase-to-keep-city-skyline | JAVA || Simple Approach || Beats 100% | java-simple-approach-beats-100-by-prince-54n8 | Intuition\n Describe your first thoughts on how to solve this problem. \nFind the maximum height of the buildings for each building in their north and east dire | princeayush04 | NORMAL | 2023-02-06T22:06:55.567175+00:00 | 2023-02-06T22:06:55.567209+00:00 | 20 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFind the maximum height of the buildings for each building in their north and east direction, then take the minimum of these two values and subtract the value with the height of the current building and add the result to the ans.\n\n# App... | 1 | 0 | ['Java'] | 1 |
max-increase-to-keep-city-skyline | C++ Solution #simpleapproach O(2*n^2) | c-solution-simpleapproach-o2n2-by-andy18-nw6f | 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 | andy1810 | NORMAL | 2023-01-26T17:18:41.269098+00:00 | 2023-01-26T17:18:41.269141+00:00 | 17 | 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 O(2*n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, ... | 1 | 0 | ['C++'] | 0 |
max-increase-to-keep-city-skyline | [ JavaScript ] Best Solution | Faster than 97% | javascript-best-solution-faster-than-97-mjy9g | Code\n\n/**\n * @param {number[][]} grid\n * @return {number}\n */\nvar maxIncreaseKeepingSkyline = function(grid) {\n let dupGrid = transposeMatrix(grid);\n | tajnur | NORMAL | 2022-12-31T20:35:22.066763+00:00 | 2022-12-31T20:35:22.066793+00:00 | 425 | false | # Code\n```\n/**\n * @param {number[][]} grid\n * @return {number}\n */\nvar maxIncreaseKeepingSkyline = function(grid) {\n let dupGrid = transposeMatrix(grid);\n let colMax = [];\n let ans = 0;\n \n dupGrid.forEach(row => {\n colMax.push(Math.max(...row));\n });\n\n grid.forEach((row, i) =>... | 1 | 0 | ['JavaScript'] | 0 |
max-increase-to-keep-city-skyline | [Beats 94.75%] Easy, straightforward, beginner solution :) | beats-9475-easy-straightforward-beginner-wkxt | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nh_max is the maximum value for each horizontal column\nv_max is the maxim | TwilightXD | NORMAL | 2022-12-30T15:57:33.613850+00:00 | 2023-01-10T14:59:32.992960+00:00 | 57 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nh_max is the maximum value for each horizontal column\nv_max is the maximum value for each vertical column \n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<... | 1 | 0 | ['Array', 'Greedy', 'Matrix', 'Python3'] | 0 |
max-increase-to-keep-city-skyline | Fastest solution || 0ms Runtime || 100% run speed || JAVA | fastest-solution-0ms-runtime-100-run-spe-bwwd | 133/133 cases passed (0 ms)\n- Your runtime beats 100 % of java submissions\n- Your memory usage beats 83.04 % of java submissions (42.3 MB)\n# Code\n\nclass So | PAPPURAJ | NORMAL | 2022-12-07T09:03:40.263553+00:00 | 2022-12-07T09:03:40.263591+00:00 | 174 | false | - 133/133 cases passed (0 ms)\n- Your runtime beats 100 % of java submissions\n- Your memory usage beats 83.04 % of java submissions (42.3 MB)\n# Code\n```\nclass Solution {\n public int maxIncreaseKeepingSkyline(int[][] grid) {\n int m=grid.length,n=grid[0].length;\n int[] r=new int[m];\n int[]... | 1 | 0 | ['Java'] | 1 |
max-increase-to-keep-city-skyline | CPP SOLUTION || EASY TO UNDERSTAND || SIMPLE AND CLEAN CODE | cpp-solution-easy-to-understand-simple-a-7lib | 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 | Abhishek777888 | NORMAL | 2022-12-07T04:36:11.521660+00:00 | 2022-12-07T04:37:06.006732+00:00 | 976 | 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)$$ -->\n0(N^2)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O... | 1 | 0 | ['C++'] | 1 |
max-increase-to-keep-city-skyline | Very Easy iterative approach || C++ | very-easy-iterative-approach-c-by-sudhan-n3qr | 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 | sudhanwaofficial | NORMAL | 2022-12-06T06:26:55.565737+00:00 | 2022-12-06T06:26:55.565781+00:00 | 229 | 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 | ['C++'] | 0 |
max-increase-to-keep-city-skyline | 100% faster java solution | 100-faster-java-solution-by-sai064-u7zq | Intuition\nTook some time to understand the question. Looked at the example testcase. pictured the matrix as in real life scenario and understood the concept. \ | sai064 | NORMAL | 2022-12-04T08:44:39.537120+00:00 | 2022-12-04T08:44:39.537164+00:00 | 410 | false | # Intuition\nTook some time to understand the question. Looked at the example testcase. pictured the matrix as in real life scenario and understood the concept. \nWe just need to increase the heights of the smaller building from each cardinal direction such that it doesn\'t effect the height view.\n\n# Approach\nWe nee... | 1 | 0 | ['Java'] | 0 |
max-increase-to-keep-city-skyline | Explained O(n * n) time and O(n) space solution in Rust and Java. | explained-on-n-time-and-on-space-solutio-4mgy | The secret here is to iterate through the array:\n1. Suppose we are at a position (i, j)\n2. You must know for this position, the height of the tallest box in i | nisaacdz | NORMAL | 2022-10-19T09:29:26.715610+00:00 | 2022-10-19T09:29:26.715687+00:00 | 95 | false | The secret here is to iterate through the array:\n1. Suppose we are at a position (i, j)\n2. You must know for this position, the height of the tallest box in its column, i, as well as in its row, j.\n3. After you find the tallest in both directions, note the minimum of the two values, c.\n4. The max increment you may ... | 1 | 0 | ['Java', 'Rust'] | 0 |
max-increase-to-keep-city-skyline | 90% FASTER || O(m+n) SPACE || TIME (m*n) || C++ | 90-faster-omn-space-time-mn-c-by-abhay_1-tnzm | \nclass Solution {\npublic:\n int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {\n int n = grid.size(),i,j;\n vector<int> col(n,INT_MIN | abhay_12345 | NORMAL | 2022-09-25T09:58:03.723468+00:00 | 2022-09-25T09:58:03.723510+00:00 | 390 | false | ```\nclass Solution {\npublic:\n int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {\n int n = grid.size(),i,j;\n vector<int> col(n,INT_MIN),row(n,INT_MIN);\n for(i = 0; i < n; i++){\n for(j = 0; j < n; j++){\n row[i] = max(row[i],grid[i][j]);\n co... | 1 | 0 | ['Greedy', 'C', 'Matrix', 'C++'] | 0 |
max-increase-to-keep-city-skyline | ✅C++ Solution | Easy 2 loop approach | c-solution-easy-2-loop-approach-by-devan-gnqm | \nclass Solution {\npublic:\n int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {\n vector<int> max_r,max_c;\n for(int i=0;i<grid.size() | Devanshul | NORMAL | 2022-09-10T17:46:27.385398+00:00 | 2022-09-10T17:46:27.385441+00:00 | 261 | false | ```\nclass Solution {\npublic:\n int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {\n vector<int> max_r,max_c;\n for(int i=0;i<grid.size();i++){\n int m_r=INT_MIN,m_c=INT_MIN;\n for(int j=0;j<grid.size();j++){ \n m_r=max(m_r,grid[i][j]);\n ... | 1 | 0 | ['C', 'C++'] | 0 |
max-increase-to-keep-city-skyline | Simplest C++ solution | simplest-c-solution-by-akriart-i76f | Just find the minimum of the maximum of each row and column and the problem is solved\n\nclass Solution {\npublic:\n int maxIncreaseKeepingSkyline(vector<vec | akriart | NORMAL | 2022-09-07T13:11:15.384087+00:00 | 2022-09-07T13:13:09.055422+00:00 | 154 | false | Just find the minimum of the maximum of each row and column and the problem is solved\n```\nclass Solution {\npublic:\n int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {\n vector<vector<int>>ans(grid.size(),vector<int>(grid[0].size(),0));\n vector<int>rowmax(grid.size(),0);\n vector<int... | 1 | 0 | ['C'] | 0 |
max-increase-to-keep-city-skyline | Rust Solution (slice bad practice!) | rust-solution-slice-bad-practice-by-morr-9eul | rust\n\nimpl Solution {\n pub fn max_increase_keeping_skyline(grid: Vec<Vec<i32>>) -> i32 {\n let mut res = 0;\n let (mut xaxis, mut yaxis) = ( | morris_tai | NORMAL | 2022-09-07T04:59:15.382378+00:00 | 2022-09-07T05:09:49.889900+00:00 | 72 | false | ```rust\n\nimpl Solution {\n pub fn max_increase_keeping_skyline(grid: Vec<Vec<i32>>) -> i32 {\n let mut res = 0;\n let (mut xaxis, mut yaxis) = (vec![], vec![]);\n let length = grid.len();\n \n for x in 0..length {\n for y in 0..grid.len() {\n xaxis.push(... | 1 | 0 | [] | 0 |
max-increase-to-keep-city-skyline | C++ Easy to understand Solution | c-easy-to-understand-solution-by-sumanta-ss51 | \nclass Solution {\npublic:\n int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {\n vector<int> row; // to store max of each row \n vect | SumantaJena | NORMAL | 2022-08-31T07:06:54.243434+00:00 | 2022-08-31T07:06:54.243478+00:00 | 185 | false | ```\nclass Solution {\npublic:\n int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {\n vector<int> row; // to store max of each row \n vector<int> col; // to store max of each col\n int n=grid.size();\n int sum=0;\n // to find the max of each row \n for(int i=0;i<n;i+... | 1 | 0 | ['C'] | 1 |
print-in-order | 5 Python threading solutions (Barrier, Lock, Event, Semaphore, Condition) with explanation | 5-python-threading-solutions-barrier-loc-80yi | \nRaise two barriers. Both wait for two threads to reach them.\n\nFirst thread can print before reaching the first barrier. Second thread can print before reach | mereck | NORMAL | 2019-07-15T19:21:27.141612+00:00 | 2019-08-26T13:47:48.615055+00:00 | 37,035 | false | \nRaise two barriers. Both wait for two threads to reach them.\n\nFirst thread can print before reaching the first barrier. Second thread can print before reaching the second barrier. Third thread can print after the second barrier.\n\n```\nfrom threading import Barrier\n\nclass Foo:\n def __init__(self):\n s... | 749 | 0 | [] | 27 |
print-in-order | [Java] Basic semaphore solution - 8ms, 36MB | java-basic-semaphore-solution-8ms-36mb-b-o4xr | "Semaphore is a bowl of marbles" - Professor Stark\n\n1. Semaphore is a bowl of marbles (or locks in this case). If you need a marble, and there are none, you w | lk00100100 | NORMAL | 2019-07-12T01:19:55.329400+00:00 | 2022-05-15T17:51:46.332889+00:00 | 34,032 | false | "Semaphore is a bowl of marbles" - Professor Stark\n\n1. Semaphore is a bowl of marbles (or locks in this case). If you need a marble, and there are none, you wait. You wait until there is one marble and then you take it. If you release(), you will add one marble to the bowl (from thin air). If you release(100), you wi... | 348 | 1 | [] | 32 |
print-in-order | Java - Synchronized/Lock/Semaphore/Condition Variable | java-synchronizedlocksemaphorecondition-3vcqk | Synchronized Method:\n\nclass Foo {\n private boolean oneDone;\n private boolean twoDone;\n \n public Foo() {\n oneDone = false;\n two | woshilxd912 | NORMAL | 2020-10-14T06:36:10.374868+00:00 | 2020-10-14T18:50:14.552638+00:00 | 17,736 | false | Synchronized Method:\n```\nclass Foo {\n private boolean oneDone;\n private boolean twoDone;\n \n public Foo() {\n oneDone = false;\n twoDone = false;\n }\n\n public synchronized void first(Runnable printFirst) throws InterruptedException {\n printFirst.run();\n oneDone = t... | 206 | 1 | ['Java'] | 17 |
print-in-order | [C++]Why most of the solutions using mutex are wrong+solution | cwhy-most-of-the-solutions-using-mutex-a-hvt6 | I actually started learning about threads recently only and even my first attempt was to take 2 mutex and lock/unlock them in order desired by us. That solution | dev1988 | NORMAL | 2019-07-25T14:23:39.718883+00:00 | 2019-07-25T14:23:39.718918+00:00 | 25,158 | false | I actually started learning about threads recently only and even my first attempt was to take 2 mutex and lock/unlock them in order desired by us. That solution is actually accepted in leetcode.\n\nHowever, while researching for difference between mutex and conditional_variable usage, i realised that the way mutex are ... | 174 | 2 | ['C'] | 26 |
print-in-order | [C++] Using std::promise | c-using-stdpromise-by-mhelvens-bqgp | C++\nclass Foo {\nprivate:\n std::promise<void> p1;\n std::promise<void> p2;\n\npublic:\n void first(function<void()> printFirst) {\n printFirst();\n p | mhelvens | NORMAL | 2019-07-12T19:17:25.860223+00:00 | 2019-07-12T19:17:25.860279+00:00 | 7,250 | false | ```C++\nclass Foo {\nprivate:\n std::promise<void> p1;\n std::promise<void> p2;\n\npublic:\n void first(function<void()> printFirst) {\n printFirst();\n p1.set_value();\n }\n\n void second(function<void()> printSecond) {\n p1.get_future().wait();\n printSecond();\n p2.set_value();\n }\n\n void thi... | 86 | 1 | ['C'] | 5 |
print-in-order | Python 3 - Semaphore | python-3-semaphore-by-awice-hzdq | \nfrom threading import Semaphore\n\nclass Foo:\n def __init__(self):\n self.two = Semaphore(0)\n self.three = Semaphore(0)\n\n def first(se | awice | NORMAL | 2019-07-12T20:57:44.266277+00:00 | 2019-07-12T20:57:44.267100+00:00 | 6,202 | false | ```\nfrom threading import Semaphore\n\nclass Foo:\n def __init__(self):\n self.two = Semaphore(0)\n self.three = Semaphore(0)\n\n def first(self, printFirst):\n printFirst()\n self.two.release()\n\n def second(self, printSecond):\n with self.two:\n printSecond()\n... | 42 | 0 | [] | 2 |
print-in-order | Python 3 | 56ms | Lock vs Event | python-3-56ms-lock-vs-event-by-andnik-ptz8 | The fastest solution for the problem is to use Lock mechanism. See the following code with comments:\npython3\nimport threading\n\nclass Foo:\n def __init__( | andnik | NORMAL | 2019-09-30T22:35:34.693027+00:00 | 2019-09-30T22:35:34.693069+00:00 | 4,514 | false | The fastest solution for the problem is to use Lock mechanism. See the following code with comments:\n```python3\nimport threading\n\nclass Foo:\n def __init__(self):\n\t\t# create lock to control sequence between first and second functions\n self.lock1 = threading.Lock()\n\t\tself.lock1.acquire()\n\t\t\n\t\t... | 39 | 0 | ['Python', 'Python3'] | 5 |
print-in-order | Java solution beats 100% in both time and space | java-solution-beats-100-in-both-time-and-gye6 | \nclass Foo {\n \n private volatile boolean onePrinted;\n private volatile boolean twoPrinted;\n\n public Foo() {\n onePrinted = false;\n | tgaurav10 | NORMAL | 2019-07-12T17:07:20.976210+00:00 | 2019-07-12T17:08:38.865523+00:00 | 8,479 | false | ```\nclass Foo {\n \n private volatile boolean onePrinted;\n private volatile boolean twoPrinted;\n\n public Foo() {\n onePrinted = false;\n twoPrinted = false; \n }\n\n public synchronized void first(Runnable printFirst) throws InterruptedException {\n \n // printFi... | 30 | 1 | [] | 11 |
print-in-order | [Java] Simple CountDownLatch solution | java-simple-countdownlatch-solution-by-m-4p93 | CountDownLatch seems a good fit for this, from java doc " A synchronization aid that allows one or more threads to wait until, a set of operations being perform | mavslee | NORMAL | 2019-07-24T04:08:53.452306+00:00 | 2019-07-24T04:08:53.452369+00:00 | 3,894 | false | CountDownLatch seems a good fit for this, from java doc " A synchronization aid that allows one or more threads to wait until, a set of operations being performed in other threads completes. "\n\n```\nimport java.util.concurrent.CountDownLatch;\n\nclass Foo {\n \n private final CountDownLatch l2;\n private fin... | 27 | 0 | [] | 3 |
print-in-order | C++ 2 mutex | c-2-mutex-by-jysui-g2rw | Idea is grab and release locks in order.\n\nclass Foo { \n mutex m1, m2;\npublic:\n Foo() {\n m1.lock(), m2.lock();\n }\n\n void first(func | jysui | NORMAL | 2019-07-12T03:02:01.905469+00:00 | 2019-07-12T03:02:01.905504+00:00 | 6,261 | false | Idea is grab and release locks in order.\n```\nclass Foo { \n mutex m1, m2;\npublic:\n Foo() {\n m1.lock(), m2.lock();\n }\n\n void first(function<void()> printFirst) {\n // printFirst() outputs "first". Do not change or remove this line.\n printFirst();\n m1.unlock();\n }\n... | 25 | 7 | [] | 4 |
print-in-order | Python Simple Working solution | python-simple-working-solution-by-simonk-t210 | python\nfrom threading import Lock\n\nclass Foo:\n def __init__(self):\n self.lock1 = Lock()\n self.lock2 = Lock()\n \n self.lock | simonkocurek | NORMAL | 2019-07-25T18:13:10.733565+00:00 | 2019-07-25T18:13:47.268397+00:00 | 2,129 | false | ```python\nfrom threading import Lock\n\nclass Foo:\n def __init__(self):\n self.lock1 = Lock()\n self.lock2 = Lock()\n \n self.lock1.acquire()\n self.lock2.acquire()\n\n\n def first(self, printFirst: \'Callable[[], None]\') -> None:\n printFirst()\n \n self... | 22 | 0 | [] | 1 |
print-in-order | Python3 Solution using 5 primitives (lock, semaphore, condition, event, barrier) | python3-solution-using-5-primitives-lock-py7g | Approach \#1. Lock\n- RLock is not suitable in this case. \n\n\n\n# Approach \#2. Semaphore\n- Semaphore(1) is equivalent to Lock()\n\n\n# Approach \#3. Conditi | idontknoooo | NORMAL | 2022-12-18T21:14:03.333747+00:00 | 2022-12-18T21:16:54.360632+00:00 | 2,371 | false | # Approach \\#1. Lock\n- `RLock` is not suitable in this case. \n\n<iframe src="https://leetcode.com/playground/mgRcT2DB/shared" frameBorder="0" width="600" height="400"></iframe>\n\n# Approach \\#2. Semaphore\n- `Semaphore(1)` is equivalent to `Lock()`\n<iframe src="https://leetcode.com/playground/c4bkiTBr/shared" fra... | 20 | 0 | ['Concurrency', 'Python3'] | 3 |
print-in-order | easiest solution || c-plus-plus || easy to understand || without using mutex 😎😎😎 | easiest-solution-c-plus-plus-easy-to-und-q37n | 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 | youdontknow001 | NORMAL | 2022-11-25T05:03:28.273633+00:00 | 2022-11-25T05:03:28.273668+00:00 | 5,380 | 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)$$ --... | 18 | 0 | ['C++'] | 1 |
print-in-order | C | Minimal code | c-minimal-code-by-kmr_shubh-nufl | \ntypedef struct {\n // User defined data is declared here.\n // using \'volatile\' to prevent compiler from playing god and optimize out this critical va | kmr_shubh | NORMAL | 2022-02-20T14:46:36.373139+00:00 | 2024-06-06T04:02:56.167900+00:00 | 1,559 | false | ```\ntypedef struct {\n // User defined data is declared here.\n // using \'volatile\' to prevent compiler from playing god and optimize out this critical variable\n // this is mutex variable, duh \n volatile int turn;\n} Foo;\n\nvoid wait(Foo* obj, int my_turn) {\n // blocking this thread till my turn\n... | 17 | 0 | ['C'] | 1 |
print-in-order | C# Semaphore-based solution | c-semaphore-based-solution-by-vlassov-1bjh | \nusing System.Threading;\n\npublic class Foo {\n private readonly Semaphore first = new Semaphore(1, 1);\n private readonly Semaphore second = new Semaph | vlassov | NORMAL | 2020-09-20T22:42:19.215797+00:00 | 2020-09-20T22:42:19.215828+00:00 | 1,417 | false | ```\nusing System.Threading;\n\npublic class Foo {\n private readonly Semaphore first = new Semaphore(1, 1);\n private readonly Semaphore second = new Semaphore(0, 1);\n private readonly Semaphore third = new Semaphore(0, 1);\n\n public void First(Action printFirst) {\n first.WaitOne();\n prin... | 16 | 1 | [] | 1 |
print-in-order | Clean and easy java solution | clean-and-easy-java-solution-by-andreazu-iq79 | \nimport java.util.concurrent.Semaphore;\n\nclass Foo {\n\n final Semaphore hasFirstRun = new Semaphore(0);\n final Semaphore hasSecondRun = new Semaphore | andreazube | NORMAL | 2019-07-12T09:02:56.943744+00:00 | 2019-07-12T09:02:56.943786+00:00 | 2,922 | false | ```\nimport java.util.concurrent.Semaphore;\n\nclass Foo {\n\n final Semaphore hasFirstRun = new Semaphore(0);\n final Semaphore hasSecondRun = new Semaphore(0);\n\n public void first(Runnable printFirst) throws InterruptedException {\n printFirst.run();\n hasFirstRun.release(); // Allows second(... | 16 | 0 | ['Java'] | 0 |
print-in-order | [JAVA] Two Methods 【CAS】ans【synchronized】 | java-two-methods-cas-anssynchronized-by-z65wr | 1. CAS\n\nimport java.util.concurrent.atomic.AtomicInteger;\n\nclass Foo {\n\n\tAtomicInteger count = new AtomicInteger();\n\n\tpublic Foo() {\n\t\tcount.set(0) | frankzhang5513 | NORMAL | 2019-07-21T09:45:31.812115+00:00 | 2019-09-05T14:11:11.911223+00:00 | 2,130 | false | **1. CAS**\n```\nimport java.util.concurrent.atomic.AtomicInteger;\n\nclass Foo {\n\n\tAtomicInteger count = new AtomicInteger();\n\n\tpublic Foo() {\n\t\tcount.set(0);\n\t}\n\n\tpublic void first(Runnable printFirst) throws InterruptedException {\n\n\t\twhile (!count.compareAndSet(0, 4)) {\n\t\t\t// printFirst.run() o... | 12 | 0 | [] | 4 |
print-in-order | [C++] three versions: condition variables, semaphores, simple bools [fastest 124ms] | c-three-versions-condition-variables-sem-g8us | Version 1: One mutex lock + two condition variables\n 124 ms\n faster than 89.41%\n\nclass Foo {\npublic:\n bool firstRan = false;\n bool secondRan = fals | aly5321 | NORMAL | 2019-07-19T18:59:04.000554+00:00 | 2019-07-19T19:22:32.721635+00:00 | 2,718 | false | Version 1: One mutex lock + two condition variables\n* 124 ms\n* faster than 89.41%\n```\nclass Foo {\npublic:\n bool firstRan = false;\n bool secondRan = false;\n pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;\n pthread_cond_t cv1 = PTHREAD_COND_INITIALIZER;\n pthread_cond_t cv2 = PTHREAD_COND_INITIALIZ... | 12 | 1 | ['C'] | 9 |
print-in-order | 4 Python solutions | 4-python-solutions-by-faceplant-x49h | Solution 1:\n\nfrom threading import Event\nclass Foo:\ndef __init__(self):\n\tself.done = (Event(),Event())\n\ndef first(self, printFirst):\n\tprintFirst()\n\t | FACEPLANT | NORMAL | 2021-01-21T16:40:32.412474+00:00 | 2021-01-21T16:40:32.412517+00:00 | 1,301 | false | **Solution 1:**\n```\nfrom threading import Event\nclass Foo:\ndef __init__(self):\n\tself.done = (Event(),Event())\n\ndef first(self, printFirst):\n\tprintFirst()\n\tself.done[0].set()\n\ndef second(self, printSecond):\n\tself.done[0].wait()\n\tprintSecond()\n\tself.done[1].set()\n\ndef third(self, printThird):\n\tsel... | 11 | 0 | [] | 0 |
print-in-order | C#- Using AutoReset | c-using-autoreset-by-christris-ny2q | csharp\nusing System.Threading; \n\npublic class Foo \n{\n private EventWaitHandle waitFirst;\n private EventWaitHandle waitSecond;\n\n public Foo() | christris | NORMAL | 2020-05-08T13:42:23.759848+00:00 | 2020-05-08T13:42:23.759899+00:00 | 1,071 | false | ```csharp\nusing System.Threading; \n\npublic class Foo \n{\n private EventWaitHandle waitFirst;\n private EventWaitHandle waitSecond;\n\n public Foo() \n {\n waitFirst = new AutoResetEvent(initialState: false);\n waitSecond = new AutoResetEvent(initialState: false);\n }\n\n public vo... | 9 | 0 | [] | 0 |
print-in-order | C++ condition variable ~ 96% (also explaining how condition_variable works) | c-condition-variable-96-also-explaining-32adi | How condition_variable works:\n1. First, you need to acquire a mutex using unique_lock. We need a unique_lock because before putting the thread to sleep, condit | dineshkwal | NORMAL | 2019-11-16T11:24:52.801839+00:00 | 2020-06-18T14:33:15.489592+00:00 | 1,384 | false | How `condition_variable` works:\n1. First, you need to acquire a `mutex` using `unique_lock`. We need a unique_lock because before putting the thread to sleep, condition_variable will need to release the mutex and unique_lock provides that flexibility.\n2. Next, you call the `wait` function of condition_variable with t... | 9 | 1 | ['C'] | 1 |
print-in-order | Java 9 different solutions: Exchanger, Barrier, Latch, Lock etc | java-9-different-solutions-exchanger-bar-r4vg | Still learning. There are just so many choices Java gave us to handle multithreading.\n\nI am sure there are more ways to do this. And my knowledge is limited, | gangjig | NORMAL | 2023-11-16T01:16:53.682856+00:00 | 2024-02-19T19:05:23.181592+00:00 | 698 | false | Still learning. There are just so many choices Java gave us to handle multithreading.\n\nI am sure there are more ways to do this. And my knowledge is limited, I will come back to this post and improve and add more comparison once I understand better.\n\nFor synchronized solution:\n\nIf a thread calls wait() but no oth... | 8 | 0 | ['Java'] | 2 |
print-in-order | Python - Simple and Elegant! Event-based | python-simple-and-elegant-event-based-by-oanr | Solution:\n\nfrom threading import Event\n\nclass Foo:\n def __init__(self):\n self.event1, self.event2 = Event(), Event()\n\n def first(self, f):\ | domthedeveloper | NORMAL | 2022-03-24T06:20:57.786767+00:00 | 2022-03-24T06:20:57.786803+00:00 | 1,388 | false | **Solution**:\n```\nfrom threading import Event\n\nclass Foo:\n def __init__(self):\n self.event1, self.event2 = Event(), Event()\n\n def first(self, f):\n f()\n self.event1.set()\n\n def second(self, f):\n self.event1.wait()\n f()\n self.event2.set()\n\n def third(... | 8 | 0 | ['Python', 'Python3'] | 0 |
print-in-order | 3 C++ Solutions: Promises, Condition_variables, Atomic Bools | 3-c-solutions-promises-condition_variabl-dnob | If you liked it please upvote!\n\nC++ promise/future:\n1 of the 2 fastest (second one is the condition_variable)\n\nclass Foo {\nprivate:\n std::promise<void | dshelem | NORMAL | 2021-02-12T13:29:02.843754+00:00 | 2021-08-20T07:28:13.048361+00:00 | 685 | false | If you liked it please upvote!\n\n**C++ promise/future:**\n1 of the 2 fastest (second one is the condition_variable)\n```\nclass Foo {\nprivate:\n std::promise<void> prom_first;\n std::promise<void> prom_second;\n std::future<void> fut_first;\n std::future<void> fut_second;\npublic:\n Foo() {\n fu... | 8 | 0 | [] | 4 |
print-in-order | [Java] 4 Different Solutions w/ Explanation | java-4-different-solutions-w-explanation-h0z6 | 0. Analysis\nHere we have 3 threads and all we want to achieve is to make sure the 3 threads run in a specific order. To achieve this, we need coordination amon | isaacjumac | NORMAL | 2020-07-28T16:43:12.278356+00:00 | 2020-07-28T16:47:22.257217+00:00 | 413 | false | ## 0. Analysis\nHere we have 3 threads and all we want to achieve is to make sure the 3 threads run in a specific order. To achieve this, we need coordination among the threads, which means accessing and managing some common objects/resources. I think the most important point to achieve is about **visibility**: the oth... | 8 | 0 | [] | 2 |
print-in-order | C : semaphore-based - simple and short | c-semaphore-based-simple-and-short-by-un-w9q1 | \ntypedef struct {\n sem_t sem_a;\n sem_t sem_b;\n} Foo;\n\nFoo* fooCreate() {\n Foo* obj = malloc(sizeof (Foo));\n sem_init(&obj->sem_a, 0, 0);\n | universalcoder12 | NORMAL | 2020-03-16T08:38:24.857577+00:00 | 2020-03-16T08:38:24.857611+00:00 | 733 | false | ```\ntypedef struct {\n sem_t sem_a;\n sem_t sem_b;\n} Foo;\n\nFoo* fooCreate() {\n Foo* obj = malloc(sizeof (Foo));\n sem_init(&obj->sem_a, 0, 0);\n sem_init(&obj->sem_b, 0, 0);\n return obj;\n}\n\nvoid first(Foo* obj) {\n printFirst();\n sem_post(&obj->sem_a);\n}\n\nvoid second(Foo* obj) {\n ... | 8 | 0 | ['C'] | 1 |
print-in-order | Naive solution without using built-in libraries | naive-solution-without-using-built-in-li-vn0o | python\nclass Foo:\n def __init__(self):\n self.called = 0\n\n def first(self, printFirst: \'Callable[[], None]\') -> None:\n while self.cal | infmount | NORMAL | 2019-07-12T03:42:32.910768+00:00 | 2019-07-12T03:44:12.000766+00:00 | 973 | false | ```python\nclass Foo:\n def __init__(self):\n self.called = 0\n\n def first(self, printFirst: \'Callable[[], None]\') -> None:\n while self.called != 0:\n continue\n # printFirst() outputs "first". Do not change or remove this line.\n printFirst()\n self.called = 1\n\... | 8 | 0 | ['Python3'] | 2 |
print-in-order | C++ Mutex, Condition Variable Simple code | c-mutex-condition-variable-simple-code-b-8sy9 | Note\n\n We don\'t need any wait for printing 1. Hence, no condition variable wait\n We don\'t need to notify anyone after printing 3. Hence, no notify after pr | sam04101 | NORMAL | 2023-09-09T12:23:59.644813+00:00 | 2023-09-09T13:08:49.038855+00:00 | 1,781 | false | # Note\n\n* We don\'t need any wait for printing 1. Hence, no condition variable wait\n* We don\'t need to notify anyone after printing 3. Hence, no notify after printing 3.\n* My code doesn\'t work when I do \n`if(currentPrint!=2) cv.wait(lock)`\nbut works only when I do \n`cv.wait(lock,[&](){return currentPrint==2;})... | 7 | 0 | ['C++'] | 3 |
print-in-order | Java with two Semaphores | java-with-two-semaphores-by-dknlam-mi5y | \nclass Foo {\n\n private Semaphore second;\n private Semaphore third;\n\n public Foo() {\n second = new Semaphore(0);\n third = new Sema | dknlam | NORMAL | 2021-05-16T18:13:38.455555+00:00 | 2021-05-16T18:14:19.472337+00:00 | 519 | false | ```\nclass Foo {\n\n private Semaphore second;\n private Semaphore third;\n\n public Foo() {\n second = new Semaphore(0);\n third = new Semaphore(0);\n }\n\n public void first(Runnable printFirst) throws InterruptedException {\n // printFirst.run() outputs "first". Do not change or r... | 7 | 0 | [] | 1 |
print-in-order | [Java] 94% runtime efficiency, using synchronized, wait and notify, explained | java-94-runtime-efficiency-using-synchro-hwuo | Although I consider myself proficient in Java, this was a great refresher on synchronized, wait, and notify. I did some research. Based on Sonar, any wait() c | janinbv | NORMAL | 2020-12-06T08:02:22.897223+00:00 | 2020-12-11T10:03:20.222217+00:00 | 2,217 | false | Although I consider myself proficient in Java, this was a great refresher on synchronized, wait, and notify. I did some research. Based on Sonar, any wait() call should check a condition in a while loop, so I have two booleans to indicate if the first and second have been printed. I declared two objects, firstObject... | 7 | 1 | ['Java'] | 2 |
print-in-order | Boring C++ mutex solution | boring-c-mutex-solution-by-casd82-4l72 | \nclass Foo {\n mutex m1;\n mutex m2;\npublic:\n Foo() {\n m1.lock();\n m2.lock();\n }\n\n void first(function<void()> printFirst) | casd82 | NORMAL | 2020-07-30T01:58:55.132823+00:00 | 2020-07-30T01:58:55.132874+00:00 | 924 | false | ```\nclass Foo {\n mutex m1;\n mutex m2;\npublic:\n Foo() {\n m1.lock();\n m2.lock();\n }\n\n void first(function<void()> printFirst) {\n \n // printFirst() outputs "first". Do not change or remove this line.\n printFirst();\n m1.unlock();\n }\n\n void seco... | 7 | 1 | ['C'] | 1 |
print-in-order | [JAVA] Simple solution | java-simple-solution-by-ikisis-ojtd | \nclass Foo {\n \n CountDownLatch latchForSecond = new CountDownLatch(1);\n CountDownLatch latchForThird = new CountDownLatch(1);\n\n public Foo() { | ikisis | NORMAL | 2020-07-24T01:55:41.391579+00:00 | 2020-07-24T01:56:33.975101+00:00 | 954 | false | ```\nclass Foo {\n \n CountDownLatch latchForSecond = new CountDownLatch(1);\n CountDownLatch latchForThird = new CountDownLatch(1);\n\n public Foo() {\n \n }\n\n public void first(Runnable printFirst) throws InterruptedException {\n // printFirst.run() outputs "first". Do not change or ... | 7 | 1 | ['Java'] | 0 |
print-in-order | C++ future-promise | c-future-promise-by-jd-eth-uofd | Using C++ 11 feature and locking the object automatically with future-promise idiom. \n\n\nclass Foo {\npublic:\n Foo() {\n \n }\n std::promise< | jd-eth | NORMAL | 2020-02-25T19:17:19.852607+00:00 | 2020-02-25T19:17:19.852655+00:00 | 685 | false | Using C++ 11 feature and locking the object automatically with future-promise idiom. \n\n```\nclass Foo {\npublic:\n Foo() {\n \n }\n std::promise<void> one, two;\n \n void first(function<void()> printFirst) {\n printFirst();\n one.set_value();\n }\n\n void second(function<void... | 7 | 0 | [] | 0 |
print-in-order | C#: 2 solutions with or without thread | c-2-solutions-with-or-without-thread-by-6mwr6 | 1 with thead\n\npublic class Foo {\n\n int count;\n public Foo() {\n count = 0;\n }\n\n public void First(Action printFirst) {\n // pr | code4happy | NORMAL | 2019-07-31T05:09:46.465269+00:00 | 2019-07-31T05:09:46.465303+00:00 | 1,169 | false | #1 with thead\n```\npublic class Foo {\n\n int count;\n public Foo() {\n count = 0;\n }\n\n public void First(Action printFirst) {\n // printFirst() outputs "first". Do not change or remove this line.\n printFirst();\n count++;\n }\n\n public void Second(Action printSecond)... | 7 | 1 | [] | 3 |
print-in-order | Different python threading solutions (Lock, Barrier, Events, Semaphore, Condition) with explanation | different-python-threading-solutions-loc-0otq | \nStart with two locked locks. First thread unlocks the first lock that the second thread is waiting on. Second thread unlocks the second lock that the third th | aswin_m | NORMAL | 2024-05-11T05:20:54.840304+00:00 | 2024-05-11T05:20:54.840336+00:00 | 721 | false | \nStart with two locked locks. First thread unlocks the first lock that the second thread is waiting on. Second thread unlocks the second lock that the third thread is waiting on.\n\n# code\n```\nfrom threading import Lock\n\nclass Foo:\n def __init__(self):\n self.locks = (Lock(),Lock())\n self.locks[... | 6 | 0 | ['Python3'] | 2 |
print-in-order | Using Manual Reset Event Set/Wait Methods | using-manual-reset-event-setwait-methods-htyn | Intuition\nWe need to synchronize the method execution in multi threaded environment so same can be achived using ManualResetEventslim class. \n\n# Approach\nW | aroraneha | NORMAL | 2023-01-02T21:05:42.871157+00:00 | 2023-01-02T21:05:42.871201+00:00 | 1,470 | false | # Intuition\nWe need to synchronize the method execution in multi threaded environment so same can be achived using ManualResetEventslim class. \n\n# Approach\nWe can have two slim events, one to synchronize the call for first and second method to ensure that first always executed before second method. \nAnd second sl... | 6 | 0 | ['C#'] | 1 |
print-in-order | c 0ms 100% | c-0ms-100-by-willdufault-eyr0 | \n\ntypedef struct\n{\n // User defined data may be declared here.\n int turn;\n pthread_mutex_t lock;\n pthread_cond_t cond;\n} Foo;\n\nFoo* fooCre | willdufault | NORMAL | 2022-10-18T03:58:09.231758+00:00 | 2022-10-18T03:58:09.231803+00:00 | 802 | false | \n```\ntypedef struct\n{\n // User defined data may be declared here.\n int turn;\n pthread_mutex_t lock;\n pthread_cond_t cond;\n} Foo;\n\nFoo* fooCreate()\n{\n Foo* obj = (Foo*) malloc(sizeof(F... | 6 | 0 | ['C'] | 0 |
print-in-order | Semaphore | semaphore-by-hobiter-rusi | \nclass Foo {\n Semaphore run2, run3;\n\n public Foo() {\n run2 = new Semaphore(0);\n run3 = new Semaphore(0);\n }\n\n public void fir | hobiter | NORMAL | 2020-02-22T04:10:18.290067+00:00 | 2020-02-22T04:10:18.290102+00:00 | 551 | false | ```\nclass Foo {\n Semaphore run2, run3;\n\n public Foo() {\n run2 = new Semaphore(0);\n run3 = new Semaphore(0);\n }\n\n public void first(Runnable printFirst) throws InterruptedException {\n \n // printFirst.run() outputs "first". Do not change or remove this line.\n pri... | 6 | 0 | [] | 1 |
print-in-order | python3 solution via threading.Event | 36 ms 13 MB | python3-solution-via-threadingevent-36-m-oayj | \nfrom threading import Event\n\n\nclass Foo:\n def __init__(self):\n\t\t# Initialize events for threads\n self.event1 = Event()\n self.event2 | kuderr | NORMAL | 2019-11-30T19:30:34.167237+00:00 | 2020-07-06T12:57:38.417575+00:00 | 1,447 | false | ```\nfrom threading import Event\n\n\nclass Foo:\n def __init__(self):\n\t\t# Initialize events for threads\n self.event1 = Event()\n self.event2 = Event()\n \n\n def first(self, printFirst: \'Callable[[], None]\') -> None:\n printFirst()\n\t\t# set flag\n self.event1.set()\n\n\... | 6 | 0 | ['Python', 'Python3'] | 2 |
print-in-order | [C# ] AutoResetEvent (Beats 100%) | c-autoresetevent-beats-100-by-vaibhavdan-nmwc | \n public class Foo\n {\n private static AutoResetEvent event_1 = new AutoResetEvent(false);\n private static AutoResetEvent event_2 = new A | vaibhavdandekar | NORMAL | 2019-11-13T11:10:59.350687+00:00 | 2019-11-13T11:11:31.276195+00:00 | 609 | false | ```\n public class Foo\n {\n private static AutoResetEvent event_1 = new AutoResetEvent(false);\n private static AutoResetEvent event_2 = new AutoResetEvent(false);\n public Foo()\n {\n\n }\n\n public void First(Action printFirst)\n {\n // printFirst() o... | 6 | 0 | [] | 1 |
print-in-order | C++: 🚀 😎 One-line Solution with `std::atomic` | c-one-line-solution-with-stdatomic-by-em-2vx1 | Intuition\nWe use a variable (turn) to keep track of turns.\n\n# Approach\nWe use std::atomic to prevent data race. Plus, we use yield to prevent busy waiting.\ | emadpres | NORMAL | 2023-05-11T06:16:37.686891+00:00 | 2023-05-11T06:18:39.940323+00:00 | 1,723 | false | # Intuition\nWe use a variable (`turn`) to keep track of turns.\n\n# Approach\nWe use `std::atomic` to prevent data race. Plus, we use `yield` to prevent busy waiting.\n\n# Code\n```\nclass Foo {\n std::atomic<int> turn = 1;\npublic:\n Foo() {\n \n }\n\n void first(function<void()> printFirst) {\n ... | 5 | 0 | ['C++'] | 1 |
print-in-order | c++ use mutex | c-use-mutex-by-hossam_hassan-wy26 | Intuition\nUse condition variable and count variable and based on the count value , the desigenated thread will be running accordingly \n\n\n\nclass Foo {\npubl | hossam_hassan | NORMAL | 2023-03-10T18:11:43.517480+00:00 | 2023-03-10T18:11:43.517520+00:00 | 2,322 | false | # Intuition\nUse condition variable and count variable and based on the count value , the desigenated thread will be running accordingly \n\n\n```\nclass Foo {\npublic:\n\n std::mutex m; \n std::condition_variable cv;\n int count; \n \n Foo():count{0} {\n \n }\n\n void first(function<void()> pr... | 5 | 0 | ['C++'] | 2 |
print-in-order | Easy Java Solution | easy-java-solution-by-raghavdabra-llkn | \nclass Foo {\n\n private final CountDownLatch firstLatch;\n private final CountDownLatch secondLatch;\n\n public Foo() {\n firs | raghavdabra | NORMAL | 2022-10-12T05:18:24.034369+00:00 | 2022-10-12T05:18:24.034407+00:00 | 2,224 | false | ```\nclass Foo {\n\n private final CountDownLatch firstLatch;\n private final CountDownLatch secondLatch;\n\n public Foo() {\n firstLatch = new CountDownLatch(1);\n secondLatch = new CountDownLatch(1);\n }\n\n public void first(Runnable printFirst) throws Interru... | 5 | 0 | ['Java'] | 0 |
print-in-order | Java | CountDownLatch simple solution | java-countdownlatch-simple-solution-by-j-8vl7 | \nimport java.util.concurrent.CountDownLatch;\n\nclass Foo {\n\n private final CountDownLatch firstLatch = new CountDownLatch(1);\n private final CountDow | JKonSir | NORMAL | 2022-03-29T12:50:55.849252+00:00 | 2022-03-29T12:51:15.449271+00:00 | 714 | false | ```\nimport java.util.concurrent.CountDownLatch;\n\nclass Foo {\n\n private final CountDownLatch firstLatch = new CountDownLatch(1);\n private final CountDownLatch secondLatch = new CountDownLatch(1);\n\n public Foo() {\n\n }\n\n public void first(Runnable printFirst) throws InterruptedException {\n ... | 5 | 0 | ['Java'] | 0 |
print-in-order | C++ SHORTEST CODE USING PROMISE CLASS | c-shortest-code-using-promise-class-by-a-9vt9 | \nclass Foo {\n \nprivate: \n promise<void> p1, p2; \n \npublic:\n Foo() {}\n void first(function<void()> printFirst) {\n pr | akarshj950 | NORMAL | 2022-03-06T13:11:13.831235+00:00 | 2022-03-06T13:11:13.831268+00:00 | 559 | false | ```\nclass Foo {\n \nprivate: \n promise<void> p1, p2; \n \npublic:\n Foo() {}\n void first(function<void()> printFirst) {\n printFirst() ;\n p1.set_value() ;\n }\n ... | 5 | 0 | ['C'] | 1 |
print-in-order | Simple python solution | simple-python-solution-by-radhadman-0u5y | \nclass Foo(object):\n def __init__(self):\n self.x = 0\n \n def first(self, printFirst):\n printFirst()\n self.x += 1\n\n | radhadman | NORMAL | 2021-04-03T18:36:49.398340+00:00 | 2021-04-03T18:36:49.398379+00:00 | 794 | false | ```\nclass Foo(object):\n def __init__(self):\n self.x = 0\n \n def first(self, printFirst):\n printFirst()\n self.x += 1\n\n def second(self, printSecond):\n while self.x < 1:\n pass\n printSecond()\n self.x += 1\n \n def third(self, pr... | 5 | 0 | [] | 1 |
print-in-order | Python - using events | python-using-events-by-samirdeeb-5mh5 | \nclass Foo:\n def __init__(self):\n self.first_event = threading.Event()\n self.second_event = threading.Event()\n \n def first(self, | samirdeeb | NORMAL | 2021-01-02T04:28:14.201459+00:00 | 2021-01-02T04:28:14.201490+00:00 | 875 | false | ```\nclass Foo:\n def __init__(self):\n self.first_event = threading.Event()\n self.second_event = threading.Event()\n \n def first(self, printFirst: \'Callable[[], None]\') -> None:\n # printFirst() outputs "first". Do not change or remove this line.\n printFirst()\n self... | 5 | 0 | ['Python3'] | 0 |
print-in-order | Java solution with synchronized methods | java-solution-with-synchronized-methods-he5zw | Code with a runtime of 10ms :\n\n\nclass Foo {\n public int count;\n \n public Foo() {\n this.count = 1;\n }\n\n synchronized public void | abdus-samee | NORMAL | 2020-08-24T10:02:45.552761+00:00 | 2020-08-24T10:02:45.552808+00:00 | 1,224 | false | Code with a runtime of 10ms :\n\n```\nclass Foo {\n public int count;\n \n public Foo() {\n this.count = 1;\n }\n\n synchronized public void first(Runnable printFirst) throws InterruptedException {\n while(count != 1) wait();\n \n // printFirst.run() outputs "first". Do not ch... | 5 | 1 | ['Java'] | 2 |
print-in-order | Accepted C# using AutoResetEvent | accepted-c-using-autoresetevent-by-maxpu-3puk | \nusing System.Threading; \n\npublic class Foo\n {\n private EventWaitHandle _waitFirst;\n private EventWaitHandle _waitSecond;\n\n p | maxpushkarev | NORMAL | 2019-10-11T06:15:20.300559+00:00 | 2019-10-11T06:15:20.300590+00:00 | 437 | false | ```\nusing System.Threading; \n\npublic class Foo\n {\n private EventWaitHandle _waitFirst;\n private EventWaitHandle _waitSecond;\n\n public Foo()\n {\n _waitFirst = new AutoResetEvent(false);\n _waitSecond = new AutoResetEvent(false);\n }\n\n publi... | 5 | 0 | [] | 3 |
print-in-order | Simple Java Solution using booleans for locking | simple-java-solution-using-booleans-for-gwdry | class Foo {\n \n private volatile boolean executedFirst,executedSecond;\n public Foo() {}\n \n public void first(Runnable printFirst) | ping_pong | NORMAL | 2019-09-07T05:41:17.023848+00:00 | 2019-09-07T05:41:17.023894+00:00 | 1,026 | false | class Foo {\n \n private volatile boolean executedFirst,executedSecond;\n public Foo() {}\n \n public void first(Runnable printFirst) throws InterruptedException {\n executedFirst = true;\n printFirst.run();\n }\n \n public void second(Runnable printSeco... | 5 | 0 | [] | 1 |
print-in-order | Java solution using wait() and notifyAll() | java-solution-using-wait-and-notifyall-b-pdfk | A lot of solutions on here are using busy waits or sleeps - which are only slightly better. That is essentially a \'fail\' w.r.t. this question on threads. Here | alexworden | NORMAL | 2019-08-30T15:49:11.179957+00:00 | 2019-08-30T15:53:59.707868+00:00 | 568 | false | A lot of solutions on here are using busy waits or sleeps - which are only slightly better. That is essentially a \'fail\' w.r.t. this question on threads. Here\'s the solution I believe an interviewer would be looking for that uses wait() and notifyAll() with a liveness check inside a synchronized block. The locking o... | 5 | 0 | [] | 1 |
print-in-order | Java compareAndSet() | java-compareandset-by-tianchi-seattle-7d9t | java\nimport java.util.concurrent.atomic.AtomicInteger;\nclass Foo {\n\n private AtomicInteger integer;\n \n public Foo() {\n integer = new Atom | tianchi-seattle | NORMAL | 2019-07-31T02:51:10.104503+00:00 | 2019-07-31T02:51:10.104534+00:00 | 839 | false | ```java\nimport java.util.concurrent.atomic.AtomicInteger;\nclass Foo {\n\n private AtomicInteger integer;\n \n public Foo() {\n integer = new AtomicInteger(); \n }\n\n public void first(Runnable printFirst) throws InterruptedException {\n while(!integer.compareAndSet(0, 1));\n pr... | 5 | 2 | [] | 5 |
print-in-order | Four approaches: Passive vs Active Blocking | four-approaches-passive-vs-active-blocki-b5xw | Approach #1: Condition specific mutexes\nThis approach uses a mutex for each of the two dependent function calls. At the beginning, all the locks are claimed in | hnorth99 | NORMAL | 2024-02-18T21:58:26.594900+00:00 | 2024-02-18T21:58:26.594930+00:00 | 92 | false | Approach #1: Condition specific mutexes\nThis approach uses a mutex for each of the two dependent function calls. At the beginning, all the locks are claimed in the constructor. This ensures that none of the funcitons of requiring a lock are able to grasp or get ownership of the mutex. When they attempt to (with their ... | 4 | 0 | ['C++'] | 2 |
print-in-order | 🍞🍞🍞 Only used vector, no lock/semaphore/low-level objects | only-used-vector-no-locksemaphorelow-lev-6pho | Approach\nNot a fan of low level stuff (even for easy questions), so here\'s a solution using nothing but a vector.\n\nI made the class have a vector, printed, | TheGElCOgecko | NORMAL | 2023-05-02T03:21:49.340586+00:00 | 2023-07-07T05:41:29.195657+00:00 | 923 | false | # Approach\nNot a fan of low level stuff (even for easy questions), so here\'s a solution using nothing but a vector.\n\nI made the class have a vector, printed, which contains bool values of whether any given print function executed. Since the same instance will be passed to each thread, all threads can update this ve... | 4 | 0 | ['Array', 'Design', 'Concurrency', 'C++'] | 3 |
print-in-order | Java Solution - (Approach 1: Using Object lock, Approach 2: Using Countdown Latch) | java-solution-approach-1-using-object-lo-2i2k | Intuition\nWe need to suspend the thread printing second until first completes, and third until second completes.\n\n# Approach 1 - Using Object lock\nUse synch | mitesh_pv | NORMAL | 2023-01-22T14:53:20.755863+00:00 | 2023-01-22T14:57:37.795876+00:00 | 1,749 | false | # Intuition\nWe need to suspend the thread printing second until first completes, and third until second completes.\n\n# Approach 1 - Using Object lock\nUse `synchronsed` block to guard the block of code, and suspend the thread using `buzy waiting` method until `first`, `second` is not printed in order.\n\n\n# Code\n``... | 4 | 1 | ['Java'] | 0 |
print-in-order | A simple solution in Java | a-simple-solution-in-java-by-toshpolaty-0s7m | \nclass Foo {\n \n CountDownLatch second;\n CountDownLatch third;\n\n public Foo() {\n second = new CountDownLatch(1);\n third = new C | toshpolaty | NORMAL | 2022-06-13T15:04:01.971082+00:00 | 2022-06-13T15:04:01.971146+00:00 | 1,213 | false | ```\nclass Foo {\n \n CountDownLatch second;\n CountDownLatch third;\n\n public Foo() {\n second = new CountDownLatch(1);\n third = new CountDownLatch(1);\n }\n\n public void first(Runnable printFirst) throws InterruptedException {\n \n // printFirst.run() outputs "first". ... | 4 | 0 | ['Java'] | 0 |
print-in-order | Simple C++ solution with mutex | simple-c-solution-with-mutex-by-dandosh-q8bk | \nclass Foo {\npublic:\n mutex a, b;\n Foo() {\n a.lock();\n b.lock();\n }\n\n void first(function<void()> printFirst) {\n \n | dandosh | NORMAL | 2022-05-11T11:27:42.982501+00:00 | 2022-05-11T11:27:42.982534+00:00 | 456 | false | ```\nclass Foo {\npublic:\n mutex a, b;\n Foo() {\n a.lock();\n b.lock();\n }\n\n void first(function<void()> printFirst) {\n \n // printFirst() outputs "first". Do not change or remove this line.\n printFirst();\n a.unlock();\n }\n\n void second(function<void... | 4 | 0 | ['C'] | 1 |
print-in-order | [Java] Simplest solution using volatile variable | java-simplest-solution-using-volatile-va-zydh | Here, volatile keyword makes the variable threadsafe and making it visible to all threads works.\n\nJava\nclass Foo {\n\n volatile int methodCompleted;\n | black_swordsman_19 | NORMAL | 2022-04-28T00:21:08.509988+00:00 | 2022-04-28T00:21:53.271815+00:00 | 1,074 | false | Here, `volatile` keyword makes the variable threadsafe and making it visible to all threads works.\n\n```Java\nclass Foo {\n\n volatile int methodCompleted;\n \n public Foo() {\n methodCompleted = 0;\n }\n\n public void first(Runnable printFirst) throws InterruptedException {\n printFirst.r... | 4 | 0 | ['Java'] | 2 |
print-in-order | Java Semaphore solution | java-semaphore-solution-by-xiii-th-tcdj | \nclass Foo {\n \n private final Semaphore second = new Semaphore(0);\n private final Semaphore third = new Semaphore(0);\n\n public void first(Runn | XIII-th | NORMAL | 2022-03-13T05:06:51.886666+00:00 | 2022-03-13T05:06:51.886709+00:00 | 641 | false | ```\nclass Foo {\n \n private final Semaphore second = new Semaphore(0);\n private final Semaphore third = new Semaphore(0);\n\n public void first(Runnable printFirst) throws InterruptedException {\n // printFirst.run() outputs "first". Do not change or remove this line.\n printFirst.run();\n ... | 4 | 0 | ['Java'] | 0 |
print-in-order | Python simple solution without any lib | python-simple-solution-without-any-lib-b-kj4u | \tclass Foo:\n\t\tdef init(self):\n\t\t\tself.seq = 0\n\n\t\tdef first(self, printFirst: \'Callable[[], None]\') -> None:\n\t\t\tprintFirst()\n\t\t\tself.seq = | overzh_tw | NORMAL | 2021-12-16T06:30:38.805668+00:00 | 2021-12-16T06:30:38.805710+00:00 | 925 | false | \tclass Foo:\n\t\tdef __init__(self):\n\t\t\tself.seq = 0\n\n\t\tdef first(self, printFirst: \'Callable[[], None]\') -> None:\n\t\t\tprintFirst()\n\t\t\tself.seq = 1\n\n\t\tdef second(self, printSecond: \'Callable[[], None]\') -> None:\n\t\t\twhile self.seq < 1:\n\t\t\t\tcontinue\n\t\t\tprintSecond()\n\t\t\tself.seq = ... | 4 | 0 | ['Python', 'Python3'] | 1 |
print-in-order | Use semaphores for C++ solution | use-semaphores-for-c-solution-by-maitrey-ltr4 | \n#include <semaphore.h>\nclass Foo {\nprotected:\n sem_t firstDone;\n sem_t secondDone;\npublic:\n Foo() {\n sem_init(&firstDone, 0, 0);\n | maitreya47 | NORMAL | 2021-11-10T22:59:26.669505+00:00 | 2021-11-10T22:59:26.669542+00:00 | 628 | false | ```\n#include <semaphore.h>\nclass Foo {\nprotected:\n sem_t firstDone;\n sem_t secondDone;\npublic:\n Foo() {\n sem_init(&firstDone, 0, 0);\n sem_init(&secondDone, 0, 0);\n }\n\n void first(function<void()> printFirst) {\n \n // printFirst() outputs "first". Do not change or ... | 4 | 0 | ['C', 'C++'] | 0 |
print-in-order | Java fastest using volatile boolean and while poll | java-fastest-using-volatile-boolean-and-wblu6 | \nclass Foo {\n volatile boolean waitTwo = true;\n volatile boolean waitThree = true;\n\n public Foo() {\n \n }\n\n public void first(Runn | shashidhar71 | NORMAL | 2021-01-09T02:32:38.583774+00:00 | 2021-01-09T02:32:38.583813+00:00 | 623 | false | ```\nclass Foo {\n volatile boolean waitTwo = true;\n volatile boolean waitThree = true;\n\n public Foo() {\n \n }\n\n public void first(Runnable printFirst) throws InterruptedException {\n \n // printFirst.run() outputs "first". Do not change or remove this line.\n printFirst... | 4 | 1 | [] | 0 |
print-in-order | Py3 Runtime: 32 ms, faster than 93.93% | py3-runtime-32-ms-faster-than-9393-by-kk-w34b | Using thread lock and lock first and second first at the begining, then release first when printFirst being called, and release second when printSecond being ca | kkfnpdc123 | NORMAL | 2020-10-23T12:07:56.455389+00:00 | 2020-10-23T12:08:31.798894+00:00 | 690 | false | Using thread lock and lock `first` and `second` first at the begining, then release `first` when `printFirst` being called, and release `second` when `printSecond` being called. After all `printThird` will be called.\n\n```class Foo:\n def __init__(self):\n self.tl1 = threading.Lock()\n self.tl2 = thre... | 4 | 0 | ['Python3'] | 1 |
print-in-order | python loop and flag | python-loop-and-flag-by-karthikasasanka-f5lk | \nclass Foo:\n def __init__(self):\n self.f = True\n self.s = False\n self.t = False\n\n\n def first(self, printFirst: \'Callable[[], | karthikasasanka | NORMAL | 2020-08-07T06:29:46.065691+00:00 | 2020-08-07T06:29:46.065734+00:00 | 260 | false | ```\nclass Foo:\n def __init__(self):\n self.f = True\n self.s = False\n self.t = False\n\n\n def first(self, printFirst: \'Callable[[], None]\') -> None:\n \n # printFirst() outputs "first". Do not change or remove this line.\n printFirst()\n self.s = True\n\n\n ... | 4 | 0 | [] | 0 |
print-in-order | Java Simple Solution using volatile (97% faster) | java-simple-solution-using-volatile-97-f-ms98 | \nclass Foo {\n\n public volatile int order;\n public Foo() {\n order= 0;\n }\n\n public void first(Runnable printFirst) throws InterruptedEx | shubhamk2700 | NORMAL | 2020-08-02T17:28:54.281174+00:00 | 2020-08-02T17:28:54.281234+00:00 | 310 | false | ```\nclass Foo {\n\n public volatile int order;\n public Foo() {\n order= 0;\n }\n\n public void first(Runnable printFirst) throws InterruptedException {\n // printFirst.run() outputs "first". Do not change or remove this line.\n printFirst.run();\n order++;\n }\n\n public ... | 4 | 0 | [] | 0 |
print-in-order | Python use 2 Queue to synchronize | python-use-2-queue-to-synchronize-by-bai-yr1z | \nfrom Queue import Queue\n\n\nclass Foo(object):\n def __init__(self):\n self.q1 = Queue()\n self.q2 = Queue()\n\n def first(self, printFir | bairongdong1 | NORMAL | 2020-06-26T16:35:55.637938+00:00 | 2020-06-26T16:35:55.637986+00:00 | 243 | false | ```\nfrom Queue import Queue\n\n\nclass Foo(object):\n def __init__(self):\n self.q1 = Queue()\n self.q2 = Queue()\n\n def first(self, printFirst):\n """\n :type printFirst: method\n :rtype: void\n """\n # printFirst() outputs "first". Do not change or remove this ... | 4 | 0 | [] | 0 |
print-in-order | Simple 6 lines java Explained Solution | simple-6-lines-java-explained-solution-b-daq7 | Intutuion\n1. Use volatile variable which will update variable \'a\' in different caches of multi core system. Note - It is only required if this code runs on m | jaswinder_97 | NORMAL | 2020-05-19T05:48:42.189061+00:00 | 2020-05-19T05:48:42.189114+00:00 | 461 | false | Intutuion\n1. Use volatile variable which will update variable \'a\' in different caches of multi core system. Note - It is only required if this code runs on multi core environment.\n2. Run printFirst.run only when a=1;\n3. Run printSecond.run only when a=2; \n4. Similarly printThird.run only when a=3l\n```\nclass Foo... | 4 | 0 | [] | 0 |
print-in-order | C++ w/ promise 124ms <92.14%, 9.4MB <100% | c-w-promise-124ms-9214-94mb-100-by-jonli-t8g0 | Runtime: 124 ms, faster than 92.14% of C++ online submissions for Print in Order.\n* Memory Usage: 9.4 MB, less than 100.00% of C++ online submissions for Print | jonlist | NORMAL | 2020-01-19T23:30:01.612733+00:00 | 2020-01-20T02:48:36.823571+00:00 | 628 | false | * Runtime: 124 ms, faster than 92.14% of C++ online submissions for Print in Order.\n* Memory Usage: 9.4 MB, less than 100.00% of C++ online submissions for Print in Order.\n\nThis is a simple task. I did try writing one solution with a condition_variable and one using future/promise.\nThe performance was fairly close,... | 4 | 0 | ['C'] | 1 |
print-in-order | Intuitive Java Solution With Explanation | intuitive-java-solution-with-explanation-57u4 | Idea\nUse Guarded Block\nhttps://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html\n\nBasically, adding synchronized keyword to all the metho | naveen_kothamasu | NORMAL | 2019-08-10T03:40:56.354812+00:00 | 2019-10-06T01:21:35.674799+00:00 | 618 | false | **Idea**\nUse `Guarded Block`\nhttps://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html\n\nBasically, adding `synchronized` keyword to all the methods will require any thread to acquire intrisic lock of the object of `Foo` (which is used by all the three thread below).\nNow on this lock, we can cond... | 4 | 0 | [] | 3 |
print-in-order | C# AutoResetEvent 116ms | c-autoresetevent-116ms-by-code-world-5dyq | \nusing System.Threading;\n\npublic class Foo {\n EventWaitHandle evnt1 = null;\n EventWaitHandle evnt2 = null;\n public Foo() {\n evnt1 = new A | code-world | NORMAL | 2019-08-01T01:44:56.185963+00:00 | 2019-08-01T01:44:56.186020+00:00 | 470 | false | ```\nusing System.Threading;\n\npublic class Foo {\n EventWaitHandle evnt1 = null;\n EventWaitHandle evnt2 = null;\n public Foo() {\n evnt1 = new AutoResetEvent(false);\n evnt2 = new AutoResetEvent(false);\n }\n\n public void First(Action printFirst) {\n \n // printFirst() out... | 4 | 0 | [] | 0 |
print-in-order | simplest solution | simplest-solution-by-code4happy-tjfk | No fancy multiple threading blablalba\n\npublic class Foo {\n\n int count;\n public Foo() {\n count = 0;\n }\n\n public void First(Action pri | code4happy | NORMAL | 2019-07-31T05:16:49.298125+00:00 | 2019-07-31T05:21:57.936963+00:00 | 889 | false | No fancy multiple threading blablalba\n```\npublic class Foo {\n\n int count;\n public Foo() {\n count = 0;\n }\n\n public void First(Action printFirst) {\n // printFirst() outputs "first". Do not change or remove this line.\n printFirst();\n count++;\n }\n\n public void Se... | 4 | 7 | [] | 6 |
print-in-order | Java using Semaphores | java-using-semaphores-by-kabutar-vroz | \nimport java.util.concurrent.Semaphore;\nclass Foo {\n\n private final Semaphore second;\n private final Semaphore third;\n \n public Foo() { | kabutar | NORMAL | 2019-07-20T22:45:38.943713+00:00 | 2019-07-20T22:45:38.943746+00:00 | 374 | false | ```\nimport java.util.concurrent.Semaphore;\nclass Foo {\n\n private final Semaphore second;\n private final Semaphore third;\n \n public Foo() { \n second = new Semaphore(0);\n third = new Semaphore(0);\n \n }\n\n public void first(Runnable printFirst) throws InterruptedEx... | 4 | 0 | [] | 0 |
print-in-order | Python 3 Submission with threading.Condition, beats 100% & 100% | python-3-submission-with-threadingcondit-khmb | \nimport threading\n\n\nclass Foo:\n def __init__(self):\n self.condition = threading.Condition()\n self.first_was_printed = False\n sel | bombadil | NORMAL | 2019-07-12T08:22:27.837905+00:00 | 2019-07-12T08:29:48.737776+00:00 | 516 | false | ```\nimport threading\n\n\nclass Foo:\n def __init__(self):\n self.condition = threading.Condition()\n self.first_was_printed = False\n self.second_was_printed = False\n\n def first(self, printFirst: \'Callable[[], None]\') -> None:\n with self.condition:\n printFirst() ... | 4 | 0 | [] | 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.