{"id":7360,"date":"2020-09-12T22:22:59","date_gmt":"2020-09-13T05:22:59","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7360"},"modified":"2020-09-12T22:23:51","modified_gmt":"2020-09-13T05:23:51","slug":"leetcode-1582-special-positions-in-a-binary-matrix","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1582-special-positions-in-a-binary-matrix\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1582. Special Positions in a Binary Matrix"},"content":{"rendered":"\n<p>Given a&nbsp;<code>rows x cols<\/code>&nbsp;matrix&nbsp;<code>mat<\/code>,&nbsp;where&nbsp;<code>mat[i][j]<\/code>&nbsp;is either&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code>,&nbsp;return&nbsp;<em>the number of special positions in&nbsp;<code>mat<\/code>.<\/em><\/p>\n\n\n\n<p>A position&nbsp;<code>(i,j)<\/code>&nbsp;is called&nbsp;<strong>special<\/strong>&nbsp;if&nbsp;<code>mat[i][j] == 1<\/code>&nbsp;and all other elements in row&nbsp;<code>i<\/code>&nbsp;and column&nbsp;<code>j<\/code>&nbsp;are&nbsp;<code>0<\/code>&nbsp;(rows and columns are&nbsp;<strong>0-indexed<\/strong>).<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[1,0,0],\n&nbsp;             [0,0,<strong>1<\/strong>],\n&nbsp;             [1,0,0]]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> (1,2) is a special position because mat[1][2] == 1 and all other elements in row 1 and column 2 are 0.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[<strong>1<\/strong>,0,0],\n&nbsp;             [0,<strong>1<\/strong>,0],\n&nbsp;             [0,0,<strong>1<\/strong>]]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> (0,0), (1,1) and (2,2) are special positions. \n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[0,0,0,<strong>1<\/strong>],\n&nbsp;             [<strong>1<\/strong>,0,0,0],\n&nbsp;             [0,1,1,0],\n&nbsp;             [0,0,0,0]]\n<strong>Output:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[0,0,0,0,0],\n&nbsp;             [<strong>1<\/strong>,0,0,0,0],\n&nbsp;             [0,<strong>1<\/strong>,0,0,0],\n&nbsp;             [0,0,<strong>1<\/strong>,0,0],\n&nbsp;             [0,0,0,1,1]]\n<strong>Output:<\/strong> 3\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>rows == mat.length<\/code><\/li><li><code>cols == mat[i].length<\/code><\/li><li><code>1 &lt;= rows, cols &lt;= 100<\/code><\/li><li><code>mat[i][j]<\/code>&nbsp;is&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sum for each row and column<\/strong><\/h2>\n\n\n\n<p>Brute force:<br>Time complexity: O(R*C*(R+C))<br>Space complexity: O(1)<\/p>\n\n\n\n<p>We can pre-compute the sums for each row and each column, ans = sum(mat[r][c] == 1 and rsum[r] == 1 and csum[c] == 1)<\/p>\n\n\n\n<p>Time complexity: O(R*C)<br>Space complexity: O(R+C)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int numSpecial(vector<vector<int>>& mat) {\n    const int rows = mat.size();\n    const int cols = mat[0].size();\n    vector<int> rs(rows);\n    vector<int> cs(cols);\n    for (int r = 0; r < rows; ++r)\n      for (int c = 0; c < cols; ++c) {\n        rs[r] += mat[r][c];\n        cs[c] += mat[r][c];\n      }\n    int ans = 0;\n    for (int r = 0; r < rows; ++r)\n      for (int c = 0; c < cols; ++c)\n        ans += mat[r][c] &#038;&#038; rs[r] == 1 &#038;&#038; cs[c] == 1;      \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;rows x cols&nbsp;matrix&nbsp;mat,&nbsp;where&nbsp;mat[i][j]&nbsp;is either&nbsp;0&nbsp;or&nbsp;1,&nbsp;return&nbsp;the number of special positions in&nbsp;mat. A position&nbsp;(i,j)&nbsp;is called&nbsp;special&nbsp;if&nbsp;mat[i][j] == 1&nbsp;and all other elements in row&nbsp;i&nbsp;and column&nbsp;j&nbsp;are&nbsp;0&nbsp;(rows and columns are&nbsp;0-indexed). Example&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[222,216,649],"class_list":["post-7360","post","type-post","status-publish","format-standard","hentry","category-array","tag-easy","tag-matrix","tag-pre-compute","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7360"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7360\/revisions"}],"predecessor-version":[{"id":7362,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7360\/revisions\/7362"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}