Skip to content

Commit 3159d01

Browse files
authored
Added tasks 558, 559, 561, 563
1 parent 8ac210c commit 3159d01

File tree

14 files changed

+462
-0
lines changed

14 files changed

+462
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,11 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16551655
| 0763 |[Partition Labels](src/main/kotlin/g0701_0800/s0763_partition_labels/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Greedy, Two_Pointers, Data_Structure_II_Day_7_String | 235 | 84.75
16561656
| 0739 |[Daily Temperatures](src/main/kotlin/g0701_0800/s0739_daily_temperatures/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_6 | 936 | 80.54
16571657
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
1658+
| 0563 |[Binary Tree Tilt](src/main/kotlin/g0501_0600/s0563_binary_tree_tilt/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree | 197 | 100.00
1659+
| 0561 |[Array Partition](src/main/kotlin/g0501_0600/s0561_array_partition_i/Solution.kt)| Easy | Array, Sorting, Greedy, Counting_Sort | 337 | 90.48
16581660
| 0560 |[Subarray Sum Equals K](src/main/kotlin/g0501_0600/s0560_subarray_sum_equals_k/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array | 692 | 53.27
1661+
| 0559 |[Maximum Depth of N-ary Tree](src/main/kotlin/g0501_0600/s0559_maximum_depth_of_n_ary_tree/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree | 196 | 100.00
1662+
| 0558 |[Logical OR of Two Binary Grids Represented as Quad-Trees](src/main/kotlin/g0501_0600/s0558_logical_or_of_two_binary_grids_represented_as_quad_trees/Solution.kt)| Medium | Tree, Divide_and_Conquer | 268 | 100.00
16591663
| 0557 |[Reverse Words in a String III](src/main/kotlin/g0501_0600/s0557_reverse_words_in_a_string_iii/Solution.kt)| Easy | String, Two_Pointers, Algorithm_I_Day_4_Two_Pointers | 215 | 98.10
16601664
| 0556 |[Next Greater Element III](src/main/kotlin/g0501_0600/s0556_next_greater_element_iii/Solution.kt)| Medium | String, Math, Two_Pointers, Programming_Skills_II_Day_10 | 137 | 80.00
16611665
| 0554 |[Brick Wall](src/main/kotlin/g0501_0600/s0554_brick_wall/Solution.kt)| Medium | Array, Hash_Table | 307 | 100.00
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g0501_0600.s0558_logical_or_of_two_binary_grids_represented_as_quad_trees
2+
3+
/*
4+
* Definition for a QuadTree node.
5+
* class Node(var `val`: Boolean, var isLeaf: Boolean) {
6+
* var topLeft: Node? = null
7+
* var topRight: Node? = null
8+
* var bottomLeft: Node? = null
9+
* var bottomRight: Node? = null
10+
* }
11+
*/
12+
13+
class Node {
14+
var `val` = false
15+
var isLeaf = false
16+
var topLeft: Node? = null
17+
var topRight: Node? = null
18+
var bottomLeft: Node? = null
19+
var bottomRight: Node? = null
20+
21+
constructor() {
22+
// empty constructor
23+
}
24+
25+
constructor(`val`: Boolean, isLeaf: Boolean) {
26+
this.`val` = `val`
27+
this.isLeaf = isLeaf
28+
topLeft = null
29+
topRight = null
30+
bottomLeft = null
31+
bottomRight = null
32+
}
33+
34+
override fun toString(): String {
35+
return (
36+
getNode(this) +
37+
getNode(topLeft) +
38+
getNode(topRight) +
39+
getNode(bottomLeft) +
40+
getNode(bottomRight)
41+
)
42+
}
43+
44+
private fun getNode(node: Node?): String {
45+
val isLeafLocal = if (node!!.isLeaf) "1" else "0"
46+
val valLocal = if (node.`val`) "1" else "0"
47+
return "[$isLeafLocal,$valLocal]"
48+
}
49+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g0501_0600.s0558_logical_or_of_two_binary_grids_represented_as_quad_trees
2+
3+
// #Medium #Tree #Divide_and_Conquer #2023_01_20_Time_268_ms_(100.00%)_Space_44.2_MB_(100.00%)
4+
5+
/*
6+
* Definition for a QuadTree node.
7+
* class Node(var `val`: Boolean, var isLeaf: Boolean) {
8+
* var topLeft: Node? = null
9+
* var topRight: Node? = null
10+
* var bottomLeft: Node? = null
11+
* var bottomRight: Node? = null
12+
* }
13+
*/
14+
15+
class Solution {
16+
fun intersect(quadTree1: Node?, quadTree2: Node?): Node? {
17+
if (quadTree1!!.isLeaf) {
18+
return if (quadTree1.`val`) quadTree1 else quadTree2
19+
}
20+
if (quadTree2!!.isLeaf) {
21+
return if (quadTree2.`val`) quadTree2 else quadTree1
22+
}
23+
val out = Node()
24+
val tl: Node? = intersect(quadTree1.topLeft, quadTree2.topLeft)
25+
val tr: Node? = intersect(quadTree1.topRight, quadTree2.topRight)
26+
val bl: Node? = intersect(quadTree1.bottomLeft, quadTree2.bottomLeft)
27+
val br: Node? = intersect(quadTree1.bottomRight, quadTree2.bottomRight)
28+
if ((
29+
tl!!.isLeaf &&
30+
tr!!.isLeaf &&
31+
bl!!.isLeaf &&
32+
br!!.isLeaf && tl.`val` == tr.`val`
33+
) && tr.`val` == bl.`val` && br.`val` == bl.`val`
34+
) {
35+
out.isLeaf = true
36+
out.`val` = tl.`val`
37+
} else {
38+
out.topLeft = tl
39+
out.topRight = tr
40+
out.bottomLeft = bl
41+
out.bottomRight = br
42+
}
43+
return out
44+
}
45+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
558\. Logical OR of Two Binary Grids Represented as Quad-Trees
2+
3+
Medium
4+
5+
A Binary Matrix is a matrix in which all the elements are either **0** or **1**.
6+
7+
Given `quadTree1` and `quadTree2`. `quadTree1` represents a `n * n` binary matrix and `quadTree2` represents another `n * n` binary matrix.
8+
9+
Return _a Quad-Tree_ representing the `n * n` binary matrix which is the result of **logical bitwise OR** of the two binary matrixes represented by `quadTree1` and `quadTree2`.
10+
11+
Notice that you can assign the value of a node to **True** or **False** when `isLeaf` is **False**, and both are **accepted** in the answer.
12+
13+
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
14+
15+
* `val`: True if the node represents a grid of 1's or False if the node represents a grid of 0's.
16+
* `isLeaf`: True if the node is leaf node on the tree or False if the node has the four children.
17+
18+
class Node {
19+
public boolean val;
20+
public boolean isLeaf;
21+
public Node topLeft;
22+
public Node topRight;
23+
public Node bottomLeft;
24+
public Node bottomRight;
25+
}
26+
27+
We can construct a Quad-Tree from a two-dimensional area using the following steps:
28+
29+
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 the value of the grid and set the four children to Null and stop.
30+
2. If the current grid has different values, set `isLeaf` to False and set `val` to any value and divide the current grid into four sub-grids as shown in the photo.
31+
3. Recurse for each of the children with the proper sub-grid.
32+
33+
![](https://assets.leetcode.com/uploads/2020/02/11/new_top.png)
34+
35+
If you want to know more about the Quad-Tree, you can refer to the [wiki](https://en.wikipedia.org/wiki/Quadtree).
36+
37+
**Quad-Tree format:**
38+
39+
The input/output represents the serialized format of a Quad-Tree using level order traversal, where `null` signifies a path terminator where no node exists below.
40+
41+
It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list `[isLeaf, val]`.
42+
43+
If the value of `isLeaf` or `val` is True we represent it as **1** in the list `[isLeaf, val]` and if the value of `isLeaf` or `val` is False we represent it as **0**.
44+
45+
**Example 1:**
46+
47+
![](https://assets.leetcode.com/uploads/2020/02/11/qt1.png) ![](https://assets.leetcode.com/uploads/2020/02/11/qt2.png)
48+
49+
**Input:** quadTree1 = [[0,1],[1,1],[1,1],[1,0],[1,0]] , quadTree2 = [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]
50+
51+
**Output:** [[0,0],[1,1],[1,1],[1,1],[1,0]]
52+
53+
**Explanation:** quadTree1 and quadTree2 are shown above. You can see the binary matrix which is represented by each Quad-Tree. If we apply logical bitwise OR on the two binary matrices we get the binary matrix below which is represented by the result Quad-Tree. Notice that the binary matrices shown are only for illustration, you don't have to construct the binary matrix to get the result tree. ![](https://assets.leetcode.com/uploads/2020/02/11/qtr.png)
54+
55+
**Example 2:**
56+
57+
**Input:** quadTree1 = [[1,0]], quadTree2 = [[1,0]]
58+
59+
**Output:** [[1,0]]
60+
61+
**Explanation:** Each tree represents a binary matrix of size 1\*1. Each matrix contains only zero. The resulting matrix is of size 1\*1 with also zero.
62+
63+
**Constraints:**
64+
65+
* `quadTree1` and `quadTree2` are both **valid** Quad-Trees each representing a `n * n` grid.
66+
* <code>n == 2<sup>x</sup></code> where `0 <= x <= 9`.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g0501_0600.s0559_maximum_depth_of_n_ary_tree
2+
3+
// #Easy #Depth_First_Search #Breadth_First_Search #Tree
4+
// #2023_01_21_Time_196_ms_(100.00%)_Space_37.6_MB_(92.31%)
5+
6+
import com_github_leetcode.Node
7+
8+
/*
9+
* Definition for a Node.
10+
* class Node(var `val`: Int) {
11+
* var neighbors: List<Node?> = listOf()
12+
* }
13+
*/
14+
15+
class Solution {
16+
private var max = 0
17+
18+
fun maxDepth(root: Node?): Int {
19+
if (root == null) {
20+
return 0
21+
}
22+
if (root.neighbors.isEmpty()) {
23+
return 1
24+
}
25+
for (child in root.neighbors) {
26+
findDepth(child, 1)
27+
}
28+
return max
29+
}
30+
31+
private fun findDepth(n: Node?, d: Int) {
32+
if (!n!!.neighbors.isEmpty()) {
33+
for (no in n.neighbors) {
34+
findDepth(no, d + 1)
35+
}
36+
} else {
37+
max = Math.max(max, d + 1)
38+
}
39+
}
40+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
559\. Maximum Depth of N-ary Tree
2+
3+
Easy
4+
5+
Given a n-ary tree, find its maximum depth.
6+
7+
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
8+
9+
_Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples)._
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png)
14+
15+
**Input:** root = [1,null,3,2,4,null,5,6]
16+
17+
**Output:** 3
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png)
22+
23+
**Input:** root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
24+
25+
**Output:** 5
26+
27+
**Constraints:**
28+
29+
* The total number of nodes is in the range <code>[0, 10<sup>4</sup>]</code>.
30+
* The depth of the n-ary tree is less than or equal to `1000`.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0501_0600.s0561_array_partition_i
2+
3+
// #Easy #Array #Sorting #Greedy #Counting_Sort
4+
// #2023_01_21_Time_337_ms_(90.48%)_Space_41.1_MB_(97.62%)
5+
6+
class Solution {
7+
fun arrayPairSum(nums: IntArray): Int {
8+
nums.sort()
9+
var sum = 0
10+
var i = 0
11+
while (i < nums.size - 1) {
12+
sum += Math.min(nums[i], nums[i + 1])
13+
i = i + 2
14+
}
15+
return sum
16+
}
17+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
561\. Array Partition
2+
3+
Easy
4+
5+
Given an integer array `nums` of `2n` integers, group these integers into `n` pairs <code>(a<sub>1</sub>, b<sub>1</sub>), (a<sub>2</sub>, b<sub>2</sub>), ..., (a<sub>n</sub>, b<sub>n</sub>)</code> such that the sum of <code>min(a<sub>i</sub>, b<sub>i</sub>)</code> for all `i` is **maximized**. Return _the maximized sum_.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,4,3,2]
10+
11+
**Output:** 4
12+
13+
**Explanation:** All possible pairings (ignoring the ordering of elements) are:
14+
1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3
15+
2. (1, 3), (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3
16+
3. (1, 2), (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4 So the maximum possible sum is 4.
17+
18+
**Example 2:**
19+
20+
**Input:** nums = [6,2,6,5,1,2]
21+
22+
**Output:** 9
23+
24+
**Explanation:** The optimal pairing is (2, 1), (2, 5), (6, 6). min(2, 1) + min(2, 5) + min(6, 6) = 1 + 2 + 6 = 9.
25+
26+
**Constraints:**
27+
28+
* <code>1 <= n <= 10<sup>4</sup></code>
29+
* `nums.length == 2 * n`
30+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0501_0600.s0563_binary_tree_tilt
2+
3+
// #Easy #Depth_First_Search #Tree #Binary_Tree
4+
// #2023_01_21_Time_197_ms_(100.00%)_Space_35.8_MB_(75.00%)
5+
6+
import com_github_leetcode.TreeNode
7+
8+
/*
9+
* Example:
10+
* var ti = TreeNode(5)
11+
* var v = ti.`val`
12+
* Definition for a binary tree node.
13+
* class TreeNode(var `val`: Int) {
14+
* var left: TreeNode? = null
15+
* var right: TreeNode? = null
16+
* }
17+
*/
18+
class Solution {
19+
private var sum = 0
20+
private fun sumTilt(root: TreeNode?): Int {
21+
if (root == null) {
22+
return 0
23+
}
24+
val ls = sumTilt(root.left)
25+
val rs = sumTilt(root.right)
26+
sum += Math.abs(ls - rs)
27+
return ls + rs + root.`val`
28+
}
29+
30+
fun findTilt(root: TreeNode?): Int {
31+
sum = 0
32+
sumTilt(root)
33+
return sum
34+
}
35+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
563\. Binary Tree Tilt
2+
3+
Easy
4+
5+
Given the `root` of a binary tree, return _the sum of every tree node's **tilt**._
6+
7+
The **tilt** of a tree node is the **absolute difference** between the sum of all left subtree node **values** and all right subtree node **values**. If a node does not have a left child, then the sum of the left subtree node **values** is treated as `0`. The rule is similar if the node does not have a right child.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/20/tilt1.jpg)
12+
13+
**Input:** root = [1,2,3]
14+
15+
**Output:** 1
16+
17+
**Explanation:**
18+
19+
Tilt of node 2 : |0-0| = 0 (no children)
20+
Tilt of node 3 : |0-0| = 0 (no children)
21+
Tilt of node 1 : |2-3| = 1 (left subtree is just left child, so sum is 2; right subtree is just right child, so sum is 3)
22+
Sum of every tilt : 0 + 0 + 1 = 1
23+
24+
**Example 2:**
25+
26+
![](https://assets.leetcode.com/uploads/2020/10/20/tilt2.jpg)
27+
28+
**Input:** root = [4,2,9,3,5,null,7]
29+
30+
**Output:** 15
31+
32+
**Explanation:**
33+
34+
Tilt of node 3 : |0-0| = 0 (no children)
35+
Tilt of node 5 : |0-0| = 0 (no children)
36+
Tilt of node 7 : |0-0| = 0 (no children)
37+
Tilt of node 2 : |3-5| = 2 (left subtree is just left child, so sum is 3; right subtree is just right child, so sum is 5)
38+
Tilt of node 9 : |0-7| = 7 (no left child, so sum is 0; right subtree is just right child, so sum is 7)
39+
Tilt of node 4 : |(3+5+2)-(9+7)| = |10-16| = 6 (left subtree values are 3, 5, and 2, which sums to 10; right subtree values are 9 and 7, which sums to 16)
40+
Sum of every tilt : 0 + 0 + 0 + 2 + 7 + 6 = 15
41+
42+
**Example 3:**
43+
44+
![](https://assets.leetcode.com/uploads/2020/10/20/tilt3.jpg)
45+
46+
**Input:** root = [21,7,14,1,1,2,2,3,3]
47+
48+
**Output:** 9
49+
50+
**Constraints:**
51+
52+
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
53+
* `-1000 <= Node.val <= 1000`

0 commit comments

Comments
 (0)