Skip to content

Commit c97ea8f

Browse files
committedApr 20, 2025
Add solution #1591
1 parent 4b23891 commit c97ea8f

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,389 LeetCode solutions in JavaScript
1+
# 1,390 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1214,6 +1214,7 @@
12141214
1588|[Sum of All Odd Length Subarrays](./solutions/1588-sum-of-all-odd-length-subarrays.js)|Easy|
12151215
1589|[Maximum Sum Obtained of Any Permutation](./solutions/1589-maximum-sum-obtained-of-any-permutation.js)|Medium|
12161216
1590|[Make Sum Divisible by P](./solutions/1590-make-sum-divisible-by-p.js)|Medium|
1217+
1591|[Strange Printer II](./solutions/1591-strange-printer-ii.js)|Hard|
12171218
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
12181219
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12191220
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|

‎solutions/1591-strange-printer-ii.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* 1591. Strange Printer II
3+
* https://leetcode.com/problems/strange-printer-ii/
4+
* Difficulty: Hard
5+
*
6+
* There is a strange printer with the following two special requirements:
7+
* - On each turn, the printer will print a solid rectangular pattern of a single color on the
8+
* grid. This will cover up the existing colors in the rectangle.
9+
* - Once the printer has used a color for the above operation, the same color cannot be used again.
10+
*
11+
* You are given a m x n matrix targetGrid, where targetGrid[row][col] is the color in the position
12+
* (row, col) of the grid.
13+
*
14+
* Return true if it is possible to print the matrix targetGrid, otherwise, return false.
15+
*/
16+
17+
/**
18+
* @param {number[][]} targetGrid
19+
* @return {boolean}
20+
*/
21+
var isPrintable = function(targetGrid) {
22+
const rows = targetGrid.length;
23+
const cols = targetGrid[0].length;
24+
const colorBounds = new Map();
25+
26+
for (let color = 1; color <= 60; color++) {
27+
let minRow = rows;
28+
let maxRow = -1;
29+
let minCol = cols;
30+
let maxCol = -1;
31+
32+
for (let r = 0; r < rows; r++) {
33+
for (let c = 0; c < cols; c++) {
34+
if (targetGrid[r][c] === color) {
35+
minRow = Math.min(minRow, r);
36+
maxRow = Math.max(maxRow, r);
37+
minCol = Math.min(minCol, c);
38+
maxCol = Math.max(maxCol, c);
39+
}
40+
}
41+
}
42+
if (maxRow >= 0) {
43+
colorBounds.set(color, [minRow, maxRow, minCol, maxCol]);
44+
}
45+
}
46+
47+
const dependencies = new Map();
48+
for (const [color, [minRow, maxRow, minCol, maxCol]] of colorBounds) {
49+
const deps = new Set();
50+
for (let r = minRow; r <= maxRow; r++) {
51+
for (let c = minCol; c <= maxCol; c++) {
52+
if (targetGrid[r][c] !== color) {
53+
deps.add(targetGrid[r][c]);
54+
}
55+
}
56+
}
57+
dependencies.set(color, deps);
58+
}
59+
60+
const visited = new Set();
61+
const recStack = new Set();
62+
63+
function hasCycle(color) {
64+
if (recStack.has(color)) return true;
65+
if (visited.has(color)) return false;
66+
67+
visited.add(color);
68+
recStack.add(color);
69+
70+
for (const dep of dependencies.get(color) || []) {
71+
if (hasCycle(dep)) return true;
72+
}
73+
74+
recStack.delete(color);
75+
return false;
76+
}
77+
78+
for (const color of colorBounds.keys()) {
79+
if (hasCycle(color)) return false;
80+
}
81+
82+
return true;
83+
};

0 commit comments

Comments
 (0)
Please sign in to comment.