Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3430100

Browse files
committedApr 8, 2025
Add solution #1284
1 parent 4f99b2c commit 3430100

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-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,208 LeetCode solutions in JavaScript
1+
# 1,209 LeetCode solutions in JavaScript
22

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

@@ -969,6 +969,7 @@
969969
1276|[Number of Burgers with No Waste of Ingredients](./solutions/1276-number-of-burgers-with-no-waste-of-ingredients.js)|Medium|
970970
1277|[Count Square Submatrices with All Ones](./solutions/1277-count-square-submatrices-with-all-ones.js)|Medium|
971971
1278|[Palindrome Partitioning III](./solutions/1278-palindrome-partitioning-iii.js)|Hard|
972+
1284|[Minimum Number of Flips to Convert Binary Matrix to Zero Matrix](./solutions/1284-minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.js)|Hard|
972973
1287|[Element Appearing More Than 25% In Sorted Array](./solutions/1287-element-appearing-more-than-25-in-sorted-array.js)|Easy|
973974
1290|[Convert Binary Number in a Linked List to Integer](./solutions/1290-convert-binary-number-in-a-linked-list-to-integer.js)|Easy|
974975
1291|[Sequential Digits](./solutions/1291-sequential-digits.js)|Medium|
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* 1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix
3+
* https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/
4+
* Difficulty: Hard
5+
*
6+
* Given a m x n binary matrix mat. In one step, you can choose one cell and flip it and all the
7+
* four neighbors of it if they exist (Flip is changing 1 to 0 and 0 to 1). A pair of cells are
8+
* called neighbors if they share one edge.
9+
*
10+
* Return the minimum number of steps required to convert mat to a zero matrix or -1 if you cannot.
11+
*
12+
* A binary matrix is a matrix with all cells equal to 0 or 1 only.
13+
*
14+
* A zero matrix is a matrix with all cells equal to 0.
15+
*/
16+
17+
/**
18+
* @param {number[][]} mat
19+
* @return {number}
20+
*/
21+
var minFlips = function(mat) {
22+
const rows = mat.length;
23+
const cols = mat[0].length;
24+
const target = 0;
25+
const start = mat.flat().reduce((acc, val, idx) => acc | (val << idx), 0);
26+
const queue = [[start, 0]];
27+
const seen = new Set([start]);
28+
const directions = [[0, 0], [0, 1], [0, -1], [1, 0], [-1, 0]];
29+
30+
while (queue.length) {
31+
const [state, steps] = queue.shift();
32+
33+
if (state === target) return steps;
34+
35+
for (let i = 0; i < rows; i++) {
36+
for (let j = 0; j < cols; j++) {
37+
let nextState = state;
38+
for (const [di, dj] of directions) {
39+
const ni = i + di;
40+
const nj = j + dj;
41+
if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
42+
const pos = ni * cols + nj;
43+
nextState ^= (1 << pos);
44+
}
45+
}
46+
if (!seen.has(nextState)) {
47+
seen.add(nextState);
48+
queue.push([nextState, steps + 1]);
49+
}
50+
}
51+
}
52+
}
53+
54+
return -1;
55+
};

0 commit comments

Comments
 (0)
Please sign in to comment.