Skip to content

Commit 4059a0f

Browse files
committed
Add solution #558
1 parent 49dac9b commit 4059a0f

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@
442442
554|[Brick Wall](./0554-brick-wall.js)|Medium|
443443
556|[Next Greater Element III](./0556-next-greater-element-iii.js)|Medium|
444444
557|[Reverse Words in a String III](./0557-reverse-words-in-a-string-iii.js)|Easy|
445+
558|[Logical OR of Two Binary Grids Represented as Quad-Trees](./0558-logical-or-of-two-binary-grids-represented-as-quad-trees.js)|Medium|
445446
560|[Subarray Sum Equals K](./0560-subarray-sum-equals-k.js)|Medium|
446447
563|[Binary Tree Tilt](./0563-binary-tree-tilt.js)|Easy|
447448
565|[Array Nesting](./0565-array-nesting.js)|Medium|
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* 558. Logical OR of Two Binary Grids Represented as Quad-Trees
3+
* https://leetcode.com/problems/logical-or-of-two-binary-grids-represented-as-quad-trees/
4+
* Difficulty: Medium
5+
*
6+
* A Binary Matrix is a matrix in which all the elements are either 0 or 1.
7+
*
8+
* Given quadTree1 and quadTree2. quadTree1 represents a n * n binary matrix and quadTree2
9+
* represents another n * n binary matrix.
10+
*
11+
* Return a Quad-Tree representing the n * n binary matrix which is the result of logical
12+
* bitwise OR of the two binary matrixes represented by quadTree1 and quadTree2.
13+
*
14+
* Notice that you can assign the value of a node to True or False when isLeaf is False, and
15+
* both are accepted in the answer.
16+
*
17+
* A Quad-Tree is a tree data structure in which each internal node has exactly four children.
18+
* Besides, each node has two attributes:
19+
* - val: True if the node represents a grid of 1's or False if the node represents a grid of 0's.
20+
* - isLeaf: True if the node is leaf node on the tree or False if the node has the four children.
21+
* class Node {
22+
* public boolean val;
23+
* public boolean isLeaf;
24+
* public Node topLeft;
25+
* public Node topRight;
26+
* public Node bottomLeft;
27+
* public Node bottomRight;
28+
* }.
29+
*
30+
* We can construct a Quad-Tree from a two-dimensional area using the following steps:
31+
* 1. If the current grid has the same value (i.e all 1's or all 0's) set isLeaf True and set val to
32+
* the value of the grid and set the four children to Null and stop.
33+
* 2. If the current grid has different values, set isLeaf to False and set val to any value and
34+
* divide the current grid into four sub-grids as shown in the photo.
35+
* 3. Recurse for each of the children with the proper sub-grid.
36+
*/
37+
38+
/**
39+
* // Definition for a QuadTree node.
40+
* function _Node(val,isLeaf,topLeft,topRight,bottomLeft,bottomRight) {
41+
* this.val = val;
42+
* this.isLeaf = isLeaf;
43+
* this.topLeft = topLeft;
44+
* this.topRight = topRight;
45+
* this.bottomLeft = bottomLeft;
46+
* this.bottomRight = bottomRight;
47+
* };
48+
*/
49+
50+
/**
51+
* @param {_Node} quadTree1
52+
* @param {_Node} quadTree2
53+
* @return {_Node}
54+
*/
55+
var intersect = function(quadTree1, quadTree2) {
56+
if (quadTree1.isLeaf) return quadTree1.val ? quadTree1 : quadTree2;
57+
if (quadTree2.isLeaf) return quadTree2.val ? quadTree2 : quadTree1;
58+
59+
const topLeft = intersect(quadTree1.topLeft, quadTree2.topLeft);
60+
const topRight = intersect(quadTree1.topRight, quadTree2.topRight);
61+
const bottomLeft = intersect(quadTree1.bottomLeft, quadTree2.bottomLeft);
62+
const bottomRight = intersect(quadTree1.bottomRight, quadTree2.bottomRight);
63+
64+
if (topLeft.isLeaf && topRight.isLeaf && bottomLeft.isLeaf && bottomRight.isLeaf
65+
&& topLeft.val === topRight.val && topRight.val === bottomLeft.val
66+
&& bottomLeft.val === bottomRight.val) {
67+
return new _Node(topLeft.val, true, null, null, null, null);
68+
}
69+
70+
return new _Node(false, false, topLeft, topRight, bottomLeft, bottomRight);
71+
};

0 commit comments

Comments
 (0)