diff --git a/README.md b/README.md index 0244069be..3b2fcf5c5 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/src/main/kotlin/g0501_0600/s0589_n_ary_tree_preorder_traversal/Solution.kt b/src/main/kotlin/g0501_0600/s0589_n_ary_tree_preorder_traversal/Solution.kt new file mode 100644 index 000000000..73f7fc39c --- /dev/null +++ b/src/main/kotlin/g0501_0600/s0589_n_ary_tree_preorder_traversal/Solution.kt @@ -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 = listOf() + * } + */ +class Solution { + fun preorder(root: Node?): List { + val res: MutableList = ArrayList() + preorderHelper(res, root) + return res + } + + private fun preorderHelper(res: MutableList, root: Node?) { + if (root == null) { + return + } + res.add(root.`val`) + for (node in root.neighbors) { + preorderHelper(res, node) + } + } +} diff --git a/src/main/kotlin/g0501_0600/s0589_n_ary_tree_preorder_traversal/readme.md b/src/main/kotlin/g0501_0600/s0589_n_ary_tree_preorder_traversal/readme.md new file mode 100644 index 000000000..952f974af --- /dev/null +++ b/src/main/kotlin/g0501_0600/s0589_n_ary_tree_preorder_traversal/readme.md @@ -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 [0, 104]. +* 0 <= Node.val <= 104 +* 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? \ No newline at end of file diff --git a/src/main/kotlin/g0501_0600/s0590_n_ary_tree_postorder_traversal/Solution.kt b/src/main/kotlin/g0501_0600/s0590_n_ary_tree_postorder_traversal/Solution.kt new file mode 100644 index 000000000..4a39175d7 --- /dev/null +++ b/src/main/kotlin/g0501_0600/s0590_n_ary_tree_postorder_traversal/Solution.kt @@ -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 = listOf() + * } + */ +class Solution { + private var ans: ArrayList? = null + + fun postorder(root: Node?): List { + ans = ArrayList() + recursion(root) + if (root != null) { + ans!!.add(root.`val`) + } + return ans as ArrayList + } + + private fun recursion(root: Node?) { + if (root == null) { + return + } + for (child in root.neighbors) { + recursion(child) + ans!!.add(child.`val`) + } + } +} diff --git a/src/main/kotlin/g0501_0600/s0590_n_ary_tree_postorder_traversal/readme.md b/src/main/kotlin/g0501_0600/s0590_n_ary_tree_postorder_traversal/readme.md new file mode 100644 index 000000000..99d09cae0 --- /dev/null +++ b/src/main/kotlin/g0501_0600/s0590_n_ary_tree_postorder_traversal/readme.md @@ -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 [0, 104]. +* 0 <= Node.val <= 104 +* 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? \ No newline at end of file diff --git a/src/main/kotlin/g0501_0600/s0591_tag_validator/Solution.kt b/src/main/kotlin/g0501_0600/s0591_tag_validator/Solution.kt new file mode 100644 index 000000000..b99d76861 --- /dev/null +++ b/src/main/kotlin/g0501_0600/s0591_tag_validator/Solution.kt @@ -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 = ArrayDeque() + var i = 0 + while (i < code.length) { + if (i > 0 && stack.isEmpty()) { + return false + } + if (code.startsWith("", j) + if (i < 0) { + return false + } + // "]]>" length is 3 + i += 3 + } else if (code.startsWith("", 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() + } +} diff --git a/src/main/kotlin/g0501_0600/s0591_tag_validator/readme.md b/src/main/kotlin/g0501_0600/s0591_tag_validator/readme.md new file mode 100644 index 000000000..7f7a55e28 --- /dev/null +++ b/src/main/kotlin/g0501_0600/s0591_tag_validator/readme.md @@ -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_CONTENT`. Among them, `` is the start tag, and `` 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 `` should be parsed as TAG\_NAME (not necessarily valid). +7. The cdata has the following format : ``. The range of `CDATA_CONTENT` is defined as the characters between ``. +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 = "
This is the first line ]]>
" + +**Output:** true + +**Explanation:** + + The code is wrapped in a closed tag :
and
. + 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 = "
>> ![cdata[]] ]>]]>]]>>]
" + +**Output:** true + +**Explanation:** + + We first separate the code into : start_tag|tag_content|end_tag. + start_tag -> "
" + end_tag -> "
" + tag_content could also be separated into : text1|cdata|text2. + text1 -> ">> ![cdata[]] " + cdata -> "]>]]>", where the CDATA_CONTENT is "
]>" + text2 -> "]]>>]" The reason why start_tag is NOT "
>>" is because of the rule 6. + The reason why cdata is NOT "]>]]>]]>" is because of the rule 7. + +**Example 3:** + +**Input:** code = " " + +**Output:** false + +**Explanation:** Unbalanced. If "" is closed, then "" must be unmatched, and vice versa. + +**Constraints:** + +* `1 <= code.length <= 500` +* `code` consists of English letters, digits, `'<'`, `'>'`, `'/'`, `'!'`, `'['`, `']'`, `'.'`, and `' '`. \ No newline at end of file diff --git a/src/main/kotlin/g0501_0600/s0592_fraction_addition_and_subtraction/Solution.kt b/src/main/kotlin/g0501_0600/s0592_fraction_addition_and_subtraction/Solution.kt new file mode 100644 index 000000000..1c7ad8e2f --- /dev/null +++ b/src/main/kotlin/g0501_0600/s0592_fraction_addition_and_subtraction/Solution.kt @@ -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 = 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]) + } +} diff --git a/src/main/kotlin/g0501_0600/s0592_fraction_addition_and_subtraction/readme.md b/src/main/kotlin/g0501_0600/s0592_fraction_addition_and_subtraction/readme.md new file mode 100644 index 000000000..adc71178f --- /dev/null +++ b/src/main/kotlin/g0501_0600/s0592_fraction_addition_and_subtraction/readme.md @@ -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. \ No newline at end of file diff --git a/src/test/kotlin/g0501_0600/s0589_n_ary_tree_preorder_traversal/SolutionTest.kt b/src/test/kotlin/g0501_0600/s0589_n_ary_tree_preorder_traversal/SolutionTest.kt new file mode 100644 index 000000000..64681c5b7 --- /dev/null +++ b/src/test/kotlin/g0501_0600/s0589_n_ary_tree_preorder_traversal/SolutionTest.kt @@ -0,0 +1,56 @@ +package g0501_0600.s0589_n_ary_tree_preorder_traversal + +import com_github_leetcode.Node +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun preorder() { + val node1 = Node(1) + val node2 = Node(2) + val node3 = Node(3) + val node4 = Node(4) + node1.neighbors = listOf(node3, node2, node4) + val node5 = Node(5) + val node6 = Node(6) + node3.neighbors = listOf(node5, node6) + assertThat(Solution().preorder(node1), equalTo(listOf(1, 3, 5, 6, 2, 4))) + } + + @Test + fun preorder2() { + val node1 = Node(1) + val node2 = Node(2) + val node3 = Node(3) + val node4 = Node(4) + val node5 = Node(5) + val node6 = Node(6) + val node7 = Node(7) + val node8 = Node(8) + val node9 = Node(9) + val node10 = Node(10) + val node11 = Node(11) + val node12 = Node(12) + val node13 = Node(13) + val node14 = Node(14) + node11.neighbors = listOf(node14) + node7.neighbors = listOf(node11) + node3.neighbors = listOf(node6, node7) + node8.neighbors = listOf(node12) + node4.neighbors = listOf(node8) + node9.neighbors = listOf(node13) + node5.neighbors = listOf(node9, node10) + node1.neighbors = listOf(node2, node3, node4, node5) + assertThat( + Solution().preorder(node1), + equalTo(listOf(1, 2, 3, 6, 7, 11, 14, 4, 8, 12, 5, 9, 13, 10)) + ) + } + + @Test + fun preorder3() { + assertThat(Solution().preorder(null), equalTo(listOf())) + } +} diff --git a/src/test/kotlin/g0501_0600/s0590_n_ary_tree_postorder_traversal/SolutionTest.kt b/src/test/kotlin/g0501_0600/s0590_n_ary_tree_postorder_traversal/SolutionTest.kt new file mode 100644 index 000000000..3cdfbf940 --- /dev/null +++ b/src/test/kotlin/g0501_0600/s0590_n_ary_tree_postorder_traversal/SolutionTest.kt @@ -0,0 +1,56 @@ +package g0501_0600.s0590_n_ary_tree_postorder_traversal + +import com_github_leetcode.Node +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun postorder() { + val node1 = Node(1) + val node2 = Node(2) + val node3 = Node(3) + val node4 = Node(4) + node1.neighbors = listOf(node3, node2, node4) + val node5 = Node(5) + val node6 = Node(6) + node3.neighbors = listOf(node5, node6) + assertThat(Solution().postorder(node1), equalTo(listOf(5, 6, 3, 2, 4, 1))) + } + + @Test + fun postorder2() { + val node1 = Node(1) + val node2 = Node(2) + val node3 = Node(3) + val node4 = Node(4) + val node5 = Node(5) + val node6 = Node(6) + val node7 = Node(7) + val node8 = Node(8) + val node9 = Node(9) + val node10 = Node(10) + val node11 = Node(11) + val node12 = Node(12) + val node13 = Node(13) + val node14 = Node(14) + node11.neighbors = listOf(node14) + node7.neighbors = listOf(node11) + node3.neighbors = listOf(node6, node7) + node8.neighbors = listOf(node12) + node4.neighbors = listOf(node8) + node9.neighbors = listOf(node13) + node5.neighbors = listOf(node9, node10) + node1.neighbors = listOf(node2, node3, node4, node5) + assertThat( + Solution().postorder(node1), + equalTo(listOf(2, 6, 14, 11, 7, 3, 12, 8, 4, 13, 9, 10, 5, 1)) + ) + } + + @Test + fun postorder3() { + assertThat(Solution().postorder(null), equalTo(listOf())) + } +} diff --git a/src/test/kotlin/g0501_0600/s0591_tag_validator/SolutionTest.kt b/src/test/kotlin/g0501_0600/s0591_tag_validator/SolutionTest.kt new file mode 100644 index 000000000..0cd1d113f --- /dev/null +++ b/src/test/kotlin/g0501_0600/s0591_tag_validator/SolutionTest.kt @@ -0,0 +1,26 @@ +package g0501_0600.s0591_tag_validator + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun isValid() { + assertThat( + Solution().isValid("
This is the first line ]]>
"), + equalTo(true) + ) + } + + @Test + fun isValid2() { + assertThat( + Solution() + .isValid( + "This is the first line ]]>" + ), + equalTo(false) + ) + } +} diff --git a/src/test/kotlin/g0501_0600/s0592_fraction_addition_and_subtraction/SolutionTest.kt b/src/test/kotlin/g0501_0600/s0592_fraction_addition_and_subtraction/SolutionTest.kt new file mode 100644 index 000000000..2e4e21713 --- /dev/null +++ b/src/test/kotlin/g0501_0600/s0592_fraction_addition_and_subtraction/SolutionTest.kt @@ -0,0 +1,22 @@ +package g0501_0600.s0592_fraction_addition_and_subtraction + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun fractionAddition() { + assertThat(Solution().fractionAddition("-1/2+1/2"), equalTo("0/1")) + } + + @Test + fun fractionAddition2() { + assertThat(Solution().fractionAddition("-1/2+1/2+1/3"), equalTo("1/3")) + } + + @Test + fun fractionAddition3() { + assertThat(Solution().fractionAddition("1/3-1/2"), equalTo("-1/6")) + } +}