{"id":7396,"date":"2020-09-20T23:12:17","date_gmt":"2020-09-21T06:12:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7396"},"modified":"2020-09-20T23:13:57","modified_gmt":"2020-09-21T06:13:57","slug":"leetcode-1591-strange-printer-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1591-strange-printer-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1591. Strange Printer II"},"content":{"rendered":"\n<p>There is a strange printer with the following two special requirements:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>On each turn, the printer will print a solid rectangular pattern of a single color on the grid. This will cover up the existing colors in the rectangle.<\/li><li>Once the printer has used a color for the above operation,&nbsp;<strong>the same color cannot be used again<\/strong>.<\/li><\/ul>\n\n\n\n<p>You are given a&nbsp;<code>m x n<\/code>&nbsp;matrix&nbsp;<code>targetGrid<\/code>, where&nbsp;<code>targetGrid[row][col]<\/code>&nbsp;is the color in the position&nbsp;<code>(row, col)<\/code>&nbsp;of the grid.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code><em>&nbsp;if it is possible to print the matrix&nbsp;<\/em><code>targetGrid<\/code><em>,<\/em><em>&nbsp;otherwise, return&nbsp;<\/em><code>false<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/08\/15\/sample_1_1929.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> targetGrid = [[1,1,1,1],[1,2,2,1],[1,2,2,1],[1,1,1,1]]\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/08\/15\/sample_2_1929.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> targetGrid = [[1,1,1,1],[1,1,3,3],[1,1,3,4],[5,5,1,4]]\n<strong>Output:<\/strong> true\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> targetGrid = [[1,2,1],[2,1,2],[1,2,1]]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> It is impossible to form targetGrid because it is not allowed to print the same color in different turns.<\/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> targetGrid = [[1,1,1],[3,1,3]]\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == targetGrid.length<\/code><\/li><li><code>n == targetGrid[i].length<\/code><\/li><li><code>1 &lt;= m, n &lt;= 60<\/code><\/li><li><code>1 &lt;= targetGrid[row][col] &lt;= 60<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Dependency graph<\/strong><\/h2>\n\n\n\n<p>For each color C find the maximum rectangle to cover it. Any other color C&#8217; in this rectangle is a dependency of C, e.g. C&#8217; must be print first in order to print C.<\/p>\n\n\n\n<p>Then this problem reduced to check if there is any cycle in the dependency graph.<\/p>\n\n\n\n<p>e.g. <br>1 2 1<br>2 1 2<br>1 2 1<br>The maximum rectangle for 1 and 2 are both [0, 0] ~ [2, 2]. 1 depends on 2, and 2 depends on 1. This is a circular reference and no way to print.<\/p>\n\n\n\n<p>Time complexity: O(C*M*N)<br>Space complexity: O(C*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  bool isPrintable(vector<vector<int>>& targetGrid) {\n    constexpr int kMaxC = 60;\n    const int m = targetGrid.size();\n    const int n = targetGrid[0].size();\n    vector<unordered_set<int>> deps(kMaxC + 1);\n    for (int c = 1; c <= kMaxC; ++c) {\n      int l = n, r = -1, t = m, b = -1;      \n      for (int i = 0; i < m; ++i)\n        for (int j = 0; j < n; ++j)\n          if (targetGrid[i][j] == c)\n            l = min(l, j), r = max(r, j), t = min(t, i), b = max(b, i);\n      if (l == -1) continue;\n      for (int i = t; i <= b; ++i)\n        for (int j = l; j <= r; ++j)\n          if (targetGrid[i][j] != c) \n            deps[c].insert(targetGrid[i][j]);\n    }\n    vector<int> seen(kMaxC + 1);\n    function<bool(int)> hasCycle = [&](int c) {\n      if (seen[c] == 1) return true;\n      if (seen[c] == 2) return false;\n      seen[c] = 1;\n      for (int t : deps[c])\n        if (hasCycle(t)) return true;\n      seen[c] = 2;\n      return false;\n    };\n    for (int c = 1; c <= kMaxC; ++c)\n      if (hasCycle(c)) return false;\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is a strange printer with the following two special requirements: On each turn, the printer will print a solid rectangular pattern of a single&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[33,77,217,137],"class_list":["post-7396","post","type-post","status-publish","format-standard","hentry","category-graph","tag-dfs","tag-graph","tag-hard","tag-topological-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7396","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=7396"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7396\/revisions"}],"predecessor-version":[{"id":7398,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7396\/revisions\/7398"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}