Skip to content

Commit

Permalink
Added tasks 589, 590, 591, 592
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanhNIT authored Jan 31, 2023
1 parent f98546c commit 137cd19
Show file tree
Hide file tree
Showing 13 changed files with 482 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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
| 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

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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
| 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

#### Day 7 Binary Search
Expand Down Expand Up @@ -1662,6 +1664,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
| 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
| 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
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
| 0592 |[Fraction Addition and Subtraction](src/main/kotlin/g0501_0600/s0592_fraction_addition_and_subtraction/Solution.kt)| Medium | String, Math, Simulation | 164 | 100.00
| 0591 |[Tag Validator](src/main/kotlin/g0501_0600/s0591_tag_validator/Solution.kt)| Hard | String, Stack | 177 | 100.00
| 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
| 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
| 0587 |[Erect the Fence](src/main/kotlin/g0501_0600/s0587_erect_the_fence/Solution.kt)| Hard | Array, Math, Geometry | 470 | 100.00
| 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
| 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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package g0501_0600.s0589_n_ary_tree_preorder_traversal

// #Easy #Depth_First_Search #Tree #Stack #Programming_Skills_I_Day_5_Function #Level_1_Day_6_Tree
// #2023_01_31_Time_233_ms_(84.02%)_Space_39.3_MB_(37.63%)

import com_github_leetcode.Node

/*
* Definition for a Node.
* class Node(var `val`: Int) {
* var children: List<Node?> = listOf()
* }
*/
class Solution {
fun preorder(root: Node?): List<Int> {
val res: MutableList<Int> = ArrayList()
preorderHelper(res, root)
return res
}

private fun preorderHelper(res: MutableList<Int>, root: Node?) {
if (root == null) {
return
}
res.add(root.`val`)
for (node in root.neighbors) {
preorderHelper(res, node)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
589\. N-ary Tree Preorder Traversal

Easy

Given the `root` of an n-ary tree, return _the preorder traversal of its nodes' values_.

Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)

**Example 1:**

![](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png)

**Input:** root = [1,null,3,2,4,null,5,6]

**Output:** [1,3,5,6,2,4]

**Example 2:**

![](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png)

**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]

**Output:** [1,2,3,6,7,11,14,4,8,12,5,9,13,10]

**Constraints:**

* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
* <code>0 <= Node.val <= 10<sup>4</sup></code>
* The height of the n-ary tree is less than or equal to `1000`.

**Follow up:** Recursive solution is trivial, could you do it iteratively?
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package g0501_0600.s0590_n_ary_tree_postorder_traversal

// #Easy #Depth_First_Search #Tree #Stack #2023_01_31_Time_237_ms_(88.10%)_Space_38.9_MB_(76.19%)

import com_github_leetcode.Node

/*
* Definition for a Node.
* class Node(var `val`: Int) {
* var children: List<Node?> = listOf()
* }
*/
class Solution {
private var ans: ArrayList<Int>? = null

fun postorder(root: Node?): List<Int> {
ans = ArrayList()
recursion(root)
if (root != null) {
ans!!.add(root.`val`)
}
return ans as ArrayList<Int>
}

private fun recursion(root: Node?) {
if (root == null) {
return
}
for (child in root.neighbors) {
recursion(child)
ans!!.add(child.`val`)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
590\. N-ary Tree Postorder Traversal

Easy

Given the `root` of an n-ary tree, return _the postorder traversal of its nodes' values_.

Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)

**Example 1:**

![](https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png)

**Input:** root = [1,null,3,2,4,null,5,6]

**Output:** [5,6,3,2,4,1]

**Example 2:**

![](https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png)

**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]

**Output:** [2,6,14,11,7,3,12,8,4,13,9,10,5,1]

**Constraints:**

* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
* <code>0 <= Node.val <= 10<sup>4</sup></code>
* The height of the n-ary tree is less than or equal to `1000`.

**Follow up:** Recursive solution is trivial, could you do it iteratively?
59 changes: 59 additions & 0 deletions src/main/kotlin/g0501_0600/s0591_tag_validator/Solution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package g0501_0600.s0591_tag_validator

// #Hard #String #Stack #2023_01_31_Time_177_ms_(100.00%)_Space_35.4_MB_(100.00%)

import java.util.ArrayDeque
import java.util.Deque

class Solution {
fun isValid(code: String): Boolean {
val stack: Deque<String> = ArrayDeque()
var i = 0
while (i < code.length) {
if (i > 0 && stack.isEmpty()) {
return false
}
if (code.startsWith("<![CDATA[", i)) {
// "<![CDATA[" length is 9
val j = i + 9
i = code.indexOf("]]>", j)
if (i < 0) {
return false
}
// "]]>" length is 3
i += 3
} else if (code.startsWith("</", i)) {
val j = i + 2
i = code.indexOf(">", j)
if (i < 0 || i == j || i - j > 9) {
return false
}
for (k in j until i) {
if (!Character.isUpperCase(code[k])) {
return false
}
}
val s = code.substring(j, i++)
if (stack.isEmpty() || stack.pop() != s) {
return false
}
} else if (code.startsWith("<", i)) {
val j = i + 1
i = code.indexOf(">", j)
if (i < 0 || i == j || i - j > 9) {
return false
}
for (k in j until i) {
if (!Character.isUpperCase(code[k])) {
return false
}
}
val s = code.substring(j, i++)
stack.push(s)
} else {
i++
}
}
return stack.isEmpty()
}
}
59 changes: 59 additions & 0 deletions src/main/kotlin/g0501_0600/s0591_tag_validator/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
591\. Tag Validator

Hard

Given a string representing a code snippet, implement a tag validator to parse the code and return whether it is valid.

A code snippet is valid if all the following rules hold:

1. The code must be wrapped in a **valid closed tag**. Otherwise, the code is invalid.
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.
3. A **valid** `TAG_NAME` only contain **upper-case letters**, and has length in range [1,9]. Otherwise, the `TAG_NAME` is **invalid**.
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**.
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.
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).
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** `]]>`.
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**.

**Example 1:**

**Input:** code = "<DIV>This is the first line <![CDATA[<div>]]></DIV>"

**Output:** true

**Explanation:**

The code is wrapped in a closed tag : <DIV> and </DIV>.
The TAG_NAME is valid, the TAG_CONTENT consists of some characters and cdata.
Although CDATA_CONTENT has an unmatched start tag with invalid TAG_NAME, it should be considered as plain text, not parsed as a tag.
So TAG_CONTENT is valid, and then the code is valid. Thus return true.

**Example 2:**

**Input:** code = "<DIV>>> ![cdata[]] <![CDATA[<div>]>]]>]]>>]</DIV>"

**Output:** true

**Explanation:**

We first separate the code into : start_tag|tag_content|end_tag.
start_tag -> "<DIV>"
end_tag -> "</DIV>"
tag_content could also be separated into : text1|cdata|text2.
text1 -> ">> ![cdata[]] "
cdata -> "<![CDATA[<div>]>]]>", where the CDATA_CONTENT is "<div>]>"
text2 -> "]]>>]" The reason why start_tag is NOT "<DIV>>>" is because of the rule 6.
The reason why cdata is NOT "<![CDATA[<div>]>]]>]]>" is because of the rule 7.

**Example 3:**

**Input:** code = "<A> <B> </A> </B>"

**Output:** false

**Explanation:** Unbalanced. If "<A>" is closed, then "<B>" must be unmatched, and vice versa.

**Constraints:**

* `1 <= code.length <= 500`
* `code` consists of English letters, digits, `'<'`, `'>'`, `'/'`, `'!'`, `'['`, `']'`, `'.'`, and `' '`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package g0501_0600.s0592_fraction_addition_and_subtraction

// #Medium #String #Math #Simulation #2023_01_31_Time_164_ms_(100.00%)_Space_35.9_MB_(66.67%)

class Solution {
private fun gcd(a: Int, b: Int): Int {
return if (a % b == 0) b else gcd(b, a % b)
}

private fun format(a: Int, b: Int): String {
val gcd = Math.abs(gcd(a, b))
return (a / gcd).toString() + "/" + b / gcd
}

private fun parse(s: String): IntArray {
val idx = s.indexOf("/")
return intArrayOf(s.substring(0, idx).toInt(), s.substring(idx + 1).toInt())
}

fun fractionAddition(expression: String): String {
var rst = intArrayOf(0, 1)
val list: MutableList<IntArray> = ArrayList()
var sb = StringBuilder().append(expression[0])
for (i in 1 until expression.length) {
val c = expression[i]
if (c == '+' || c == '-') {
list.add(parse(sb.toString()))
sb = StringBuilder().append(c)
} else {
sb.append(c)
}
}
list.add(parse(sb.toString()))
for (num in list) {
rst = intArrayOf(rst[0] * num[1] + rst[1] * num[0], rst[1] * num[1])
}
return format(rst[0], rst[1])
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
592\. Fraction Addition and Subtraction

Medium

Given a string `expression` representing an expression of fraction addition and subtraction, return the calculation result in string format.

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`.

**Example 1:**

**Input:** expression = "-1/2+1/2"

**Output:** "0/1"

**Example 2:**

**Input:** expression = "-1/2+1/2+1/3"

**Output:** "1/3"

**Example 3:**

**Input:** expression = "1/3-1/2"

**Output:** "-1/6"

**Constraints:**

* The input string only contains `'0'` to `'9'`, `'/'`, `'+'` and `'-'`. So does the output.
* Each fraction (input and output) has the format `±numerator/denominator`. If the first input fraction or the output is positive, then `'+'` will be omitted.
* 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.
* The number of given fractions will be in the range `[1, 10]`.
* The numerator and denominator of the **final result** are guaranteed to be valid and in the range of **32-bit** int.
Loading

0 comments on commit 137cd19

Please sign in to comment.