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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
binary-tree-coloring-game | Python solution with simple steps | python-solution-with-simple-steps-by-val-m6p7 | Everything we need to know to answer if we can win this game the total tree size and left and right subtrees size, because we only have 3 options to choose t | valerab | NORMAL | 2021-02-01T00:19:21.014030+00:00 | 2021-02-01T00:19:21.014072+00:00 | 94 | false | Everything we need to know to answer if we can win this game the total tree size and left and right subtrees size, because we only have 3 options to choose to win this game:\n\n1. The choice left sub tree or red node, if left subtree more that whole tree \n1. The choice right sub tree or red node, if right subtree ... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | JAVA | DFS + DP + Greedy | 100% | | java-dfs-dp-greedy-100-by-idbasketball08-o97b | \nclass Solution {\n //these are the vals of the left and right node of x\n public int left = -1, right = -1;\n public boolean btreeGameWinningMove(Tre | idbasketball08 | NORMAL | 2021-01-18T00:41:31.105955+00:00 | 2021-01-18T00:41:52.263439+00:00 | 101 | false | ```\nclass Solution {\n //these are the vals of the left and right node of x\n public int left = -1, right = -1;\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n //strategy: DFS + DP + Greedy\n //try to block the 3 neighbors that player 1 chooses\n //dp[i] means all o... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | Java simple solution- one bottom up recursion | java-simple-solution-one-bottom-up-recur-mvx9 | \'\'\'\nclass Solution {\n int left;\n int right;\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n if(root==null) return f | uzya | NORMAL | 2020-10-06T03:30:56.684014+00:00 | 2020-10-06T03:30:56.684058+00:00 | 135 | false | \'\'\'\nclass Solution {\n int left;\n int right;\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n if(root==null) return false;\n dfs(root, x);\n //should see where to block , which node of x\'s left, right or parent\n //the part with most nodes. part including... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | C++ beat 100% | c-beat-100-by-shuqi7-eguy | \nTreeNode *find(TreeNode *node, int x) {\n\tif (node == NULL) return NULL;\n\tif (node->val == x) return node;\n\tTreeNode *left = find(node->left, x);\n\tTree | shuqi7 | NORMAL | 2020-09-27T15:44:46.386641+00:00 | 2020-09-27T15:44:46.386672+00:00 | 165 | false | ```\nTreeNode *find(TreeNode *node, int x) {\n\tif (node == NULL) return NULL;\n\tif (node->val == x) return node;\n\tTreeNode *left = find(node->left, x);\n\tTreeNode *right = find(node->right, x);\n\tif (left != NULL) return left;\n\treturn right;\n}\n\nint count(TreeNode *node) {\n\tif (node == NULL) return 0;\n\tre... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | Javascript Easy Solution With Clear Explanation [DFS] | javascript-easy-solution-with-clear-expl-9h84 | Okay so the solution to this problem is that we can win on 2 conditions:\n If the player has chosen a node where entire sum of all the nodes in the subtree for | james2allen | NORMAL | 2020-09-14T17:26:49.629579+00:00 | 2020-09-14T17:28:56.290313+00:00 | 184 | false | Okay so the solution to this problem is that we can win on 2 conditions:\n* If the player has chosen a node where entire sum of all the nodes in the subtree for their chosen node is less than the sum of the remainder nodes, you will easily win in this case by choosing the parent node to their tree and cutting them off.... | 1 | 0 | ['Depth-First Search', 'JavaScript'] | 0 |
binary-tree-coloring-game | My O(n) Solution | Accepted | Java | Beats 100% | my-on-solution-accepted-java-beats-100-b-5ur0 | \nclass Solution {\n int left = 0, right = 0;\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n \n helper(root, x);\n | Bhavyaa_Arora_08 | NORMAL | 2020-08-18T13:34:37.305754+00:00 | 2020-08-18T13:34:37.305917+00:00 | 77 | false | ```\nclass Solution {\n int left = 0, right = 0;\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n \n helper(root, x);\n int o = n - left - right -1 ;\n return left > n/2 || right > n/2 || o > n/2;\n \n }\n \n public int helper(TreeNode node, int x)... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | c++ BFS | c-bfs-by-sahlot01-rtt3 | \nint solve(TreeNode*node){\n if(node==NULL) return 0;\n int cnt=1;\n cnt+=solve(node->left);\n cnt+=solve(node->right);\n return cnt;\n}\n\nclas | sahlot01 | NORMAL | 2020-08-01T06:39:40.727548+00:00 | 2020-08-01T06:39:40.727597+00:00 | 107 | false | ```\nint solve(TreeNode*node){\n if(node==NULL) return 0;\n int cnt=1;\n cnt+=solve(node->left);\n cnt+=solve(node->right);\n return cnt;\n}\n\nclass Solution {\npublic:\n bool btreeGameWinningMove(TreeNode* node, int k, int x) {\n queue<TreeNode*>q;\n q.push(node);\n TreeNode*n;\... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | Python with explanation O(N) | python-with-explanation-on-by-rambhagwan-3y6k | You need to make yourself belive that 2nd player will only try to take either left node of x or right node of x or parent node of x:\nif 2nd playes takes left n | rambhagwan | NORMAL | 2020-07-28T07:27:28.162504+00:00 | 2020-07-28T07:27:28.162555+00:00 | 182 | false | You need to make yourself belive that 2nd player will only try to take either left node of x or right node of x or parent node of x:\nif 2nd playes takes left node of x:\n\tthen he can capture all nodes in this subtree. player 1 will capture all nodes nodes except these.\nif 2nd playes takes right node of x:\n\tthen he... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | JAVA EASIEST 0 ms | java-easiest-0-ms-by-gauravsethia-weyv | \nclass Solution {\n int left, right;\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n if(n==1)\n return false;\n | gauravsethia | NORMAL | 2020-07-19T17:58:00.892021+00:00 | 2020-07-19T17:58:00.892070+00:00 | 135 | false | ```\nclass Solution {\n int left, right;\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n if(n==1)\n return false;\n if(n==2)\n return false;\n if(n==3)\n {\n if(x!=1)\n return true;\n return false;\n ... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | JAVA easy to understand solution | java-easy-to-understand-solution-by-jian-bg2s | \n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n | jianhuilin1124 | NORMAL | 2020-06-22T03:36:33.687504+00:00 | 2020-06-22T03:36:33.687554+00:00 | 240 | false | ```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n * TreeNode(int val) { this.val = val; }\n * TreeNode(int val, TreeNode left, TreeNode right) {\n * this.val = val;\n * this.left = l... | 1 | 0 | ['Java'] | 0 |
binary-tree-coloring-game | Python Concise O(n) Solution (Beats 90%) | python-concise-on-solution-beats-90-by-z-eono | There are 3 best choices depending on the node the first player chose:\n1. The node\'s left child.\n2. The node\'s right child.\n3. The node\'s parent.\n\nFor e | zlax | NORMAL | 2020-06-20T10:40:54.395253+00:00 | 2020-06-20T10:43:03.334335+00:00 | 146 | false | There are 3 best choices depending on the node the first player chose:\n1. The node\'s left child.\n2. The node\'s right child.\n3. The node\'s parent.\n\nFor each choice, our best size is the size of that subtree (if it\'s the parent, its the total nodes - nodes rooted at the 1st player\'s node). The other player gets... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | Java easy to understand solution | java-easy-to-understand-solution-by-hema-g8eg | My algorithm:\n1. Find the node with value x\n2. Calculate number of nodes in the left and right sub trees of x\n3. n - (leftCount + rightCount + 1) will give r | hemanthreddy | NORMAL | 2020-06-16T09:40:41.723577+00:00 | 2020-06-16T09:43:55.350090+00:00 | 115 | false | My algorithm:\n1. Find the node with value `x`\n2. Calculate number of nodes in the left and right sub trees of `x`\n3. `n - (leftCount + rightCount + 1)` will give remaining subtree node count.\n4. Check if one of the above 3 subtrees has node count greater than the sum of remaining two.\n\n```\nclass Solution {\n ... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | Python | O(n) simple | python-on-simple-by-somanish-d3dz | We are interested in number of left and right child the X node has. Our case of winning is in either of 2 scenarios:\n1. I get any child (left or right) with ma | somanish | NORMAL | 2020-06-07T21:21:57.242633+00:00 | 2020-06-07T21:21:57.242677+00:00 | 104 | false | We are interested in number of left and right child the X node has. Our case of winning is in either of 2 scenarios:\n1. I get any child (left or right) with majority of nodes then I can block his coloring\n2. If parent has majority, I can select parent to block other half from red color\n```\nclass Solution:\n def ... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | cpp solution | cpp-solution-by-_mrbing-bofd | //I will win the game when when I choose a node in neighbour of x that has maximum number of nodes so that i will have more nodes to color if l is left neighb | _mrbing | NORMAL | 2020-05-30T04:59:59.583637+00:00 | 2020-05-30T04:59:59.583679+00:00 | 86 | false | //I will win the game when when I choose a node in neighbour of x that has maximum number of nodes so that i will have more nodes to color if l is left neighbour , r is right neighbour and u is upper neighbour then either of the below three should be true l > r+u || r > l+u || u > l+r whichever is true we\'ll choose ... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | JAVA 100% 100% With explanation | java-100-100-with-explanation-by-betterc-vmd8 | \t//find node in leftSubTree,then in rightSubTree ,then find leftNodeCount\n\t//if max of above 3 is greater than n/2 return true \n\tclass Solution {\n\t\tpubl | bettercallavi | NORMAL | 2020-05-24T08:06:23.476291+00:00 | 2020-05-24T08:06:23.476349+00:00 | 159 | false | \t//find node in leftSubTree,then in rightSubTree ,then find leftNodeCount\n\t//if max of above 3 is greater than n/2 return true \n\tclass Solution {\n\t\tpublic boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n\t\t\tTreeNode node=find(root,x);\n\t\t\tint countLeft=count(node.left);\n\t\t\tint countRight=c... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | Easy O(n) JAVA DFS solution | easy-on-java-dfs-solution-by-legendaryen-abkp | \nclass Solution {\n private int leftCount;\n private int rightCount;\n \n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n | legendaryengineer | NORMAL | 2020-04-25T23:53:11.040429+00:00 | 2020-04-25T23:53:11.040485+00:00 | 103 | false | ```\nclass Solution {\n private int leftCount;\n private int rightCount;\n \n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n find(root, x);\n int red = 1 + leftCount + rightCount;\n if (n - red > red || leftCount > n - leftCount || rightCount > n - rightCount) retu... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | C++ dfs | c-dfs-by-wufengxuan1230-4ltq | \nclass Solution {\npublic:\n int parent = 0;\n int left = 0;\n int right = 0;\n \n int traverse(TreeNode* root, int x)\n {\n if(root)\ | wufengxuan1230 | NORMAL | 2020-04-14T12:39:29.693875+00:00 | 2020-04-14T12:39:41.808444+00:00 | 160 | false | ```\nclass Solution {\npublic:\n int parent = 0;\n int left = 0;\n int right = 0;\n \n int traverse(TreeNode* root, int x)\n {\n if(root)\n {\n int l = traverse(root->left, x);\n int r = traverse(root->right, x);\n \n if(root->val == x)\n ... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | Python3 82.89% (28 ms)/100.00% (13.8 MB) -- O(n) time/O(h) space (recursion) -- 3 counters | python3-8289-28-ms10000-138-mb-on-timeoh-4m4c | \nclass Solution:\n def count_nodes(self, node, counters, index, x):\n if (node):\n if (node.val == x):\n self.count_nodes(n | numiek_p | NORMAL | 2020-04-02T21:13:22.703545+00:00 | 2020-04-02T21:13:22.703593+00:00 | 92 | false | ```\nclass Solution:\n def count_nodes(self, node, counters, index, x):\n if (node):\n if (node.val == x):\n self.count_nodes(node.left, counters, 1, x)\n self.count_nodes(node.right, counters, 2, x)\n else:\n counters[index] += 1\n ... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | Java Simple BFS | java-simple-bfs-by-hobiter-rn78 | \nclass Solution {\n int left, right, x;\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n this.x = x;\n dfs(root); | hobiter | NORMAL | 2020-03-15T06:14:31.839937+00:00 | 2020-03-15T17:58:11.273860+00:00 | 198 | false | ```\nclass Solution {\n int left, right, x;\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n this.x = x;\n dfs(root); \n return Math.max(Math.max(left, right), n - left - right - 1) > n / 2;\n }\n \n private int dfs(TreeNode node) {\n if (node == null)... | 1 | 0 | [] | 1 |
binary-tree-coloring-game | [C++] 0ms | Time O(N) | Space O(H) | Simple | c-0ms-time-on-space-oh-simple-by-dhdnr12-0day | \nclass Solution {\nprivate:\n int p1,pl,pr,X;\npublic:\n bool btreeGameWinningMove(TreeNode* root, int n, int x) {\n if (n == 1) return false;\n | dhdnr1220 | NORMAL | 2020-02-13T14:50:13.519060+00:00 | 2020-02-13T14:50:13.519094+00:00 | 132 | false | ```\nclass Solution {\nprivate:\n int p1,pl,pr,X;\npublic:\n bool btreeGameWinningMove(TreeNode* root, int n, int x) {\n if (n == 1) return false;\n p1 = pl = pr = 0; X = x;\n dfs(root);\n return (n - p1 > p1) || (pl > n - pl) || (pr > n - pr); \n }\n \n int dfs(TreeNode* n) {... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | Java simple solution beats 100% time & space with explanation | java-simple-solution-beats-100-time-spac-zcxs | \nclass Solution {\n private int left, right, val;\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n val = x; left =0; right = | akiramonster | NORMAL | 2019-12-31T17:30:48.044886+00:00 | 2019-12-31T17:30:48.044956+00:00 | 148 | false | ```\nclass Solution {\n private int left, right, val;\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n val = x; left =0; right = 0;\n count(root);\n return left > n/2 || right > n/2 || (left + right +1) < (n+1)/2;\n }\n \n private int count(TreeNode root){\n ... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | Easy DFS C++ Solution | easy-dfs-c-solution-by-codhek-fvdt | \n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode(int x) : v | codhek | NORMAL | 2019-12-29T18:00:22.909359+00:00 | 2019-12-29T18:00:22.909394+00:00 | 216 | false | ```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n vector <int> graph[105];\n set <int> vis;\n int size = 0;\n \n void... | 1 | 0 | ['Depth-First Search', 'C'] | 0 |
binary-tree-coloring-game | java recursion O(n) time O(h) space, beats 100% | java-recursion-on-time-oh-space-beats-10-cvyh | \n/*\nthe problem is same as seeing if can count # of nodes from x.left, or x.right, or x.parent, such that we can count # of nodes, ycount, such that ycount > | makotobot98 | NORMAL | 2019-12-26T21:13:34.245108+00:00 | 2019-12-26T21:13:34.245144+00:00 | 100 | false | ```\n/*\nthe problem is same as seeing if can count # of nodes from x.left, or x.right, or x.parent, such that we can count # of nodes, ycount, such that ycount > n - ycount\n\nthe reason for this intuition is that any node acts as a path block, meaning we pick any node i, then depends on the opponent node j, either le... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | Javascript 4 line solution | javascript-4-line-solution-by-liushuaima-rte0 | ```js\nvar btreeGameWinningMove = function(root, n, x) {\n const count = root => root ? count(root.left) + count(root.right) + 1 : 0;\n const find = root | liushuaimaya | NORMAL | 2019-12-24T12:13:03.332785+00:00 | 2019-12-24T12:17:46.795853+00:00 | 172 | false | ```js\nvar btreeGameWinningMove = function(root, n, x) {\n const count = root => root ? count(root.left) + count(root.right) + 1 : 0;\n const find = root => root ? root.val === x && root || find(root.left) || find(root.right) : false;\n const node = find(root), l = count(node.left), r = count(node.right), p = ... | 1 | 1 | ['JavaScript'] | 0 |
binary-tree-coloring-game | Explanation for recursive & short C# [100% speed and memory] | explanation-for-recursive-short-c-100-sp-o1jt | Fairly simple.\n\n Since we\'re following the parent pointer, treat the binary tree as a graph\n The x divides the graph into at most 3 subgraphs (left, right, | lano1 | NORMAL | 2019-12-21T20:19:20.686746+00:00 | 2019-12-21T20:26:46.429817+00:00 | 141 | false | Fairly simple.\n\n* Since we\'re following the parent pointer, treat the binary tree as a graph\n* The ```x``` divides the graph into at most 3 subgraphs (left, right, parent)\n* The optimal move is to play adjacent to ```x``` so we can colour one of the three subgraph \n* If any one of the 3 subgraphs has a count grea... | 1 | 0 | ['Binary Tree'] | 0 |
binary-tree-coloring-game | Java Simple Code 100% time and 100% memory with explanation. | java-simple-code-100-time-and-100-memory-ox74 | Suppose the first player marked node x. The main concept is, being a second player, we can stop the first player from expanding by either marking the parent of | pramitb | NORMAL | 2019-12-21T12:49:37.529900+00:00 | 2019-12-21T12:49:37.529945+00:00 | 102 | false | Suppose the first player marked node ***x***. The main concept is, being a second player, we can stop the first player from expanding by either marking the parent of ***x***, right child of ***x*** or the left child of ***x***. So, let\'s count the number of nodes in the left subtree of the node ***x*** as ***cl*** and... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | python -- O(N) | python-on-by-ht_wang-vkis | \ndef btreeGameWinningMove(self, root, n, x):\n """\n :type root: TreeNode\n :type n: int\n :type x: int\n :rtype: bool\n | ht_wang | NORMAL | 2019-12-17T06:02:30.701289+00:00 | 2019-12-17T06:02:30.701349+00:00 | 117 | false | ```\ndef btreeGameWinningMove(self, root, n, x):\n """\n :type root: TreeNode\n :type n: int\n :type x: int\n :rtype: bool\n """\n self.l, self.r = None, None\n def numNodes(root, x):\n if not root:\n return 0 \n l = numNodes(r... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | Java (100% / 100%) | java-100-100-by-benlu1117-lohu | \n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode(int x) | benlu1117 | NORMAL | 2019-12-11T01:04:10.775878+00:00 | 2019-12-11T01:04:10.775915+00:00 | 97 | false | ```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode(int x) { val = x; }\n * }\n */\nclass Solution {\n \n // after first player choose, the second play can have 3 options, choose parent, left or right so that ... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | JAVA DFS | java-dfs-by-jwang89225-w98d | \nThe first step is to find the node with value x. \nThe node will split whole tree into at almost three subtrees. \n \n node x \'s left subtree, \n node x\ | jwang89225 | NORMAL | 2019-11-22T14:40:27.675235+00:00 | 2019-11-22T14:40:27.675269+00:00 | 148 | false | \nThe first step is to find the node with value x. \nThe node will split whole tree into at almost three subtrees. \n ```\n node x \'s left subtree, \n node x\' s right subtree, \n the rest of subtree from node\'x parent \n```\n \n If parent of node x is null, which means the node x is root node. At this scenari... | 1 | 0 | ['Depth-First Search'] | 0 |
binary-tree-coloring-game | Python greedy solution | python-greedy-solution-by-loickenleetcod-sf49 | To block x player, we only consider xnode\'s parent node or it\'s two children nodes as y node.\n\n def btreeGameWinningMove(self, root: TreeNode, n: int, x: | loickenleetcode | NORMAL | 2019-10-29T07:31:34.007386+00:00 | 2019-10-29T07:31:34.007420+00:00 | 98 | false | To block x player, we only consider xnode\'s parent node or it\'s two children nodes as y node.\n```\n def btreeGameWinningMove(self, root: TreeNode, n: int, x: int) -> bool:\n def find(root, x):\n if not root:\n return\n if root.val == x:\n return root\n ... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | Java 100% Time 100% Memory | java-100-time-100-memory-by-peterpei666-m0f4 | \nclass Solution {\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n TreeNode cur = findNode(root, x);\n int numP = 0;\n | peterpei666 | NORMAL | 2019-10-15T23:14:46.092027+00:00 | 2019-10-15T23:14:46.092063+00:00 | 213 | false | ```\nclass Solution {\n public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n TreeNode cur = findNode(root, x);\n int numP = 0;\n if (findSize(cur.left) > n / 2) return true;\n if (findSize(cur.right) > n / 2) return true;\n if (findSize(cur) <= n / 2) return true;\n... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | Easy to understand C++ 100% faster and time and space complexity | easy-to-understand-c-100-faster-and-time-huwg | \n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode(int x) : v | pallavshah | NORMAL | 2019-09-01T09:04:18.284480+00:00 | 2019-09-01T09:04:18.284516+00:00 | 189 | false | ```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n \n void findXInTree(TreeNode*& temp, TreeNode* root, int x) {\n if(ro... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | C# Solution | c-solution-by-leonhard_euler-rpx8 | \npublic class Solution \n{\n public bool BtreeGameWinningMove(TreeNode root, int n, int x) \n {\n var node = FindNode(root, x);\n int l = C | Leonhard_Euler | NORMAL | 2019-08-05T01:26:11.503220+00:00 | 2019-08-05T01:26:11.503266+00:00 | 99 | false | ```\npublic class Solution \n{\n public bool BtreeGameWinningMove(TreeNode root, int n, int x) \n {\n var node = FindNode(root, x);\n int l = CountNodes(node.left), r = CountNodes(node.right);\n if(l > n/2 || r > n/2 || (n - l - r - 1) > n/2) return true;\n return false;\n }\n \n... | 1 | 0 | [] | 0 |
binary-tree-coloring-game | Python DFS Solution | python-dfs-solution-by-dragonrider-2h48 | If we observe that there are three "optimal" choices for \'y\'. \'y\' could either choose \'x\'\'s parent, x\'s left child or x\'s right child. Then the final o | dragonrider | NORMAL | 2019-08-04T18:46:08.183559+00:00 | 2019-08-04T18:46:08.183596+00:00 | 127 | false | If we observe that there are three "optimal" choices for \'y\'. \'y\' could either choose \'x\'\'s parent, x\'s left child or x\'s right child. Then the final optimal choice for \'y\' is the optimal among those three. If any choice result wining of \'y\', then \'y\' will win. Otherwise, \'y\' will loose.\n\n\nBelow is ... | 1 | 0 | [] | 0 |
painting-a-grid-with-three-different-colors | [C++/Python] DP & DFS & Bitmask - Picture explain - Clean & Concise | cpython-dp-dfs-bitmask-picture-explain-c-qhhb | Note\n- Simillar to problem: 1411. Number of Ways to Paint N \xD7 3 Grid, but in this case, the number of rows is not fixed to 3, it\'s a dynamic values <= 5. S | hiepit | NORMAL | 2021-07-11T04:03:53.708578+00:00 | 2021-07-12T03:48:42.831678+00:00 | 11,193 | false | **Note**\n- Simillar to problem: [1411. Number of Ways to Paint N \xD7 3 Grid](https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/discuss/574912), but in this case, the number of rows is not fixed to 3, it\'s a dynamic values <= 5. So we need to use dfs to generate all possible columns.\n- Use DP to calcuat... | 137 | 10 | [] | 21 |
painting-a-grid-with-three-different-colors | Top-down DP with bit mask | top-down-dp-with-bit-mask-by-votrubac-46g5 | Since m is limited to 5, and colours are limited to 3, we can represent the previous column using a bit mask (two bits for each color). We can have up to 1023 c | votrubac | NORMAL | 2021-07-11T04:03:02.225352+00:00 | 2021-07-11T18:23:38.225514+00:00 | 7,435 | false | Since `m` is limited to `5`, and colours are limited to `3`, we can represent the previous column using a bit mask (two bits for each color). We can have up to 1023 combinations (including white color for convenience) of colors in the column.\n\n1. `cur`: colors for the current column.\n2. `prev`: colors for the previo... | 79 | 4 | ['C'] | 20 |
painting-a-grid-with-three-different-colors | [C++] DP | c-dp-by-divyanshu1-do96 | Similar to: 1411. Number of Ways to Paint N \xD7 3 Grid\n\n\n\nvector<string> moves;\nint MOD = 1e9 + 7;\nvoid fill(string s, int n, int p){\n if(n==0){\n | divyanshu1 | NORMAL | 2021-07-11T04:15:01.149422+00:00 | 2021-07-25T04:27:47.654801+00:00 | 4,837 | false | *Similar to*: [1411. Number of Ways to Paint N \xD7 3 Grid](https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/discuss/941091/c%2B%2Boror-Easy-to-understand-oror-DFS-%2B-Memorization-oror-Space-%3A-O(12n)-Time-%3AO(12n))\n\n```\n\nvector<string> moves;\nint MOD = 1e9 + 7;\nvoid fill(string s, int n, int p){... | 67 | 11 | ['Dynamic Programming', 'C'] | 1 |
painting-a-grid-with-three-different-colors | Graph Based Approach | C++ | 100% Space & Time | Explained! | graph-based-approach-c-100-space-time-ex-8w1j | \t\tvector states;\n\t\tvoid generate(string state, char prev, int m){\n \n\t\t\tif(m==0){\n\t\t\t\tstates.push_back(state);\n\t\t\t\treturn;\n\t\t\t}\n\ | raghavchugh21 | NORMAL | 2021-07-11T06:25:06.557627+00:00 | 2022-03-19T12:06:05.326954+00:00 | 2,768 | false | \t\tvector<string> states;\n\t\tvoid generate(string state, char prev, int m){\n \n\t\t\tif(m==0){\n\t\t\t\tstates.push_back(state);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tstring colors = "RGB";\n\t\t\tfor(auto &color: colors){\n\t\t\t\tif(color!=prev)\n\t\t\t\t\tgenerate(state+color, color, m-1);\n\t\t\t}\n\n\t\t}\... | 45 | 0 | [] | 10 |
painting-a-grid-with-three-different-colors | [Python] O(2^m*n*m) dp solution, explained | python-o2mnm-dp-solution-explained-by-db-adxg | Solution 1\nThe idea is to go row by row and check that we do not have conflicts of colors in each row and between rows.\n\n1. C is all candidates for row, that | dbabichev | NORMAL | 2021-07-11T10:11:35.243245+00:00 | 2021-07-12T09:40:37.740460+00:00 | 2,244 | false | #### Solution 1\nThe idea is to go row by row and check that we do not have conflicts of colors in each row and between rows.\n\n1. `C` is all candidates for row, that is all rows, such that we do not have conflicts among horizontal adjacent cells.\n2. Also create dictionarly `d`: where for each candidate we collect al... | 42 | 1 | ['Dynamic Programming'] | 4 |
painting-a-grid-with-three-different-colors | dp state compression, very similar to 1411 | dp-state-compression-very-similar-to-141-hlk3 | State compression;\n\nThis questions is very similar to the 1411\tNumber of Ways to Paint N \xD7 3 Grid .\n\n1, First we rotate the grid 90 degree clockwise, | deepli | NORMAL | 2021-07-11T04:04:47.049761+00:00 | 2021-07-11T18:20:55.664485+00:00 | 2,463 | false | State compression;\n\nThis questions is very similar to the 1411\tNumber of Ways to Paint N \xD7 3 Grid .\n\n1, First we rotate the grid 90 degree clockwise, now it became the harder version of 1411, that is N X M Grid (M is in the range [1-5])\n2, we use a 3-based number to represent the state of one row. For examp... | 24 | 3 | [] | 7 |
painting-a-grid-with-three-different-colors | Java Simple Column-by-Column DP Solution O(48^2*N) | java-simple-column-by-column-dp-solution-dv9p | For m = 5, there are at most 48 valid states for a single column so we can handle it column by column.\nWe encode the color arrangement by bit mask (3 bit for a | hdchen | NORMAL | 2021-07-11T04:07:51.192665+00:00 | 2021-07-24T08:10:07.522852+00:00 | 2,282 | false | For m = 5, there are at most 48 valid states for a single column so we can handle it column by column.\nWe encode the color arrangement by bit mask (3 bit for a position) and use dfs to generate the all valid states.\nThen for each column, we iterator all the states and check if it\'s still valid with the previous colu... | 15 | 0 | [] | 5 |
painting-a-grid-with-three-different-colors | [Python] Straight forward solution | python-straight-forward-solution-by-vbsh-o75f | Using dp. Consider each row to be a state, we can find the valid adjacent states. Becomes number of paths problem.\nTime complexity : O(n*3^m) \n```\n\nfrom fun | vbshubham | NORMAL | 2021-07-11T04:04:59.968236+00:00 | 2021-07-11T13:44:06.304760+00:00 | 1,515 | false | Using dp. Consider each row to be a state, we can find the valid adjacent states. Becomes number of paths problem.\nTime complexity : O(n*3^m) \n```\n\nfrom functools import cache, lru_cache\nfrom itertools import product\n\n\nclass Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n mod = 10 ** 9 + ... | 14 | 2 | ['Dynamic Programming', 'Depth-First Search', 'Python'] | 1 |
painting-a-grid-with-three-different-colors | Thought Process, 3^(mn)mn -> n((2^(m-2))^2), 95% ever | thought-process-3mnmn-n2m-22-95-ever-by-iqycv | foreword: I\'m trying to share logical thought process for deriving various solutions from scratch with minimum tricks, if you think this style helps, please ki | jimhorng | NORMAL | 2021-08-03T11:54:29.097884+00:00 | 2021-09-02T14:57:38.466097+00:00 | 938 | false | *foreword: I\'m trying to share logical thought process for deriving various solutions from scratch with minimum tricks, if you think this style helps, please kindly **upvote** and/or **comment** for encouragement, or leave your thoughts to tune the content, thanks and happy thought processing :)*\n\n1) naive: find all... | 11 | 0 | ['Dynamic Programming', 'Python'] | 1 |
painting-a-grid-with-three-different-colors | C++ Solution Using Binary Exponentiation On Matrix O(n^3 * log(m)) | c-solution-using-binary-exponentiation-o-foxs | First we generate valid sequences with length N (N <= 5) (no adjacent cells have same color, ex:[1, 2, 3, 1, 3])\nWith the sequences, we can build an adjacency | felixhuang07 | NORMAL | 2021-07-11T04:00:37.345350+00:00 | 2022-04-30T05:02:47.372935+00:00 | 1,654 | false | First we generate valid sequences with length N (N <= 5) (no adjacent cells have same color, ex:[1, 2, 3, 1, 3])\nWith the sequences, we can build an adjacency matrix.\nFor example, [1, 2, 3, 2, 1] can move to [2, 1, 2, 1, 3] because\n[1, 2, 3, 2, 1]\n[2, 1, 2, 1, 3]\nno adjacent cells have the same color\n[1, 3, 2, 1,... | 11 | 1 | ['C', 'Matrix'] | 1 |
painting-a-grid-with-three-different-colors | Python 3 || Linear Transformations || T/S: 99% / 97% | python-3-linear-transformations-ts-99-97-q405 | \n\nclass Solution:\n def colorTheGrid(self, m: int, n: int, mod = 1_000_000_007) -> int:\n\n if m == 1:\n return 3*pow(2, n-1, mod) %mod\n | Spaulding_ | NORMAL | 2024-08-16T05:37:19.207868+00:00 | 2024-08-16T05:37:19.207903+00:00 | 221 | false | \n```\nclass Solution:\n def colorTheGrid(self, m: int, n: int, mod = 1_000_000_007) -> int:\n\n if m == 1:\n return 3*pow(2, n-1, mod) %mod\n\n if m == 2:\n return 2*pow(3, n, mod) %mod\n\n if m == 3:\n x0, x1 = 0, 3\n\n for _ in range(n):\n ... | 9 | 0 | ['Python3'] | 0 |
painting-a-grid-with-three-different-colors | Python count solution | python-count-solution-by-faceplant-fvgp | \n# find all possible patterns for a single row: [rgb, rbg, ...]\ndef findComb(s):\n\tif len(s) < m:\n\t\tfor c in "rgb":\n\t\t\tif c != s[-1]:\n\t\t\t\tfindCom | FACEPLANT | NORMAL | 2021-07-11T04:03:36.441730+00:00 | 2021-07-11T05:14:00.754128+00:00 | 795 | false | ```\n# find all possible patterns for a single row: [rgb, rbg, ...]\ndef findComb(s):\n\tif len(s) < m:\n\t\tfor c in "rgb":\n\t\t\tif c != s[-1]:\n\t\t\t\tfindComb(s + c)\n\telse:\n\t\tks.append(s)\nks = []\nfor c in "rgb":\n\tfindComb(c)\n\n# find all possible pairs: {rgb: [gbr, brg, ...], brg: [gbr, ...]}\nh = {}\nf... | 9 | 0 | [] | 2 |
painting-a-grid-with-three-different-colors | [Python] Simple Top-down DP explained | python-simple-top-down-dp-explained-by-w-wz8g | As each column is at most 5 colours high, then we process them column by column. We simply try all combinations and keep processing the ones that do not share a | watashij | NORMAL | 2021-11-03T01:55:50.799148+00:00 | 2021-11-03T01:55:50.799185+00:00 | 693 | false | As each column is at most 5 colours high, then we process them column by column. We simply try all combinations and keep processing the ones that do not share any colour with the previous column. \n\nThe trick is to use bit mask. We can label the 3 colours as `1`, `2`, `4`, which in binary form is `001`, `010`, `100`. ... | 7 | 0 | [] | 1 |
painting-a-grid-with-three-different-colors | 💥💥 Beats 100% on runtime and memory [EXPLAINED] | beats-100-on-runtime-and-memory-explaine-wgow | Intuition\nColoring a grid such that no two adjacent cells have the same color, leveraging the limited number of colors (red, green, blue) to create valid combi | r9n | NORMAL | 2024-10-20T09:36:16.251050+00:00 | 2024-10-20T09:36:16.251082+00:00 | 103 | false | # Intuition\nColoring a grid such that no two adjacent cells have the same color, leveraging the limited number of colors (red, green, blue) to create valid combinations while ensuring that all cells are painted.\n\n# Approach\nGenerate all valid color combinations for a single row, build an adjacency list representing... | 6 | 0 | ['Dynamic Programming', 'Backtracking', 'Depth-First Search', 'C#'] | 0 |
painting-a-grid-with-three-different-colors | [EASY] Machine Learning + Web Development + Blockchain + AR/VR + Bitset DP | easy-machine-learning-web-development-bl-30mi | This problem can be easily solved using Machine Learning + Web Development + Blockchain + AR/VR + Bitset DP:\n\nclass Solution {\npublic:\n void backtrack(lo | nikhilbalwani | NORMAL | 2022-01-07T17:55:22.356048+00:00 | 2022-01-07T17:55:22.356092+00:00 | 827 | false | * This problem can be easily solved using Machine Learning + Web Development + Blockchain + AR/VR + Bitset DP:\n```\nclass Solution {\npublic:\n void backtrack(long curr, int m, vector<int>& states) {\n if (m == 0) {\n states.push_back(curr);\n return;\n }\n \n for (... | 5 | 3 | [] | 1 |
painting-a-grid-with-three-different-colors | [c++] simple graph + memoization based solution with comments | c-simple-graph-memoization-based-solutio-ddtd | \nclass Solution {\npublic:\n \n vector<vector<long long int>>dp;\n vector<string>v;\n #define mod 1000000007\n \n //generating all proper str | SJ4u | NORMAL | 2021-07-11T10:43:40.745022+00:00 | 2021-07-11T10:43:40.745057+00:00 | 793 | false | ```\nclass Solution {\npublic:\n \n vector<vector<long long int>>dp;\n vector<string>v;\n #define mod 1000000007\n \n //generating all proper strings (no adj of same color of size m )\n \n void recurs(int m,char prev,string s)\n {\n if(m==0)\n {\n v.push_back(s)... | 5 | 0 | ['Dynamic Programming', 'Graph', 'C'] | 2 |
painting-a-grid-with-three-different-colors | [Python3] DP 5 Cases | python3-dp-5-cases-by-agrpractice-yt51 | DP 5 Cases\n\nInuition\n\nThere are only 5 cases to handle (1 <= m <= 5). Since m is small the recurrence relations per m can be discovered and returned.\n\nWit | AgrPractice | NORMAL | 2022-12-04T22:34:16.843190+00:00 | 2023-01-16T19:12:32.209982+00:00 | 640 | false | **DP 5 Cases**\n\n**Inuition**\n\nThere are only 5 cases to handle (`1 <= m <= 5`). Since `m` is small the recurrence relations per `m` can be discovered and returned.\n\nWith `m` fixed, for each time we increase `n` think in terms of possible `1*m` blocks that can be added to all existing `(n - 1)*m` blocks. \n\n**Cas... | 4 | 0 | ['Dynamic Programming', 'Python', 'Python3'] | 0 |
painting-a-grid-with-three-different-colors | Java Solution with Fast matrix power | java-solution-with-fast-matrix-power-by-54z2t | 1. Group possible paintings of a row to their templates and find number of painting of each template.\n#2. Find for each template pair how many paintings of sec | rs9 | NORMAL | 2021-07-11T18:35:54.562949+00:00 | 2021-07-11T18:58:35.355809+00:00 | 1,112 | false | #1. Group possible paintings of a row to their templates and find number of painting of each template.\n#2. Find for each template pair how many paintings of second template can be after each painting of first template.\n#3. create vector A with number of possible paintings of one row for each template (as said in #1).... | 4 | 0 | ['Dynamic Programming', 'Java'] | 0 |
painting-a-grid-with-three-different-colors | Same as N*3 colors c++ best easy soln | same-as-n3-colors-c-best-easy-soln-by-_r-apa4 | ```\n//just do n*3 and generalise case for 1,2,3,4,5 copy \nint dp[5000][5][5][5][5][5];\nclass Solution {\npublic:\n int mod=1000000007;\n \n bool ch | _rampj | NORMAL | 2021-07-11T04:04:25.420661+00:00 | 2023-12-06T18:55:39.067863+00:00 | 454 | false | ```\n//just do n*3 and generalise case for 1,2,3,4,5 copy \nint dp[5000][5][5][5][5][5];\nclass Solution {\npublic:\n int mod=1000000007;\n \n bool check(int a, int b, int c,int d,int e, int na, int nb, int nc,int nd,int ne){\n if(na!=a && nb!=b &nc!=c && nd!=d && ne!=e && na!=nb && nb!=nc && nc!=nd &&... | 4 | 6 | ['C++'] | 2 |
painting-a-grid-with-three-different-colors | O(9*(n+m)*4^{m-1}): Backtracking + dynamic programming | o9nm4m-1-backtracking-dynamic-programmin-j8ko | We have m rows and n columns; we need to determine how many colorings of the grid exist where no two 4-directionally adjacent cells have the same color.\n\nGENE | shaunakdas88 | NORMAL | 2022-07-11T18:26:28.149505+00:00 | 2022-07-12T14:31:08.615689+00:00 | 292 | false | We have `m` rows and `n` columns; we need to determine how many colorings of the grid exist where no two 4-directionally adjacent cells have the same color.\n\n**GENERATING VALID COLUMNS: BACKTRACKING**\nWe first generate the set of all possible "valid columns", i.e. any column where no two consecutive rows are the sam... | 3 | 0 | [] | 0 |
painting-a-grid-with-three-different-colors | Simple Solution with DP & Bitmask | simple-solution-with-dp-bitmask-by-faang-9vqw | Use 2bits to represent the state each cell, need 2*m bits since there are m rows\n2. Use 00, 01, 10 to represent 3 types of color\n3. Update dp[i][j] from dp[i | faang2022 | NORMAL | 2021-07-11T05:37:06.345421+00:00 | 2021-07-11T05:44:47.744610+00:00 | 419 | false | 1. Use 2bits to represent the state each cell, need 2*m bits since there are m rows\n2. Use 00, 01, 10 to represent 3 types of color\n3. Update dp[i][j] from dp[i-1][k] if j, k, and (j, k) are valid. j, k is the state of the colume\n```\nclass Solution {\npublic:\n const int M = 1e9+7;\n int m;\n \n\t// check... | 3 | 0 | [] | 0 |
painting-a-grid-with-three-different-colors | Python | Blazing fast O(log n) solution using modular matrix exponentiation | python-blazing-fast-olog-n-solution-usin-kob1 | Intuition\nThis solution is an extension to the solution for the same problem where $m = 3$ which can be found here. \n\nFor simplicity, we use $0,1,2$ to denot | parzan | NORMAL | 2024-06-11T20:24:49.844806+00:00 | 2024-06-11T20:24:49.844850+00:00 | 103 | false | # Intuition\nThis solution is an extension to the solution for the same problem where $m = 3$ which can be found [here](https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/solutions/5222661/python-simple-dp-and-fast-matrix-multiplication-with-full-explanation/). \n\nFor simplicity, we use $0,1,2$ to denote t... | 2 | 0 | ['Math', 'Matrix', 'Combinatorics', 'Python3'] | 1 |
painting-a-grid-with-three-different-colors | 2-D DP + BitMask + Unordered Map || C++ || Well Explained | 2-d-dp-bitmask-unordered-map-c-well-expl-vjck | Intuition\nThe problem requires coloring a grid of m rows and n columns such that no adjacent cells have the same color. We can approach this problem using dyna | EGhost08 | NORMAL | 2023-09-23T21:45:50.415169+00:00 | 2023-09-23T21:45:50.415193+00:00 | 251 | false | ### Intuition\nThe problem requires coloring a grid of `m` rows and `n` columns such that no adjacent cells have the same color. We can approach this problem using dynamic programming, where we keep track of the valid color masks for each cell in a row.\n\n### Approach\n1. We define a `generate_mask` function that gene... | 2 | 0 | ['Hash Table', 'Dynamic Programming', 'Bit Manipulation', 'Bitmask', 'C++'] | 0 |
painting-a-grid-with-three-different-colors | 2 Bitmask + Dp solution | 2-bitmask-dp-solution-by-hacker_antace-rozn | Intuition\n Describe your first thoughts on how to solve this problem. \nNote the memoisation. To reduce the memory usage we only memoise the i and premask as a | hacker_antace | NORMAL | 2023-09-23T06:00:07.734143+00:00 | 2023-09-23T06:02:26.749082+00:00 | 427 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nNote the memoisation. To reduce the memory usage we only memoise the i and premask as at j = 0, it is obvious that curmask should be zero.\n\n2 bits represents colors - \n00 -> while\n01 -> red\n10 -> yellow\n11 -> blue\n\nTo identify the... | 2 | 0 | ['Dynamic Programming', 'Recursion', 'Memoization', 'Bitmask', 'C++'] | 1 |
painting-a-grid-with-three-different-colors | Java | Short DP (bottom-up) | java-short-dp-bottom-up-by-student2091-a8ix | Ideas\nEach column is a state, with 3^5 possible states; however, there are a lot of invalid states and base 3 is difficult to handle, so we will use 2^10, 2 bi | Student2091 | NORMAL | 2022-07-10T20:44:26.819440+00:00 | 2022-07-10T20:47:41.427888+00:00 | 415 | false | #### Ideas\nEach column is a state, with `3^5` possible states; however, there are a lot of invalid states and base 3 is difficult to handle, so we will use `2^10`, 2 bits each to represent it. Here I\'ve chosen `00 to be invalid, and 01 -> blue, 10 -> red, 11 -> green.`\n\nThen for each configuration, we check previou... | 2 | 0 | ['Dynamic Programming', 'Java'] | 0 |
painting-a-grid-with-three-different-colors | dp + bitmask | brute-force dp | dp-bitmask-brute-force-dp-by-diyora13-jrqc | \nclass Solution {\npublic:\n int n,m,dp[1000][5][(1<<10)],mod=1e9+7;\n \n int sol(int i,int j,int mask)\n {\n if(i==n) return 1;\n if | diyora13 | NORMAL | 2022-05-31T08:23:18.516037+00:00 | 2022-05-31T08:23:18.516083+00:00 | 530 | false | ```\nclass Solution {\npublic:\n int n,m,dp[1000][5][(1<<10)],mod=1e9+7;\n \n int sol(int i,int j,int mask)\n {\n if(i==n) return 1;\n if(j==m) return sol(i+1,0,mask);\n \n long ans=dp[i][j][mask];\n if(ans!=-1) return ans;\n ans=0;\n \n for(int k=1;k<... | 2 | 0 | ['Dynamic Programming', 'C', 'Bitmask', 'C++'] | 0 |
painting-a-grid-with-three-different-colors | My C++ Solution... with dfs and bottom up dp | my-c-solution-with-dfs-and-bottom-up-dp-n2bpc | \nclass Solution {\npublic:\n vector<string> stripes;\n vector<char> temp;\n int p = 1e9 + 7;\n int colorTheGrid(int m, int n) {\n // we want | thin_k_ing | NORMAL | 2021-08-07T12:50:42.395169+00:00 | 2021-08-07T12:50:58.540863+00:00 | 388 | false | ```\nclass Solution {\npublic:\n vector<string> stripes;\n vector<char> temp;\n int p = 1e9 + 7;\n int colorTheGrid(int m, int n) {\n // we want to know what are the different\n // ways a single row can be filled with\n // given that the total number of columns\n // are m and we ... | 2 | 0 | [] | 0 |
painting-a-grid-with-three-different-colors | (C++) DP, Bitmask (Base 3) - Commented code for better understandability | c-dp-bitmask-base-3-commented-code-for-b-6zxb | Learnt DP with bitmasking yesterday and it feels good to have solved this problem today :)\n\nclass Solution {\npublic:\n int M, N;\n int MOD = 1e9 + 7;\n | nihargupta1512 | NORMAL | 2021-07-16T14:19:38.548949+00:00 | 2021-07-16T14:19:38.549019+00:00 | 756 | false | Learnt DP with bitmasking yesterday and it feels good to have solved this problem today :)\n```\nclass Solution {\npublic:\n int M, N;\n int MOD = 1e9 + 7;\n \n // function to fetch the (M-b-1)th bit of the mask with base 3\n int isEqual(int a, int b, int x)\n {\n if(a == -1)\n retur... | 2 | 0 | [] | 1 |
painting-a-grid-with-three-different-colors | Easy Java, comments, 28ms, O(n*P*P) complexity, memory O(P), where P is column permutations count | easy-java-comments-28ms-onpp-complexity-kq2fm | \nclass Solution {\n public int colorTheGrid(int m, int n) {\n //each permutation will be encoded as Long where every color will be packed into 0xFF b | dimitr | NORMAL | 2021-07-13T06:30:50.254072+00:00 | 2021-07-13T06:35:30.615948+00:00 | 311 | false | ```\nclass Solution {\n public int colorTheGrid(int m, int n) {\n //each permutation will be encoded as Long where every color will be packed into 0xFF byte \n //for example RGB combination will be represented as 0x010204\n //while BGRGB combination will be 0x0402010204\n final long R = 1... | 2 | 0 | [] | 0 |
painting-a-grid-with-three-different-colors | Python3. DFS (top down dp) without bit masking | python3-dfs-top-down-dp-without-bit-mask-40ch | \nclass Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n column_patterns = []\n \n\t\t# collect all column patterns with using back | yaroslav-repeta | NORMAL | 2021-07-12T17:03:26.393140+00:00 | 2021-07-12T17:06:58.535278+00:00 | 158 | false | ```\nclass Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n column_patterns = []\n \n\t\t# collect all column patterns with using backtracking\n def backtrack(pattern):\n if len(pattern) == m:\n column_patterns.append(pattern)\n else:\n ... | 2 | 0 | [] | 0 |
painting-a-grid-with-three-different-colors | Python top-down DP | python-top-down-dp-by-lixuanji-84ei | We represent cells with an integer, so the row "rgb" -> (0,1,2) \n\nThis is the core dp function, counting the number of valid colourings of a mxn grid with th | lixuanji | NORMAL | 2021-07-11T04:35:53.337326+00:00 | 2021-07-11T04:39:31.397184+00:00 | 232 | false | We represent cells with an integer, so the row "rgb" -> `(0,1,2)` \n\nThis is the core dp function, counting the number of valid colourings of a `mxn` grid with the constraint that the topmost row must be able to be placed below a row `prev`\n\n```\n@lru_cache(None) \ndef ans(self, n, prev):\n\tif n == 0: return 1\n\t... | 2 | 0 | [] | 0 |
painting-a-grid-with-three-different-colors | Java | 100% faster | Generalized | java-100-faster-generalized-by-mangostic-8mx3 | This is a generalized solution of any n and any m.\n\nThe code looks complicated, but the logic is quite easy. Although the code is long, the solution has by fa | MangoStickyRise | NORMAL | 2021-07-11T04:19:12.013797+00:00 | 2021-07-11T19:36:24.986936+00:00 | 670 | false | This is a **generalized** solution of any `n` and any `m`.\n\nThe code looks complicated, but the logic is quite easy. Although the code is long, the solution has by far the best performance.\n\nSay, `m = 5`. We name the color as numbers: `0, 1, 2`\n\nSteps:\n1. For each line, we have limited `patterns`, eg, `01010`, `... | 2 | 0 | [] | 1 |
painting-a-grid-with-three-different-colors | C++ O(n * (3^m) * f) DFS + DP | c-on-3m-f-dfs-dp-by-mingrui-d8i5 | The idea is to calculate all next column states from a previous column state (3^m). Then we can DP on each column.\nEach state is compressed into an int (curr, | mingrui | NORMAL | 2021-07-11T04:06:27.855697+00:00 | 2021-07-11T04:59:14.066440+00:00 | 438 | false | The idea is to calculate all next column states from a previous column state (3^m). Then we can DP on each column.\nEach state is compressed into an int (`curr`, `pre`, `hmap`\'s key) in which each adjacent two bits indicates a choice of color (00 as white).\n`hmap` counts the number of occurances of each state.\nA min... | 2 | 0 | [] | 1 |
painting-a-grid-with-three-different-colors | Simple Top Down recursive DP + bitmask. Generate all valid combination first. | simple-top-down-recursive-dp-bitmask-gen-lyxe | Code | Michael_Teng6 | NORMAL | 2025-04-09T19:18:22.705555+00:00 | 2025-04-09T19:18:22.705555+00:00 | 6 | false |
# Code
```cpp []
class Solution {
public:
int m,n;
int mod=1e9+7;
int dp[1000][32][32];
int dfs(int currow,int previgreen,int previblue,vector<pair<int,int>>& masks)
{
if(currow==n) return 1;
if(dp[currow][previgreen][previblue]!=-1) return dp[currow][previgreen][previblue];
... | 1 | 0 | ['Dynamic Programming', 'Depth-First Search', 'Recursion', 'Memoization', 'Bitmask', 'C++'] | 0 |
painting-a-grid-with-three-different-colors | Simplest Explanation | | Thought Process | | Why Bitmasking | simplest-explanation-thought-process-why-qj8t | IntuitionTo solve this problem, let's analyze the given constraints and requirements:
We have a grid of size m × n.
We can only use 3 colors to paint the grid. | UKS_28 | NORMAL | 2025-02-09T16:20:43.630369+00:00 | 2025-02-09T16:20:43.630369+00:00 | 102 | false | # Intuition
To solve this problem, let's analyze the given constraints and requirements:
- We have a grid of size **m × n**.
- We can only use **3 colors** to paint the grid.
- No two adjacent cells (horizontally or vertically) can have the same color.
### Initial Thoughts
A brute force approach would be to assign ... | 1 | 0 | ['C++'] | 0 |
painting-a-grid-with-three-different-colors | Simple 3D dp Solution | simple-3d-dp-solution-by-ammar-a-khan-i8xx | Code | ammar-a-khan | NORMAL | 2025-02-05T15:18:38.489881+00:00 | 2025-02-05T15:18:38.489881+00:00 | 79 | false | # Code
```cpp
int mod = 1000000007;
//memoization soln
int helper(int i, int j, int mask, int &m, int &n, vector<vector<vector<int>>> &dp){ //mask:[0, i) stores mask for curr col, rest for prev col
if (i == n){ return 1; }
if (dp[i][j][mask] == -1){ //compute if not already
int count = 0;
int ... | 1 | 0 | ['Dynamic Programming', 'Bit Manipulation', 'Bitmask', 'C++'] | 0 |
painting-a-grid-with-three-different-colors | The most readable solution without bitmask (and beats 73%)! | the-most-readable-solution-without-bitma-wznn | Intuition\nThere are at most 5 rows. So there are at most $3\cdot2^4 = 48$ possible ways to fill a column. This problem can be simplified to this: choose with r | kunix | NORMAL | 2024-04-11T04:34:48.469307+00:00 | 2024-04-11T04:34:48.469338+00:00 | 66 | false | # Intuition\nThere are at most 5 rows. So there are at most $3\\cdot2^4 = 48$ possible ways to fill a column. This problem can be simplified to this: choose with repitition $n$ columns from a set of columns such that no adjacent cells have the same color.\n\nConsidering filling the grid from left to right column by col... | 1 | 0 | ['Python3'] | 0 |
painting-a-grid-with-three-different-colors | A elegant ruby solution | a-elegant-ruby-solution-by-safiir-sgry | ruby\nrequire "matrix"\n\n# @param {Integer} m\n# @param {Integer} n\n# @return {Integer}\ndef color_the_grid(m, n)\n mod = 10**9 + 7\n dp = m == 1 ? [[3]] : | safiir | NORMAL | 2023-05-06T07:51:18.809512+00:00 | 2023-05-06T07:51:18.809545+00:00 | 15 | false | ```ruby\nrequire "matrix"\n\n# @param {Integer} m\n# @param {Integer} n\n# @return {Integer}\ndef color_the_grid(m, n)\n mod = 10**9 + 7\n dp = m == 1 ? [[3]] : [[6]] * (2**(m - 2))\n (Matrix[*$matrixs[m - 1]]**(n - 1) * Matrix[*dp]).sum % mod\nend\n\n$matrixs = [\n [[2]],\n [[3]],\n [[3, 2], [2, 2]],\n [[3, 2, ... | 1 | 0 | ['Dynamic Programming', 'Ruby'] | 0 |
painting-a-grid-with-three-different-colors | Explained Solution | explained-solution-by-rkkumar421-5qgp | Approach\nWe are using integer as bit mask to pass which color have we used as we can only use 1,2,3 color .\n\n# Complexity\n- Time complexity: O(NM2^M*2)\n Ad | rkkumar421 | NORMAL | 2023-01-13T06:00:14.009049+00:00 | 2023-01-13T06:00:14.009098+00:00 | 198 | false | # Approach\nWe are using integer as bit mask to pass which color have we used as we can only use 1,2,3 color .\n\n# Complexity\n- Time complexity: O(N*M*2^M*2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass... | 1 | 0 | ['Dynamic Programming', 'Bit Manipulation', 'Memoization', 'C++'] | 0 |
painting-a-grid-with-three-different-colors | JAVA||O(3*2^(m-1)*n)||EASY UNDERSTANDING|| | javao32m-1neasy-understanding-by-asppand-a22q | \nclass Solution \n{\n static int mod=(int)(1e9+7);\n public static int dfs(int n,ArrayList<ArrayList<Integer>> arr,int src,int dp[][])\n {\n if(n= | asppanda | NORMAL | 2022-05-22T08:57:43.216090+00:00 | 2022-05-22T08:59:11.519978+00:00 | 297 | false | ```\nclass Solution \n{\n static int mod=(int)(1e9+7);\n public static int dfs(int n,ArrayList<ArrayList<Integer>> arr,int src,int dp[][])\n {\n if(n==0)\n {\n return 1;\n }\n if(dp[n][src]!=-1)\n {\n return dp[n][src];\n }\n int val=0;\n for(Integ... | 1 | 0 | ['Dynamic Programming', 'Depth-First Search', 'Java'] | 0 |
painting-a-grid-with-three-different-colors | Python | Easier to Understand | python-easier-to-understand-by-aryonbe-3h9n | Foward implementation based on\nhttps://leetcode.com/problems/painting-a-grid-with-three-different-colors/discuss/1330889/Python-O(2mnm)-dp-solution-explained\n | aryonbe | NORMAL | 2022-03-18T01:52:10.707249+00:00 | 2022-04-23T13:06:30.154911+00:00 | 391 | false | Foward implementation based on\nhttps://leetcode.com/problems/painting-a-grid-with-three-different-colors/discuss/1330889/Python-O(2m*n*m)-dp-solution-explained\n\n```\nfrom functools import lru_cache\n#dfs(i,row) means the number of color configuration based on current configuration of upper part(row)\nclass Solution:... | 1 | 0 | ['Depth-First Search', 'Python'] | 1 |
painting-a-grid-with-three-different-colors | [C++] hash map for DP with bitmasking column index and colored column, explained | c-hash-map-for-dp-with-bitmasking-column-rlch | The idea is to use 0b100, 0b010 and 0b001 respectively for R, G and B colors. For example, a column of size m = 5 with RGRBG code would give an integer : 0b100 | sjaubain | NORMAL | 2021-11-21T13:59:53.134636+00:00 | 2021-11-21T16:52:41.063430+00:00 | 388 | false | The idea is to use `0b100`, `0b010` and `0b001` respectively for R, G and B colors. For example, a column of size `m = 5` with RGRBG code would give an integer : `0b100 010 100 010 001`. To know in one pass how to spot the right column with its index, I shift the index beyond the 32 first bits used for the colors. Ther... | 1 | 0 | ['C'] | 0 |
painting-a-grid-with-three-different-colors | (C++) 1931. Painting a Grid With Three Different Colors | c-1931-painting-a-grid-with-three-differ-33j8 | \n\nclass Solution {\npublic:\n int colorTheGrid(int m, int n) {\n long memo[m][n][1<<2*m]; \n memset(memo, 0, sizeof(memo));\n \n | qeetcode | NORMAL | 2021-07-15T15:45:14.998261+00:00 | 2021-07-15T15:45:47.075517+00:00 | 524 | false | \n```\nclass Solution {\npublic:\n int colorTheGrid(int m, int n) {\n long memo[m][n][1<<2*m]; \n memset(memo, 0, sizeof(memo));\n \n function<long(int, int, int)> fn = [&](int i, int j, int mask) {\n if (j == n) return 1l; \n if (i == m) return fn(0, j+1, mask); \n ... | 1 | 0 | ['C'] | 0 |
painting-a-grid-with-three-different-colors | [Python3] top-down dp | python3-top-down-dp-by-ye15-zt4s | \n\nclass Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n \n @cache\n def fn(i, j, mask): \n """Return number of | ye15 | NORMAL | 2021-07-15T04:13:40.416042+00:00 | 2021-07-15T04:13:40.416068+00:00 | 462 | false | \n```\nclass Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n \n @cache\n def fn(i, j, mask): \n """Return number of ways to color grid."""\n if j == n: return 1 \n if i == m: return fn(0, j+1, mask)\n ans = 0 \n for x in 1<<2*i,... | 1 | 0 | ['Python3'] | 2 |
painting-a-grid-with-three-different-colors | Mutation from hiepit's 3 X N solution | mutation-from-hiepits-3-x-n-solution-by-ysnhe | I know this solution looks a bit silly but understandable and feasible!\nMutate from : https://leetcode.com/problems/painting-a-grid-with-three-different-colors | GoogleNick | NORMAL | 2021-07-12T18:00:07.848687+00:00 | 2021-07-12T18:14:41.445392+00:00 | 103 | false | I know this solution looks a bit silly but understandable and feasible!\nMutate from : https://leetcode.com/problems/painting-a-grid-with-three-different-colors/discuss/1330185/C%2B%2BPython-DP-and-DFS-and-Bitmask-Picture-explain-Clean-and-Concise\n```\nclass Solution {\n int dp[1001][4][4][4][4][4] = {};\npublic:\n... | 1 | 0 | [] | 1 |
painting-a-grid-with-three-different-colors | [Javascript] Bitmask & DFS with Cache | javascript-bitmask-dfs-with-cache-by-zac-c2c8 | Javascript version of - https://leetcode.com/problems/painting-a-grid-with-three-different-colors/discuss/1330185/C%2B%2BPython-DP-and-DFS-and-Bitmask-Picture-e | zachzwy | NORMAL | 2021-07-11T23:38:53.148775+00:00 | 2021-07-11T23:38:53.148806+00:00 | 103 | false | Javascript version of - https://leetcode.com/problems/painting-a-grid-with-three-different-colors/discuss/1330185/C%2B%2BPython-DP-and-DFS-and-Bitmask-Picture-explain-Clean-and-Concise\n\n```\n// To store previous column state in DP, we can use BitMask,\n// each 2 bits store a color (1=Red, 2=Green, 3=Blue, 0=White),\n... | 1 | 0 | [] | 0 |
painting-a-grid-with-three-different-colors | 1931 Backtrack with memo | DP row by row | 1931-backtrack-with-memo-dp-row-by-row-b-ajvr | Solution 1: simple backtrack\n2D dp -> 1D dp -> string (string is hashable(cached) while list is not)\nm is no more than 5, so let m be 1D dp dimension\n\ndef c | m1kasa | NORMAL | 2021-07-11T14:24:14.912473+00:00 | 2021-07-23T03:46:07.584271+00:00 | 177 | false | # Solution 1: simple backtrack\n2D dp -> 1D dp -> string (string is hashable(cached) while list is not)\nm is no more than 5, so let m be 1D dp dimension\n```\ndef colorTheGrid(self, m: int, n: int) -> int:\n\t@lru_cache(None)\n\tdef backtrack(k=0, dp="0" * (m + 1)):\n\t\tif k == m * n: return 1\n\t\ti, j = divmod(k, m... | 1 | 0 | [] | 0 |
painting-a-grid-with-three-different-colors | [Python] Faster then 100% | python-faster-then-100-by-whymustihavean-aw6j | The mats are the transition matrices I calculated using other codes.\n\n\nimport numpy\nmats=[None,None,None,numpy.array([[3, 2], [2, 2]]),\n numpy.array([[3 | WhymustIhaveaname | NORMAL | 2021-07-11T05:45:07.174977+00:00 | 2021-07-11T05:45:07.175044+00:00 | 138 | false | The `mats` are the transition matrices I calculated using other codes.\n\n```\nimport numpy\nmats=[None,None,None,numpy.array([[3, 2], [2, 2]]),\n numpy.array([[3, 2, 1, 2], [2, 2, 1, 2], [1, 1, 2, 1], [2, 2, 1, 2]]),\n numpy.array([[3, 2, 2, 1, 0, 1, 2, 2], [2, 2, 2, 1, 1, 1, 1, 1], [2, 2, 2, 1, 0, 1, 2, 2], [1,... | 1 | 0 | ['Dynamic Programming', 'Matrix'] | 1 |
painting-a-grid-with-three-different-colors | [Python3] Dynamic Programming Explained | O(mn(3^ m)) | python3-dynamic-programming-explained-om-h971 | Approach\n\nWe have a grid of size ( m * n ) and we want to fill each grid box with some color with 3 choices. A look at constraints gives some insight on numbe | wormx | NORMAL | 2021-07-11T04:45:34.960072+00:00 | 2021-07-11T04:53:36.521274+00:00 | 133 | false | Approach\n\nWe have a grid of size ( m * n ) and we want to fill each grid box with some color with 3 choices. A look at constraints gives some insight on number of rows being limited to 5 which seems to be a good candidate for a ternary mask (base 3).\n\nLets try to explain this with an exmaple.\nAsume m = 3 and n = 3... | 1 | 0 | [] | 0 |
painting-a-grid-with-three-different-colors | [Python] Column-by-Column Counting | python-column-by-column-counting-by-nthi-5gbg | The general idea of this approach is that since columns are small, m <= 5, we can think about counting the number of ways as we fill out the grid left to right, | nthistle | NORMAL | 2021-07-11T04:30:07.444477+00:00 | 2021-07-11T04:30:07.444541+00:00 | 210 | false | The general idea of this approach is that since columns are small, `m <= 5`, we can think about counting the number of ways as we fill out the grid left to right, column by column. Specifically, once we fill out a column, we no longer care about the colors in any other columns to the left of it. This way, we can just k... | 1 | 0 | [] | 0 |
painting-a-grid-with-three-different-colors | C++| Brute way | using strings for state | c-brute-way-using-strings-for-state-by-a-cy9g | ```\nclass Solution {\npublic:\n void g(string &s,int m,vector&v,int i,string &c){\n if(i==m){\n v.push_back(c);\n return;\n | amar_o1 | NORMAL | 2021-07-11T04:12:26.859673+00:00 | 2021-07-11T04:12:26.859706+00:00 | 189 | false | ```\nclass Solution {\npublic:\n void g(string &s,int m,vector<string>&v,int i,string &c){\n if(i==m){\n v.push_back(c);\n return;\n }\n if(i==0){\n for(int j=1;j<=3;j++){\n if(j!=s[i]-\'0\'){\n c.push_back(j+\'0\');\n ... | 1 | 1 | [] | 0 |
painting-a-grid-with-three-different-colors | Python 3 dp | python-3-dp-by-szlghl1-06o4 | python\nclass Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n endings = set()\n \n def dfs_ending(i, cur_ending):\n | szlghl1 | NORMAL | 2021-07-11T04:01:15.686228+00:00 | 2021-07-11T04:01:15.686257+00:00 | 441 | false | ```python\nclass Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n endings = set()\n \n def dfs_ending(i, cur_ending):\n if i == m:\n endings.add(cur_ending)\n return\n for to_add in range(3):\n c = str(to_add)\n ... | 1 | 1 | [] | 2 |
painting-a-grid-with-three-different-colors | 1931. Painting a Grid With Three Different Colors | 1931-painting-a-grid-with-three-differen-fcau | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | G8xd0QPqTy | NORMAL | 2025-01-16T12:43:08.697235+00:00 | 2025-01-16T12:43:08.697235+00:00 | 21 | 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
`... | 0 | 0 | ['Java'] | 0 |
painting-a-grid-with-three-different-colors | DP java | dp-java-by-garcol-0ju7 | IntuitionDPApproachBitmask to store previous stateComplexity
Time complexity: 243 * O(n)
Space complexity: 243 * O(n)
Code | garcol | NORMAL | 2025-01-14T14:31:09.120680+00:00 | 2025-01-14T14:55:23.602638+00:00 | 15 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
DP
# Approach
<!-- Describe your approach to solving the problem. -->
Bitmask to store previous state
# Complexity
- Time complexity: 243 * O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: 243 * O(n)
<!-- Ad... | 0 | 0 | ['Java'] | 0 |
painting-a-grid-with-three-different-colors | dp+ cache | dp-cache-by-akther-dpg2 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | akther | NORMAL | 2025-01-07T11:53:26.942059+00:00 | 2025-01-07T11:53:26.942059+00:00 | 14 | 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
`... | 0 | 0 | ['Dynamic Programming', 'Python3'] | 0 |
painting-a-grid-with-three-different-colors | Make DAG of adjacent rows | make-dag-of-adjacent-rows-by-theabbie-fxsa | \nM = 10 ** 9 + 7\n\ncache = {}\n\nclass Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n if m not in cache:\n good = []\n | theabbie | NORMAL | 2024-12-27T06:59:45.496781+00:00 | 2024-12-27T06:59:45.496805+00:00 | 4 | false | ```\nM = 10 ** 9 + 7\n\ncache = {}\n\nclass Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n if m not in cache:\n good = []\n def gen(arr):\n if len(arr) == m:\n good.append(arr[:])\n return\n for l in range(... | 0 | 0 | ['Python'] | 0 |
painting-a-grid-with-three-different-colors | 1931. Painting a Grid With Three Different Colors.cpp | 1931-painting-a-grid-with-three-differen-x4ij | Code\n\nclass Solution {\npublic:\n int colorTheGrid(int m, int n) {\n long memo[m][n][1<<2*m]; \n memset(memo, 0, sizeof(memo));\n \n | 202021ganesh | NORMAL | 2024-10-22T09:51:40.868931+00:00 | 2024-10-22T09:51:40.868972+00:00 | 3 | false | **Code**\n```\nclass Solution {\npublic:\n int colorTheGrid(int m, int n) {\n long memo[m][n][1<<2*m]; \n memset(memo, 0, sizeof(memo));\n \n function<long(int, int, int)> fn = [&](int i, int j, int mask) {\n if (j == n) return 1l; \n if (i == m) return fn(0, j+1, ma... | 0 | 0 | ['C'] | 0 |
painting-a-grid-with-three-different-colors | Painting a Grid With Three Different Colors | painting-a-grid-with-three-different-col-ifsc | \n# Approach\n Describe your approach to solving the problem. \nGenerate Valid Row Configurations:\n\nEnumerate all possible ways to color a single row of lengt | Ansh1707 | NORMAL | 2024-10-15T07:52:12.108108+00:00 | 2024-10-15T07:52:12.108133+00:00 | 6 | false | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nGenerate Valid Row Configurations:\n\nEnumerate all possible ways to color a single row of length \n\uD835\uDC5A\nm with three colors while ensuring that no two adjacent cells share the same color.\nDetermine Valid Transitions Between Rows:\n\nPreco... | 0 | 0 | ['Python'] | 0 |
painting-a-grid-with-three-different-colors | USING GRAPHS BUT GETTING TIME LIMIT EXCEEDED. | using-graphs-but-getting-time-limit-exce-ia13 | 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 | damon109 | NORMAL | 2024-08-19T21:46:25.691029+00:00 | 2024-08-19T21:46:25.691053+00:00 | 12 | 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)$$ --... | 0 | 0 | ['Dynamic Programming', 'Graph', 'C++'] | 0 |
painting-a-grid-with-three-different-colors | BY USING PREDEFINED STRING WITH ADJACENT MATRIX WITH DP. | by-using-predefined-string-with-adjacent-xgbv | 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 | damon109 | NORMAL | 2024-08-19T21:40:28.521055+00:00 | 2024-08-19T21:40:28.521087+00:00 | 7 | 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)$$ --... | 0 | 0 | ['Dynamic Programming', 'C++'] | 0 |
painting-a-grid-with-three-different-colors | Bit Masking with easy intuitive variable names | bit-masking-with-easy-intuitive-variable-l5lo | 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 | Dhruv_001 | NORMAL | 2024-08-06T12:43:26.425247+00:00 | 2024-08-06T12:43:26.425277+00:00 | 21 | 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: O(N*3^N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N*3^N)\n<!-- Add your space complexity here... | 0 | 0 | ['Dynamic Programming', 'Bitmask', 'C++'] | 0 |
painting-a-grid-with-three-different-colors | Python 3: TC O(8**m log(n)), SC O(4**m): Transition Matrix, Scaling and Squaring | python-3-tc-o8m-logn-sc-o4m-transition-m-dhjr | Intuition\n\nWe want the total ways. A natural subproblem is to get the total ways to get n columns ending with each valid color pattern.\n\nIf we can do that, | biggestchungus | NORMAL | 2024-07-30T21:49:25.234968+00:00 | 2024-07-30T21:49:25.234986+00:00 | 1 | false | # Intuition\n\nWe want the total ways. A natural subproblem is to get the total ways to get `n` columns ending with each valid color pattern.\n\nIf we can do that, then for each color pattern `c`, we can\n* find all the other color patterns `c\'` we can append this column to\n* add up all ways to get `n-1` columns with... | 0 | 0 | ['Python3'] | 0 |
painting-a-grid-with-three-different-colors | 👍Runtime 167 ms Beats 100.00% | runtime-167-ms-beats-10000-by-pvt2024-6pnc | Code\n\nconst mod = 1e9 + 7\n\nfunc colorTheGrid(m int, n int) int {\n\tstate := make(map[int]int64)\n\tdfs(state, 0, m, -1, 0)\n\tset := make([]int, 0, len(sta | pvt2024 | NORMAL | 2024-07-08T03:15:38.413888+00:00 | 2024-07-08T03:15:38.413926+00:00 | 5 | false | # Code\n```\nconst mod = 1e9 + 7\n\nfunc colorTheGrid(m int, n int) int {\n\tstate := make(map[int]int64)\n\tdfs(state, 0, m, -1, 0)\n\tset := make([]int, 0, len(state))\n\tfor k := range state {\n\t\tset = append(set, k)\n\t}\n\n\tfor i := 1; i < n; i++ {\n\t\tdp := make(map[int]int64)\n\t\tfor _, a := range set {\n\t... | 0 | 0 | ['Go'] | 0 |
painting-a-grid-with-three-different-colors | DP | dp-by-yoongyeom-fl1f | Code\n\nclass Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n t = [\'a\', \'b\', \'c\']\n for i in range(m-1):\n new = | YoonGyeom | NORMAL | 2024-05-19T13:16:45.661991+00:00 | 2024-05-19T13:16:45.662023+00:00 | 12 | false | # Code\n```\nclass Solution:\n def colorTheGrid(self, m: int, n: int) -> int:\n t = [\'a\', \'b\', \'c\']\n for i in range(m-1):\n new = []\n for s in t:\n if s[-1] != \'a\': new.append(s+\'a\')\n if s[-1] != \'b\': new.append(s+\'b\')\n ... | 0 | 0 | ['Python3'] | 0 |
painting-a-grid-with-three-different-colors | Simple DP solution✅✅ | simple-dp-solution-by-jayesh_06-fgv1 | \n\n# Code\n\nclass Solution {\npublic:\n int dp[1001][50];\n int mod=1e9+7;\n void genrate(int i,string& s,vector<string>& v,char ch,int m){\n | Jayesh_06 | NORMAL | 2024-05-09T07:44:51.969706+00:00 | 2024-05-09T07:44:51.969742+00:00 | 102 | false | \n\n# Code\n```\nclass Solution {\npublic:\n int dp[1001][50];\n int mod=1e9+7;\n void genrate(int i,string& s,vector<string>& v,char ch,int m){\n if(i==m){\n v.push_back(s);\n return ;\n }\n if(ch!=\'R\'){\n s+=\'R\';\n genrate(i+1,s,v,\'R\',m);\... | 0 | 0 | ['Dynamic Programming', 'C++'] | 0 |
painting-a-grid-with-three-different-colors | Space Optimized DP | space-optimized-dp-by-abhishequmar-kx9u | Code\n\n#include <bits/stdc++.h>\n\n#define pb push_back\n#define po pop_back\n#include <string>\nusing namespace std;\n\ntemplate <typename T>\nstd::istream &o | abhishequmar | NORMAL | 2024-03-24T02:03:17.736917+00:00 | 2024-03-24T02:03:17.736951+00:00 | 21 | false | # Code\n```\n#include <bits/stdc++.h>\n\n#define pb push_back\n#define po pop_back\n#include <string>\nusing namespace std;\n\ntemplate <typename T>\nstd::istream &operator>>(std::istream &in, std::vector<T> &v){for(int i =0;i<v.size();i++){in>>v[i];}return in;}\n\ntemplate <typename A, typename B>\nostream &operator<<... | 0 | 0 | ['Dynamic Programming', 'C++'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.