Skip to content

Commit 137cd19

Browse files
authored
Added tasks 589, 590, 591, 592
1 parent f98546c commit 137cd19

File tree

13 files changed

+482
-0
lines changed

13 files changed

+482
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
7777

7878
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
7979
|-|-|-|-|-|-
80+
| 0589 |[N-ary Tree Preorder Traversal](src/main/kotlin/g0501_0600/s0589_n_ary_tree_preorder_traversal/Solution.kt)| Easy | Depth_First_Search, Tree, Stack | 233 | 84.02
8081
| 0496 |[Next Greater Element I](src/main/kotlin/g0401_0500/s0496_next_greater_element_i/Solution.kt)| Easy | Array, Hash_Table, Stack, Monotonic_Stack | 171 | 100.00
8182

8283
#### Day 6 Array
@@ -429,6 +430,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
429430

430431
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
431432
|-|-|-|-|-|-
433+
| 0589 |[N-ary Tree Preorder Traversal](src/main/kotlin/g0501_0600/s0589_n_ary_tree_preorder_traversal/Solution.kt)| Easy | Depth_First_Search, Tree, Stack | 233 | 84.02
432434
| 0102 |[Binary Tree Level Order Traversal](src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 332 | 67.53
433435

434436
#### Day 7 Binary Search
@@ -1662,6 +1664,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16621664
| 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
16631665
| 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
16641666
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
1667+
| 0592 |[Fraction Addition and Subtraction](src/main/kotlin/g0501_0600/s0592_fraction_addition_and_subtraction/Solution.kt)| Medium | String, Math, Simulation | 164 | 100.00
1668+
| 0591 |[Tag Validator](src/main/kotlin/g0501_0600/s0591_tag_validator/Solution.kt)| Hard | String, Stack | 177 | 100.00
1669+
| 0590 |[N-ary Tree Postorder Traversal](src/main/kotlin/g0501_0600/s0590_n_ary_tree_postorder_traversal/Solution.kt)| Easy | Depth_First_Search, Tree, Stack | 237 | 88.10
1670+
| 0589 |[N-ary Tree Preorder Traversal](src/main/kotlin/g0501_0600/s0589_n_ary_tree_preorder_traversal/Solution.kt)| Easy | Depth_First_Search, Tree, Stack, Programming_Skills_I_Day_5_Function, Level_1_Day_6_Tree | 233 | 84.02
16651671
| 0587 |[Erect the Fence](src/main/kotlin/g0501_0600/s0587_erect_the_fence/Solution.kt)| Hard | Array, Math, Geometry | 470 | 100.00
16661672
| 0586 |[Customer Placing the Largest Number of Orders](src/main/kotlin/g0501_0600/s0586_customer_placing_the_largest_number_of_orders/script.sql)| Easy | LeetCode_Curated_SQL_70, Database, SQL_I_Day_8_Function | 768 | 44.85
16671673
| 0584 |[Find Customer Referee](src/main/kotlin/g0501_0600/s0584_find_customer_referee/script.sql)| Easy | Database, SQL_I_Day_1_Select | 779 | 43.48
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g0501_0600.s0589_n_ary_tree_preorder_traversal
2+
3+
// #Easy #Depth_First_Search #Tree #Stack #Programming_Skills_I_Day_5_Function #Level_1_Day_6_Tree
4+
// #2023_01_31_Time_233_ms_(84.02%)_Space_39.3_MB_(37.63%)
5+
6+
import com_github_leetcode.Node
7+
8+
/*
9+
* Definition for a Node.
10+
* class Node(var `val`: Int) {
11+
* var children: List<Node?> = listOf()
12+
* }
13+
*/
14+
class Solution {
15+
fun preorder(root: Node?): List<Int> {
16+
val res: MutableList<Int> = ArrayList()
17+
preorderHelper(res, root)
18+
return res
19+
}
20+
21+
private fun preorderHelper(res: MutableList<Int>, root: Node?) {
22+
if (root == null) {
23+
return
24+
}
25+
res.add(root.`val`)
26+
for (node in root.neighbors) {
27+
preorderHelper(res, node)
28+
}
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
589\. N-ary Tree Preorder Traversal
2+
3+
Easy
4+
5+
Given the `root` of an n-ary tree, return _the preorder traversal of its nodes' values_.
6+
7+
Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png)
12+
13+
**Input:** root = [1,null,3,2,4,null,5,6]
14+
15+
**Output:** [1,3,5,6,2,4]
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png)
20+
21+
**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]
22+
23+
**Output:** [1,2,3,6,7,11,14,4,8,12,5,9,13,10]
24+
25+
**Constraints:**
26+
27+
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
28+
* <code>0 <= Node.val <= 10<sup>4</sup></code>
29+
* The height of the n-ary tree is less than or equal to `1000`.
30+
31+
**Follow up:** Recursive solution is trivial, could you do it iteratively?
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0501_0600.s0590_n_ary_tree_postorder_traversal
2+
3+
// #Easy #Depth_First_Search #Tree #Stack #2023_01_31_Time_237_ms_(88.10%)_Space_38.9_MB_(76.19%)
4+
5+
import com_github_leetcode.Node
6+
7+
/*
8+
* Definition for a Node.
9+
* class Node(var `val`: Int) {
10+
* var children: List<Node?> = listOf()
11+
* }
12+
*/
13+
class Solution {
14+
private var ans: ArrayList<Int>? = null
15+
16+
fun postorder(root: Node?): List<Int> {
17+
ans = ArrayList()
18+
recursion(root)
19+
if (root != null) {
20+
ans!!.add(root.`val`)
21+
}
22+
return ans as ArrayList<Int>
23+
}
24+
25+
private fun recursion(root: Node?) {
26+
if (root == null) {
27+
return
28+
}
29+
for (child in root.neighbors) {
30+
recursion(child)
31+
ans!!.add(child.`val`)
32+
}
33+
}
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
590\. N-ary Tree Postorder Traversal
2+
3+
Easy
4+
5+
Given the `root` of an n-ary tree, return _the postorder traversal of its nodes' values_.
6+
7+
Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png)
12+
13+
**Input:** root = [1,null,3,2,4,null,5,6]
14+
15+
**Output:** [5,6,3,2,4,1]
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png)
20+
21+
**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]
22+
23+
**Output:** [2,6,14,11,7,3,12,8,4,13,9,10,5,1]
24+
25+
**Constraints:**
26+
27+
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
28+
* <code>0 <= Node.val <= 10<sup>4</sup></code>
29+
* The height of the n-ary tree is less than or equal to `1000`.
30+
31+
**Follow up:** Recursive solution is trivial, could you do it iteratively?
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package g0501_0600.s0591_tag_validator
2+
3+
// #Hard #String #Stack #2023_01_31_Time_177_ms_(100.00%)_Space_35.4_MB_(100.00%)
4+
5+
import java.util.ArrayDeque
6+
import java.util.Deque
7+
8+
class Solution {
9+
fun isValid(code: String): Boolean {
10+
val stack: Deque<String> = ArrayDeque()
11+
var i = 0
12+
while (i < code.length) {
13+
if (i > 0 && stack.isEmpty()) {
14+
return false
15+
}
16+
if (code.startsWith("<![CDATA[", i)) {
17+
// "<![CDATA[" length is 9
18+
val j = i + 9
19+
i = code.indexOf("]]>", j)
20+
if (i < 0) {
21+
return false
22+
}
23+
// "]]>" length is 3
24+
i += 3
25+
} else if (code.startsWith("</", i)) {
26+
val j = i + 2
27+
i = code.indexOf(">", j)
28+
if (i < 0 || i == j || i - j > 9) {
29+
return false
30+
}
31+
for (k in j until i) {
32+
if (!Character.isUpperCase(code[k])) {
33+
return false
34+
}
35+
}
36+
val s = code.substring(j, i++)
37+
if (stack.isEmpty() || stack.pop() != s) {
38+
return false
39+
}
40+
} else if (code.startsWith("<", i)) {
41+
val j = i + 1
42+
i = code.indexOf(">", j)
43+
if (i < 0 || i == j || i - j > 9) {
44+
return false
45+
}
46+
for (k in j until i) {
47+
if (!Character.isUpperCase(code[k])) {
48+
return false
49+
}
50+
}
51+
val s = code.substring(j, i++)
52+
stack.push(s)
53+
} else {
54+
i++
55+
}
56+
}
57+
return stack.isEmpty()
58+
}
59+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
591\. Tag Validator
2+
3+
Hard
4+
5+
Given a string representing a code snippet, implement a tag validator to parse the code and return whether it is valid.
6+
7+
A code snippet is valid if all the following rules hold:
8+
9+
1. The code must be wrapped in a **valid closed tag**. Otherwise, the code is invalid.
10+
2. A **closed tag** (not necessarily valid) has exactly the following format : `<TAG_NAME>TAG_CONTENT</TAG_NAME>`. Among them, `<TAG_NAME>` is the start tag, and `</TAG_NAME>` is the end tag. The TAG\_NAME in start and end tags should be the same. A closed tag is **valid** if and only if the TAG\_NAME and TAG\_CONTENT are valid.
11+
3. A **valid** `TAG_NAME` only contain **upper-case letters**, and has length in range [1,9]. Otherwise, the `TAG_NAME` is **invalid**.
12+
4. A **valid** `TAG_CONTENT` may contain other **valid closed tags**, **cdata** and any characters (see note1) **EXCEPT** unmatched `<`, unmatched start and end tag, and unmatched or closed tags with invalid TAG\_NAME. Otherwise, the `TAG_CONTENT` is **invalid**.
13+
5. A start tag is unmatched if no end tag exists with the same TAG\_NAME, and vice versa. However, you also need to consider the issue of unbalanced when tags are nested.
14+
6. A `<` is unmatched if you cannot find a subsequent `>`. And when you find a `<` or `</`, all the subsequent characters until the next `>` should be parsed as TAG\_NAME (not necessarily valid).
15+
7. The cdata has the following format : `<![CDATA[CDATA_CONTENT]]>`. The range of `CDATA_CONTENT` is defined as the characters between `<![CDATA[` and the **first subsequent** `]]>`.
16+
8. `CDATA_CONTENT` may contain **any characters**. The function of cdata is to forbid the validator to parse `CDATA_CONTENT`, so even it has some characters that can be parsed as tag (no matter valid or invalid), you should treat it as **regular characters**.
17+
18+
**Example 1:**
19+
20+
**Input:** code = "<DIV>This is the first line <![CDATA[<div>]]></DIV>"
21+
22+
**Output:** true
23+
24+
**Explanation:**
25+
26+
The code is wrapped in a closed tag : <DIV> and </DIV>.
27+
The TAG_NAME is valid, the TAG_CONTENT consists of some characters and cdata.
28+
Although CDATA_CONTENT has an unmatched start tag with invalid TAG_NAME, it should be considered as plain text, not parsed as a tag.
29+
So TAG_CONTENT is valid, and then the code is valid. Thus return true.
30+
31+
**Example 2:**
32+
33+
**Input:** code = "<DIV>>> ![cdata[]] <![CDATA[<div>]>]]>]]>>]</DIV>"
34+
35+
**Output:** true
36+
37+
**Explanation:**
38+
39+
We first separate the code into : start_tag|tag_content|end_tag.
40+
start_tag -> "<DIV>"
41+
end_tag -> "</DIV>"
42+
tag_content could also be separated into : text1|cdata|text2.
43+
text1 -> ">> ![cdata[]] "
44+
cdata -> "<![CDATA[<div>]>]]>", where the CDATA_CONTENT is "<div>]>"
45+
text2 -> "]]>>]" The reason why start_tag is NOT "<DIV>>>" is because of the rule 6.
46+
The reason why cdata is NOT "<![CDATA[<div>]>]]>]]>" is because of the rule 7.
47+
48+
**Example 3:**
49+
50+
**Input:** code = "<A> <B> </A> </B>"
51+
52+
**Output:** false
53+
54+
**Explanation:** Unbalanced. If "<A>" is closed, then "<B>" must be unmatched, and vice versa.
55+
56+
**Constraints:**
57+
58+
* `1 <= code.length <= 500`
59+
* `code` consists of English letters, digits, `'<'`, `'>'`, `'/'`, `'!'`, `'['`, `']'`, `'.'`, and `' '`.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g0501_0600.s0592_fraction_addition_and_subtraction
2+
3+
// #Medium #String #Math #Simulation #2023_01_31_Time_164_ms_(100.00%)_Space_35.9_MB_(66.67%)
4+
5+
class Solution {
6+
private fun gcd(a: Int, b: Int): Int {
7+
return if (a % b == 0) b else gcd(b, a % b)
8+
}
9+
10+
private fun format(a: Int, b: Int): String {
11+
val gcd = Math.abs(gcd(a, b))
12+
return (a / gcd).toString() + "/" + b / gcd
13+
}
14+
15+
private fun parse(s: String): IntArray {
16+
val idx = s.indexOf("/")
17+
return intArrayOf(s.substring(0, idx).toInt(), s.substring(idx + 1).toInt())
18+
}
19+
20+
fun fractionAddition(expression: String): String {
21+
var rst = intArrayOf(0, 1)
22+
val list: MutableList<IntArray> = ArrayList()
23+
var sb = StringBuilder().append(expression[0])
24+
for (i in 1 until expression.length) {
25+
val c = expression[i]
26+
if (c == '+' || c == '-') {
27+
list.add(parse(sb.toString()))
28+
sb = StringBuilder().append(c)
29+
} else {
30+
sb.append(c)
31+
}
32+
}
33+
list.add(parse(sb.toString()))
34+
for (num in list) {
35+
rst = intArrayOf(rst[0] * num[1] + rst[1] * num[0], rst[1] * num[1])
36+
}
37+
return format(rst[0], rst[1])
38+
}
39+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
592\. Fraction Addition and Subtraction
2+
3+
Medium
4+
5+
Given a string `expression` representing an expression of fraction addition and subtraction, return the calculation result in string format.
6+
7+
The final result should be an [irreducible fraction](https://en.wikipedia.org/wiki/Irreducible_fraction). If your final result is an integer, change it to the format of a fraction that has a denominator `1`. So in this case, `2` should be converted to `2/1`.
8+
9+
**Example 1:**
10+
11+
**Input:** expression = "-1/2+1/2"
12+
13+
**Output:** "0/1"
14+
15+
**Example 2:**
16+
17+
**Input:** expression = "-1/2+1/2+1/3"
18+
19+
**Output:** "1/3"
20+
21+
**Example 3:**
22+
23+
**Input:** expression = "1/3-1/2"
24+
25+
**Output:** "-1/6"
26+
27+
**Constraints:**
28+
29+
* The input string only contains `'0'` to `'9'`, `'/'`, `'+'` and `'-'`. So does the output.
30+
* Each fraction (input and output) has the format `±numerator/denominator`. If the first input fraction or the output is positive, then `'+'` will be omitted.
31+
* The input only contains valid **irreducible fractions**, where the **numerator** and **denominator** of each fraction will always be in the range `[1, 10]`. If the denominator is `1`, it means this fraction is actually an integer in a fraction format defined above.
32+
* The number of given fractions will be in the range `[1, 10]`.
33+
* The numerator and denominator of the **final result** are guaranteed to be valid and in the range of **32-bit** int.

0 commit comments

Comments
 (0)