Skip to content

Commit 039bf6f

Browse files
authored
Added tasks 1021, 1022, 1023, 1024
1 parent 4cecadb commit 039bf6f

File tree

13 files changed

+489
-0
lines changed

13 files changed

+489
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,6 +1750,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17501750
| # | Title | Difficulty | Tag | Time, ms | Time, %
17511751
|------|----------------|-------------|-------------|----------|---------
17521752
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
1753+
| 1024 |[Video Stitching](src/main/kotlin/g1001_1100/s1024_video_stitching/Solution.kt)| Medium | Array, Dynamic_Programming, Greedy | 141 | 100.00
1754+
| 1023 |[Camelcase Matching](src/main/kotlin/g1001_1100/s1023_camelcase_matching/Solution.kt)| Medium | String, Two_Pointers, Trie, String_Matching | 149 | 60.00
1755+
| 1022 |[Remove Outermost Parentheses](src/main/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree | 158 | 88.89
1756+
| 1021 |[Remove Outermost Parentheses](src/main/kotlin/g1001_1100/s1021_remove_outermost_parentheses/Solution.kt)| Easy | String, Stack | 156 | 60.00
17531757
| 1020 |[Number of Enclaves](src/main/kotlin/g1001_1100/s1020_number_of_enclaves/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Graph_Theory_I_Day_3_Matrix_Related_Problems | 369 | 76.26
17541758
| 1019 |[Next Greater Node In Linked List](src/main/kotlin/g1001_1100/s1019_next_greater_node_in_linked_list/Solution.kt)| Medium | Array, Stack, Linked_List, Monotonic_Stack | 472 | 75.00
17551759
| 1018 |[Binary Prefix Divisible By 5](src/main/kotlin/g1001_1100/s1018_binary_prefix_divisible_by_5/Solution.kt)| Easy | Array | 297 | 100.00
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g1001_1100.s1021_remove_outermost_parentheses
2+
3+
// #Easy #String #Stack #2023_05_22_Time_156_ms_(60.00%)_Space_37.3_MB_(40.00%)
4+
5+
class Solution {
6+
fun removeOuterParentheses(s: String): String {
7+
val primitives: MutableList<String> = ArrayList()
8+
var i = 1
9+
while (i < s.length) {
10+
val initialI = i - 1
11+
var left = 1
12+
while (i < s.length && left > 0) {
13+
if (s[i] == '(') {
14+
left++
15+
} else {
16+
left--
17+
}
18+
i++
19+
}
20+
primitives.add(s.substring(initialI, i))
21+
i++
22+
}
23+
val sb = StringBuilder()
24+
for (primitive in primitives) {
25+
sb.append(primitive, 1, primitive.length - 1)
26+
}
27+
return sb.toString()
28+
}
29+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
1021\. Remove Outermost Parentheses
2+
3+
Easy
4+
5+
A valid parentheses string is either empty `""`, `"(" + A + ")"`, or `A + B`, where `A` and `B` are valid parentheses strings, and `+` represents string concatenation.
6+
7+
* For example, `""`, `"()"`, `"(())()"`, and `"(()(()))"` are all valid parentheses strings.
8+
9+
A valid parentheses string `s` is primitive if it is nonempty, and there does not exist a way to split it into `s = A + B`, with `A` and `B` nonempty valid parentheses strings.
10+
11+
Given a valid parentheses string `s`, consider its primitive decomposition: <code>s = P<sub>1</sub> + P<sub>2</sub> + ... + P<sub>k</sub></code>, where <code>P<sub>i</sub></code> are primitive valid parentheses strings.
12+
13+
Return `s` _after removing the outermost parentheses of every primitive string in the primitive decomposition of_ `s`.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "(()())(())"
18+
19+
**Output:** "()()()"
20+
21+
**Explanation:** The input string is "(()())(())", with primitive decomposition "(()())" + "(())". After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
22+
23+
**Example 2:**
24+
25+
**Input:** s = "(()())(())(()(()))"
26+
27+
**Output:** "()()()()(())"
28+
29+
**Explanation:** The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))". After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".
30+
31+
**Example 3:**
32+
33+
**Input:** s = "()()"
34+
35+
**Output:** ""
36+
37+
**Explanation:** The input string is "()()", with primitive decomposition "()" + "()". After removing outer parentheses of each part, this is "" + "" = "".
38+
39+
**Constraints:**
40+
41+
* <code>1 <= s.length <= 10<sup>5</sup></code>
42+
* `s[i]` is either `'('` or `')'`.
43+
* `s` is a valid parentheses string.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g1001_1100.s1022_sum_of_root_to_leaf_binary_numbers
2+
3+
// #Easy #Depth_First_Search #Tree #Binary_Tree
4+
// #2023_05_22_Time_158_ms_(88.89%)_Space_36.3_MB_(11.11%)
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+
fun sumRootToLeaf(root: TreeNode?): Int {
20+
val paths: MutableList<List<Int>> = ArrayList()
21+
dfs(root, paths, ArrayList())
22+
var sum = 0
23+
for (list in paths) {
24+
var num = 0
25+
for (i in list) {
26+
num = (num shl 1) + i
27+
}
28+
sum += num
29+
}
30+
return sum
31+
}
32+
33+
private fun dfs(root: TreeNode?, paths: MutableList<List<Int>>, path: MutableList<Int>) {
34+
path.add(root!!.`val`)
35+
if (root.left != null) {
36+
dfs(root.left!!, paths, path)
37+
path.removeAt(path.size - 1)
38+
}
39+
if (root.right != null) {
40+
dfs(root.right!!, paths, path)
41+
path.removeAt(path.size - 1)
42+
}
43+
if (root.left == null && root.right == null) {
44+
paths.add(ArrayList(path))
45+
}
46+
}
47+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
1021\. Remove Outermost Parentheses
2+
3+
Easy
4+
5+
A valid parentheses string is either empty `""`, `"(" + A + ")"`, or `A + B`, where `A` and `B` are valid parentheses strings, and `+` represents string concatenation.
6+
7+
* For example, `""`, `"()"`, `"(())()"`, and `"(()(()))"` are all valid parentheses strings.
8+
9+
A valid parentheses string `s` is primitive if it is nonempty, and there does not exist a way to split it into `s = A + B`, with `A` and `B` nonempty valid parentheses strings.
10+
11+
Given a valid parentheses string `s`, consider its primitive decomposition: <code>s = P<sub>1</sub> + P<sub>2</sub> + ... + P<sub>k</sub></code>, where <code>P<sub>i</sub></code> are primitive valid parentheses strings.
12+
13+
Return `s` _after removing the outermost parentheses of every primitive string in the primitive decomposition of_ `s`.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "(()())(())"
18+
19+
**Output:** "()()()"
20+
21+
**Explanation:** The input string is "(()())(())", with primitive decomposition "(()())" + "(())". After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
22+
23+
**Example 2:**
24+
25+
**Input:** s = "(()())(())(()(()))"
26+
27+
**Output:** "()()()()(())"
28+
29+
**Explanation:** The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))". After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".
30+
31+
**Example 3:**
32+
33+
**Input:** s = "()()"
34+
35+
**Output:** ""
36+
37+
**Explanation:** The input string is "()()", with primitive decomposition "()" + "()". After removing outer parentheses of each part, this is "" + "" = "".
38+
39+
**Constraints:**
40+
41+
* <code>1 <= s.length <= 10<sup>5</sup></code>
42+
* `s[i]` is either `'('` or `')'`.
43+
* `s` is a valid parentheses string.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g1001_1100.s1023_camelcase_matching
2+
3+
// #Medium #String #Two_Pointers #Trie #String_Matching
4+
// #2023_05_22_Time_149_ms_(60.00%)_Space_35.1_MB_(40.00%)
5+
6+
import java.util.LinkedList
7+
8+
class Solution {
9+
fun camelMatch(queries: Array<String>, pattern: String): List<Boolean> {
10+
val ret: MutableList<Boolean> = LinkedList()
11+
for (query in queries) {
12+
ret.add(check(query, pattern))
13+
}
14+
return ret
15+
}
16+
17+
private fun check(query: String, pattern: String): Boolean {
18+
val patternLen = pattern.length
19+
var patternPos = 0
20+
var uppercaseCount = 0
21+
for (element in query) {
22+
val c = element
23+
if (Character.isUpperCase(c)) {
24+
if (patternPos < patternLen && c != pattern[patternPos]) {
25+
return false
26+
}
27+
uppercaseCount++
28+
if (uppercaseCount > patternLen) {
29+
return false
30+
}
31+
patternPos++
32+
} else {
33+
if (patternPos < patternLen && c == pattern[patternPos]) {
34+
patternPos++
35+
}
36+
}
37+
}
38+
return patternPos == patternLen
39+
}
40+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
1023\. Camelcase Matching
2+
3+
Medium
4+
5+
Given an array of strings `queries` and a string `pattern`, return a boolean array `answer` where `answer[i]` is `true` if `queries[i]` matches `pattern`, and `false` otherwise.
6+
7+
A query word `queries[i]` matches `pattern` if you can insert lowercase English letters pattern so that it equals the query. You may insert each character at any position and you may not insert any characters.
8+
9+
**Example 1:**
10+
11+
**Input:** queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"
12+
13+
**Output:** [true,false,true,true,false]
14+
15+
**Explanation:** "FooBar" can be generated like this "F" + "oo" + "B" + "ar". "FootBall" can be generated like this "F" + "oot" + "B" + "all". "FrameBuffer" can be generated like this "F" + "rame" + "B" + "uffer".
16+
17+
**Example 2:**
18+
19+
**Input:** queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa"
20+
21+
**Output:** [true,false,true,false,false]
22+
23+
**Explanation:** "FooBar" can be generated like this "Fo" + "o" + "Ba" + "r". "FootBall" can be generated like this "Fo" + "ot" + "Ba" + "ll".
24+
25+
**Example 3:**
26+
27+
**Input:** queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT"
28+
29+
**Output:** [false,true,false,false,false]
30+
31+
**Explanation:** "FooBarTest" can be generated like this "Fo" + "o" + "Ba" + "r" + "T" + "est".
32+
33+
**Constraints:**
34+
35+
* `1 <= pattern.length, queries.length <= 100`
36+
* `1 <= queries[i].length <= 100`
37+
* `queries[i]` and `pattern` consist of English letters.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g1001_1100.s1024_video_stitching
2+
3+
// #Medium #Array #Dynamic_Programming #Greedy
4+
// #2023_05_22_Time_141_ms_(100.00%)_Space_34.8_MB_(100.00%)
5+
6+
import java.util.Arrays
7+
8+
class Solution {
9+
fun videoStitching(clips: Array<IntArray>, time: Int): Int {
10+
Arrays.sort(clips) { a: IntArray, b: IntArray ->
11+
if (a[0] == b[0]
12+
) a[1] - b[1] else a[0] - b[0]
13+
}
14+
var count = 0
15+
var covered = 0
16+
var i = 0
17+
var start = 0
18+
while (start < time) {
19+
while (i < clips.size && clips[i][0] <= start) {
20+
covered = covered.coerceAtLeast(clips[i][1])
21+
i++
22+
}
23+
if (start == covered) {
24+
return -1
25+
}
26+
count++
27+
start = covered
28+
}
29+
return count
30+
}
31+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
1024\. Video Stitching
2+
3+
Medium
4+
5+
You are given a series of video clips from a sporting event that lasted `time` seconds. These video clips can be overlapping with each other and have varying lengths.
6+
7+
Each video clip is described by an array `clips` where <code>clips[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> indicates that the ith clip started at <code>start<sub>i</sub></code> and ended at <code>end<sub>i</sub></code>.
8+
9+
We can cut these clips into segments freely.
10+
11+
* For example, a clip `[0, 7]` can be cut into segments `[0, 1] + [1, 3] + [3, 7]`.
12+
13+
Return _the minimum number of clips needed so that we can cut the clips into segments that cover the entire sporting event_ `[0, time]`. If the task is impossible, return `-1`.
14+
15+
**Example 1:**
16+
17+
**Input:** clips = [[0,2],[4,6],[8,10],[1,9],[1,5],[5,9]], time = 10
18+
19+
**Output:** 3
20+
21+
**Explanation:** We take the clips [0,2], [8,10], [1,9]; a total of 3 clips.
22+
23+
Then, we can reconstruct the sporting event as follows:
24+
25+
We cut [1,9] into segments [1,2] + [2,8] + [8,9].
26+
27+
Now we have segments [0,2] + [2,8] + [8,10] which cover the sporting event [0, 10].
28+
29+
**Example 2:**
30+
31+
**Input:** clips = [[0,1],[1,2]], time = 5
32+
33+
**Output:** -1
34+
35+
**Explanation:** We cannot cover [0,5] with only [0,1] and [1,2].
36+
37+
**Example 3:**
38+
39+
**Input:** clips = [[0,1],[6,8],[0,2],[5,6],[0,4],[0,3],[6,7],[1,3],[4,7],[1,4],[2,5],[2,6],[3,4],[4,5],[5,7],[6,9]], time = 9
40+
41+
**Output:** 3
42+
43+
**Explanation:** We can take clips [0,4], [4,7], and [6,9].
44+
45+
**Constraints:**
46+
47+
* `1 <= clips.length <= 100`
48+
* <code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 100</code>
49+
* `1 <= time <= 100`
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g1001_1100.s1021_remove_outermost_parentheses
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun removeOuterParentheses() {
10+
assertThat(Solution().removeOuterParentheses("(()())(())"), equalTo("()()()"))
11+
}
12+
13+
@Test
14+
fun removeOuterParentheses2() {
15+
assertThat(
16+
Solution().removeOuterParentheses("(()())(())(()(()))"),
17+
equalTo("()()()()(())")
18+
)
19+
}
20+
21+
@Test
22+
fun removeOuterParentheses3() {
23+
assertThat(Solution().removeOuterParentheses("()()"), equalTo(""))
24+
}
25+
}

0 commit comments

Comments
 (0)