Skip to content

Commit

Permalink
[leetcode] 1591. strange-printer-ii.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
cycheng committed Sep 27, 2020
1 parent 979ab58 commit ecf5db1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
#### [Biweekly 35](https://leetcode.com/contest/biweekly-contest-35/ranking)
* (5) [1589. maximum-sum-obtained-of-any-permutation.cpp](https://github.com/cycheng/coding-for-fun/blob/master/leetcode/1589.%20maximum-sum-obtained-of-any-permutation.cpp)
* (5) [1590. make-sum-divisible-by-p.cpp](https://github.com/cycheng/coding-for-fun/blob/master/leetcode/1590.%20make-sum-divisible-by-p.cpp)
* (6) [1591. strange-printer-ii.cpp](https://github.com/cycheng/coding-for-fun/blob/master/leetcode/1591.%20strange-printer-ii.cpp)

#### [2019 Facebook Hacker Cup](https://www.facebook.com/codingcompetitions/hacker-cup/2019)
* [Qualification Round](https://www.facebook.com/codingcompetitions/hacker-cup/2019/qualification-round)
Expand Down
63 changes: 63 additions & 0 deletions leetcode/1591. strange-printer-ii.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// https://leetcode.com/contest/biweekly-contest-35/problems/strange-printer-ii/
//
// #biweekly 35
// #6, #topological order, #greedy, #like

// Given a [m x n] matrix, the grid value means color.
//
// Each turn, we can print a solid rectangular of a color in the matrix.
// The same color cannot be used again.
//
// Given a [m x n] matrix targetGrid, return true if it doesn't volatile the
// above rule, else return false.
#include "std.h"

enum {
UNINIT,
VISITED,
CHECKED,
};
bool dfsHasLoop(const unordered_set<int> edges[61], vector<int> &state,
int color) {
state[color] = VISITED;
for (int v : edges[color]) {
if (state[v] == CHECKED)
continue;
if (state[v] == VISITED)
return true;
if (dfsHasLoop(edges, state, v))
return true;
}
state[color] = CHECKED;
return false;
}

bool isPrintable(vector<vector<int>> &targetGrid) {
const int m = targetGrid.size(), n = targetGrid[0].size();
unordered_set<int> edges[61];
// greedy, O(CMN)
for (int color = 1; color <= 60; ++color) {
int top = m, left = n, right = 0, bottom = 0;
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (color == targetGrid[i][j]) {
top = min(top, i);
left = min(left, j);
right = max(right, j);
bottom = max(bottom, i);
}

// find overlapped grid
for (int i = top; i <= bottom; ++i)
for (int j = left; j <= right; ++j)
if (color != targetGrid[i][j])
edges[color].insert(targetGrid[i][j]);
}

// dfs check loop
vector<int> state(61);
for (int color = 1; color <= 60; ++color)
if (dfsHasLoop(edges, state, color))
return false;
return true;
}

0 comments on commit ecf5db1

Please sign in to comment.