Skip to content

Commit 69981e7

Browse files
authored
Added tasks 684, 685, 686, 687
1 parent 88f522d commit 69981e7

File tree

13 files changed

+420
-0
lines changed

13 files changed

+420
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.9'
16761676
| 0864 |[Shortest Path to Get All Keys](src/main/kotlin/g0801_0900/s0864_shortest_path_to_get_all_keys/Solution.kt)| Hard | Breadth_First_Search, Bit_Manipulation | 176 | 100.00
16771677
| 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
16781678
| 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
1679+
| 0687 |[Longest Univalue Path](src/main/kotlin/g0601_0700/s0687_longest_univalue_path/Solution.kt)| Medium | Depth_First_Search, Tree, Binary_Tree | 303 | 100.00
1680+
| 0686 |[Repeated String Match](src/main/kotlin/g0601_0700/s0686_repeated_string_match/Solution.kt)| Medium | String, String_Matching | 164 | 100.00
1681+
| 0685 |[Redundant Connection II](src/main/kotlin/g0601_0700/s0685_redundant_connection_ii/Solution.kt)| Hard | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 176 | 100.00
1682+
| 0684 |[Redundant Connection](src/main/kotlin/g0601_0700/s0684_redundant_connection/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 181 | 88.24
16791683
| 0682 |[Baseball Game](src/main/kotlin/g0601_0700/s0682_baseball_game/Solution.kt)| Easy | Array, Stack, Simulation | 159 | 100.00
16801684
| 0680 |[Valid Palindrome II](src/main/kotlin/g0601_0700/s0680_valid_palindrome_ii/Solution.kt)| Easy | String, Greedy, Two_Pointers | 296 | 79.17
16811685
| 0679 |[24 Game](src/main/kotlin/g0601_0700/s0679_24_game/Solution.kt)| Hard | Array, Math, Backtracking | 175 | 100.00
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0601_0700.s0684_redundant_connection
2+
3+
// #Medium #Depth_First_Search #Breadth_First_Search #Graph #Union_Find
4+
// #2023_02_17_Time_181_ms_(88.24%)_Space_35.4_MB_(100.00%)
5+
6+
class Solution {
7+
private lateinit var par: IntArray
8+
fun findRedundantConnection(edges: Array<IntArray>): IntArray {
9+
val ans = IntArray(2)
10+
val n = edges.size
11+
par = IntArray(n + 1)
12+
for (i in 0 until n) {
13+
par[i] = i
14+
}
15+
for (edge in edges) {
16+
val lx = find(edge[0])
17+
val ly = find(edge[1])
18+
if (lx != ly) {
19+
par[lx] = ly
20+
} else {
21+
ans[0] = edge[0]
22+
ans[1] = edge[1]
23+
}
24+
}
25+
return ans
26+
}
27+
28+
private fun find(x: Int): Int {
29+
return if (par[x] == x) {
30+
x
31+
} else find(par[x])
32+
}
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
684\. Redundant Connection
2+
3+
Medium
4+
5+
In this problem, a tree is an **undirected graph** that is connected and has no cycles.
6+
7+
You are given a graph that started as a tree with `n` nodes labeled from `1` to `n`, with one additional edge added. The added edge has two **different** vertices chosen from `1` to `n`, and was not an edge that already existed. The graph is represented as an array `edges` of length `n` where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the graph.
8+
9+
Return _an edge that can be removed so that the resulting graph is a tree of_ `n` _nodes_. If there are multiple answers, return the answer that occurs last in the input.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/05/02/reduntant1-1-graph.jpg)
14+
15+
**Input:** edges = [[1,2],[1,3],[2,3]]
16+
17+
**Output:** [2,3]
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2021/05/02/reduntant1-2-graph.jpg)
22+
23+
**Input:** edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
24+
25+
**Output:** [1,4]
26+
27+
**Constraints:**
28+
29+
* `n == edges.length`
30+
* `3 <= n <= 1000`
31+
* `edges[i].length == 2`
32+
* <code>1 <= a<sub>i</sub> < b<sub>i</sub> <= edges.length</code>
33+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
34+
* There are no repeated edges.
35+
* The given graph is connected.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g0601_0700.s0685_redundant_connection_ii
2+
3+
// #Hard #Depth_First_Search #Breadth_First_Search #Graph #Union_Find
4+
// #2023_02_17_Time_176_ms_(100.00%)_Space_38_MB_(33.33%)
5+
6+
class Solution {
7+
private lateinit var par: IntArray
8+
fun findRedundantDirectedConnection(edges: Array<IntArray>): IntArray {
9+
val n = edges.size
10+
val hasPar = IntArray(n + 1)
11+
for (edge in edges) {
12+
val v = edge[1]
13+
hasPar[v]++
14+
}
15+
par = IntArray(n + 1)
16+
for (i in par.indices) {
17+
par[i] = i
18+
}
19+
for (edge in edges) {
20+
val u = edge[0]
21+
val v = edge[1]
22+
if (hasPar[v] == 1) {
23+
val lu = find(u)
24+
val lv = find(v)
25+
if (lu != lv) {
26+
par[lu] = lv
27+
} else {
28+
return edge
29+
}
30+
}
31+
}
32+
for (edge in edges) {
33+
val u = edge[0]
34+
val v = edge[1]
35+
if (hasPar[v] > 1) {
36+
val lu = find(u)
37+
val lv = find(v)
38+
if (lu != lv) {
39+
par[lu] = lv
40+
} else {
41+
return edge
42+
}
43+
}
44+
}
45+
return IntArray(2)
46+
}
47+
48+
private fun find(x: Int): Int {
49+
return if (par[x] == x) {
50+
x
51+
} else find(par[x])
52+
}
53+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
685\. Redundant Connection II
2+
3+
Hard
4+
5+
In this problem, a rooted tree is a **directed** graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, except for the root node which has no parents.
6+
7+
The given input is a directed graph that started as a rooted tree with `n` nodes (with distinct values from `1` to `n`), with one additional directed edge added. The added edge has two different vertices chosen from `1` to `n`, and was not an edge that already existed.
8+
9+
The resulting graph is given as a 2D-array of `edges`. Each element of `edges` is a pair <code>[u<sub>i</sub>, v<sub>i</sub>]</code> that represents a **directed** edge connecting nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>, where <code>u<sub>i</sub></code> is a parent of child <code>v<sub>i</sub></code>.
10+
11+
Return _an edge that can be removed so that the resulting graph is a rooted tree of_ `n` _nodes_. If there are multiple answers, return the answer that occurs last in the given 2D-array.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2020/12/20/graph1.jpg)
16+
17+
**Input:** edges = [[1,2],[1,3],[2,3]]
18+
19+
**Output:** [2,3]
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2020/12/20/graph2.jpg)
24+
25+
**Input:** edges = [[1,2],[2,3],[3,4],[4,1],[1,5]]
26+
27+
**Output:** [4,1]
28+
29+
**Constraints:**
30+
31+
* `n == edges.length`
32+
* `3 <= n <= 1000`
33+
* `edges[i].length == 2`
34+
* <code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code>
35+
* <code>u<sub>i</sub> != v<sub>i</sub></code>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g0601_0700.s0686_repeated_string_match
2+
3+
// #Medium #String #String_Matching #2023_02_17_Time_164_ms_(100.00%)_Space_35.5_MB_(50.00%)
4+
5+
class Solution {
6+
fun repeatedStringMatch(a: String, b: String): Int {
7+
val existsChar = CharArray(127)
8+
for (chA in a.toCharArray()) {
9+
existsChar[chA.code] = 1.toChar()
10+
}
11+
for (chB in b.toCharArray()) {
12+
if (existsChar[chB.code].code < 1) {
13+
return -1
14+
}
15+
}
16+
val lenB = b.length - 1
17+
val sb = StringBuilder(a)
18+
var lenSbA = sb.length - 1
19+
var repeatCount = 1
20+
while (lenSbA < lenB) {
21+
sb.append(a)
22+
repeatCount++
23+
lenSbA = sb.length - 1
24+
}
25+
if (!isFound(sb, b)) {
26+
sb.append(a)
27+
repeatCount++
28+
return if (!isFound(sb, b)) -1 else repeatCount
29+
}
30+
return repeatCount
31+
}
32+
33+
private fun isFound(a: StringBuilder, b: String): Boolean {
34+
for (i in a.indices) {
35+
var k = i
36+
var m = 0
37+
while (k < a.length && m < b.length) {
38+
if (a[k] != b[m]) {
39+
break
40+
} else {
41+
k++
42+
m++
43+
}
44+
}
45+
if (m == b.length) {
46+
return true
47+
}
48+
}
49+
return false
50+
}
51+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
686\. Repeated String Match
2+
3+
Medium
4+
5+
Given two strings `a` and `b`, return _the minimum number of times you should repeat string_ `a` _so that string_ `b` _is a substring of it_. If it is impossible for `b` to be a substring of `a` after repeating it, return `-1`.
6+
7+
**Notice:** string `"abc"` repeated 0 times is `""`, repeated 1 time is `"abc"` and repeated 2 times is `"abcabc"`.
8+
9+
**Example 1:**
10+
11+
**Input:** a = "abcd", b = "cdabcdab"
12+
13+
**Output:** 3
14+
15+
**Explanation:** We return 3 because by repeating a three times "ab**cdabcdab**cd", b is a substring of it.
16+
17+
**Example 2:**
18+
19+
**Input:** a = "a", b = "aa"
20+
21+
**Output:** 2
22+
23+
**Constraints:**
24+
25+
* <code>1 <= a.length, b.length <= 10<sup>4</sup></code>
26+
* `a` and `b` consist of lowercase English letters.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g0601_0700.s0687_longest_univalue_path
2+
3+
// #Medium #Depth_First_Search #Tree #Binary_Tree
4+
// #2023_02_17_Time_303_ms_(100.00%)_Space_39.2_MB_(100.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+
fun longestUnivaluePath(root: TreeNode?): Int {
20+
if (root == null) {
21+
return 0
22+
}
23+
val res = IntArray(1)
24+
preorderLongestSinglePathLen(root, res)
25+
return res[0]
26+
}
27+
28+
private fun preorderLongestSinglePathLen(root: TreeNode?, res: IntArray): Int {
29+
if (root == null) {
30+
return -1
31+
}
32+
var left = preorderLongestSinglePathLen(root.left, res)
33+
var right = preorderLongestSinglePathLen(root.right, res)
34+
left = if (root.left == null || root.`val` == root.left!!.`val`) {
35+
left + 1
36+
} else {
37+
0
38+
}
39+
right = if (root.right == null || root.`val` == root.right!!.`val`) {
40+
right + 1
41+
} else {
42+
0
43+
}
44+
val longestPathLenPassingThroughRoot = left + right
45+
res[0] = res[0].coerceAtLeast(longestPathLenPassingThroughRoot)
46+
return left.coerceAtLeast(right)
47+
}
48+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
687\. Longest Univalue Path
2+
3+
Medium
4+
5+
Given the `root` of a binary tree, return _the length of the longest path, where each node in the path has the same value_. This path may or may not pass through the root.
6+
7+
**The length of the path** between two nodes is represented by the number of edges between them.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/13/ex1.jpg)
12+
13+
**Input:** root = [5,4,5,1,1,null,5]
14+
15+
**Output:** 2
16+
17+
**Explanation:** The shown image shows that the longest path of the same value (i.e. 5).
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2020/10/13/ex2.jpg)
22+
23+
**Input:** root = [1,4,5,4,4,null,5]
24+
25+
**Output:** 2
26+
27+
**Explanation:** The shown image shows that the longest path of the same value (i.e. 4).
28+
29+
**Constraints:**
30+
31+
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
32+
* `-1000 <= Node.val <= 1000`
33+
* The depth of the tree will not exceed `1000`.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0601_0700.s0684_redundant_connection
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 findRedundantConnection() {
10+
assertThat(
11+
Solution().findRedundantConnection(arrayOf(intArrayOf(1, 2), intArrayOf(1, 3), intArrayOf(2, 3))),
12+
equalTo(intArrayOf(2, 3))
13+
)
14+
}
15+
16+
@Test
17+
fun findRedundantConnection2() {
18+
assertThat(
19+
Solution()
20+
.findRedundantConnection(
21+
arrayOf(
22+
intArrayOf(1, 2),
23+
intArrayOf(2, 3),
24+
intArrayOf(3, 4),
25+
intArrayOf(1, 4),
26+
intArrayOf(1, 5)
27+
)
28+
),
29+
equalTo(intArrayOf(1, 4))
30+
)
31+
}
32+
}

0 commit comments

Comments
 (0)