Skip to content

Commit e5c24af

Browse files
authored
Added tasks 814, 815, 816, 817
1 parent 7327bbd commit e5c24af

File tree

13 files changed

+523
-0
lines changed

13 files changed

+523
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
379379
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
380380
|-|-|-|-|-|-
381381
| 0210 |[Course Schedule II](src.save/main/kotlin/g0201_0300/s0210_course_schedule_ii/Solution.kt)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 266 | 96.32
382+
| 0815 |[Bus Routes](src/main/kotlin/g0801_0900/s0815_bus_routes/Solution.kt)| Hard | Array, Hash_Table, Breadth_First_Search | 429 | 100.00
382383

383384
#### Day 12 Dynamic Programming
384385

@@ -1711,6 +1712,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.10'
17111712
| 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
17121713
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Level_2_Day_10_Graph/BFS/DFS | 308 | 57.93
17131714
| 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
1715+
| 0817 |[Linked List Components](src/main/kotlin/g0801_0900/s0817_linked_list_components/Solution.kt)| Medium | Hash_Table, Linked_List | 239 | 100.00
1716+
| 0816 |[Ambiguous Coordinates](src/main/kotlin/g0801_0900/s0816_ambiguous_coordinates/Solution.kt)| Medium | String, Backtracking | 231 | 100.00
1717+
| 0815 |[Bus Routes](src/main/kotlin/g0801_0900/s0815_bus_routes/Solution.kt)| Hard | Array, Hash_Table, Breadth_First_Search, Level_2_Day_11_Graph/BFS/DFS | 429 | 100.00
1718+
| 0814 |[Binary Tree Pruning](src/main/kotlin/g0801_0900/s0814_binary_tree_pruning/Solution.kt)| Medium | Depth_First_Search, Tree, Binary_Tree | 127 | 100.00
17141719
| 0813 |[Largest Sum of Averages](src/main/kotlin/g0801_0900/s0813_largest_sum_of_averages/Solution.kt)| Medium | Array, Dynamic_Programming | 160 | 100.00
17151720
| 0812 |[Largest Triangle Area](src/main/kotlin/g0801_0900/s0812_largest_triangle_area/Solution.kt)| Easy | Array, Math, Geometry | 156 | 71.43
17161721
| 0811 |[Subdomain Visit Count](src/main/kotlin/g0801_0900/s0811_subdomain_visit_count/Solution.kt)| Medium | Array, String, Hash_Table, Counting | 220 | 100.00
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0801_0900.s0814_binary_tree_pruning
2+
3+
// #Medium #Depth_First_Search #Tree #Binary_Tree
4+
// #2023_03_22_Time_127_ms_(100.00%)_Space_34.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 pruneTree(root: TreeNode?): TreeNode? {
20+
if (root == null) {
21+
return root
22+
}
23+
root.left = pruneTree(root.left)
24+
root.right = pruneTree(root.right)
25+
return if (root.left == null && root.right == null && root.`val` == 0) {
26+
null
27+
} else root
28+
}
29+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
814\. Binary Tree Pruning
2+
3+
Medium
4+
5+
Given the `root` of a binary tree, return _the same tree where every subtree (of the given tree) not containing a_ `1` _has been removed_.
6+
7+
A subtree of a node `node` is `node` plus every node that is a descendant of `node`.
8+
9+
**Example 1:**
10+
11+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_2.png)
12+
13+
**Input:** root = [1,null,0,0,1]
14+
15+
**Output:** [1,null,0,null,1]
16+
17+
**Explanation:**
18+
19+
Only the red nodes satisfy the property "every subtree not containing a 1".
20+
21+
The diagram on the right represents the answer.
22+
23+
**Example 2:**
24+
25+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_1.png)
26+
27+
**Input:** root = [1,0,1,0,0,0,1]
28+
29+
**Output:** [1,null,1,null,1]
30+
31+
**Example 3:**
32+
33+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/05/1028.png)
34+
35+
**Input:** root = [1,1,0,1,1,0,1,0]
36+
37+
**Output:** [1,1,0,1,1,null,1]
38+
39+
**Constraints:**
40+
41+
* The number of nodes in the tree is in the range `[1, 200]`.
42+
* `Node.val` is either `0` or `1`.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package g0801_0900.s0815_bus_routes
2+
3+
// #Hard #Array #Hash_Table #Breadth_First_Search #Level_2_Day_11_Graph/BFS/DFS
4+
// #2023_03_22_Time_429_ms_(100.00%)_Space_55.8_MB_(100.00%)
5+
6+
import java.util.Arrays
7+
import java.util.LinkedList
8+
import java.util.Queue
9+
import kotlin.collections.ArrayList
10+
import kotlin.collections.HashSet
11+
import kotlin.collections.MutableSet
12+
13+
class Solution {
14+
fun numBusesToDestination(routes: Array<IntArray>, source: Int, target: Int): Int {
15+
if (source == target) {
16+
return 0
17+
}
18+
val targetRoutes: MutableSet<Int> = HashSet()
19+
val queue: Queue<Int> = LinkedList()
20+
val taken = BooleanArray(routes.size)
21+
val graph = buildGraph(routes, source, target, queue, targetRoutes, taken)
22+
if (targetRoutes.isEmpty()) {
23+
return -1
24+
}
25+
var bus = 1
26+
while (!queue.isEmpty()) {
27+
val size = queue.size
28+
for (i in 0 until size) {
29+
val route = queue.poll()
30+
if (targetRoutes.contains(route)) {
31+
return bus
32+
}
33+
for (nextRoute in graph[route]!!) {
34+
if (!taken[nextRoute]) {
35+
queue.offer(nextRoute)
36+
taken[nextRoute] = true
37+
}
38+
}
39+
}
40+
bus++
41+
}
42+
return -1
43+
}
44+
45+
private fun buildGraph(
46+
routes: Array<IntArray>,
47+
source: Int,
48+
target: Int,
49+
queue: Queue<Int>,
50+
targetRoutes: MutableSet<Int>,
51+
taken: BooleanArray
52+
): Array<ArrayList<Int>?> {
53+
val len = routes.size
54+
val graph: Array<ArrayList<Int>?> = arrayOfNulls(len)
55+
for (i in 0 until len) {
56+
Arrays.sort(routes[i])
57+
graph[i] = ArrayList()
58+
var id = Arrays.binarySearch(routes[i], source)
59+
if (id >= 0) {
60+
queue.offer(i)
61+
taken[i] = true
62+
}
63+
id = Arrays.binarySearch(routes[i], target)
64+
if (id >= 0) {
65+
targetRoutes.add(i)
66+
}
67+
}
68+
for (i in 0 until len) {
69+
for (j in i + 1 until len) {
70+
if (commonStop(routes[i], routes[j])) {
71+
graph[i]?.add(j)
72+
graph[j]?.add(i)
73+
}
74+
}
75+
}
76+
return graph
77+
}
78+
79+
private fun commonStop(routeA: IntArray, routeB: IntArray): Boolean {
80+
var idA = 0
81+
var idB = 0
82+
while (idA < routeA.size && idB < routeB.size) {
83+
if (routeA[idA] == routeB[idB]) {
84+
return true
85+
} else if (routeA[idA] < routeB[idB]) {
86+
idA++
87+
} else {
88+
idB++
89+
}
90+
}
91+
return false
92+
}
93+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
815\. Bus Routes
2+
3+
Hard
4+
5+
You are given an array `routes` representing bus routes where `routes[i]` is a bus route that the <code>i<sup>th</sup></code> bus repeats forever.
6+
7+
* For example, if `routes[0] = [1, 5, 7]`, this means that the <code>0<sup>th</sup></code> bus travels in the sequence `1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...` forever.
8+
9+
You will start at the bus stop `source` (You are not on any bus initially), and you want to go to the bus stop `target`. You can travel between bus stops by buses only.
10+
11+
Return _the least number of buses you must take to travel from_ `source` _to_ `target`. Return `-1` if it is not possible.
12+
13+
**Example 1:**
14+
15+
**Input:** routes = [[1,2,7],[3,6,7]], source = 1, target = 6
16+
17+
**Output:** 2
18+
19+
**Explanation:** The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.
20+
21+
**Example 2:**
22+
23+
**Input:** routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
24+
25+
**Output:** -1
26+
27+
**Constraints:**
28+
29+
* `1 <= routes.length <= 500`.
30+
* <code>1 <= routes[i].length <= 10<sup>5</sup></code>
31+
* All the values of `routes[i]` are **unique**.
32+
* <code>sum(routes[i].length) <= 10<sup>5</sup></code>
33+
* <code>0 <= routes[i][j] < 10<sup>6</sup></code>
34+
* <code>0 <= source, target < 10<sup>6</sup></code>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package g0801_0900.s0816_ambiguous_coordinates
2+
3+
// #Medium #String #Backtracking #2023_03_22_Time_231_ms_(100.00%)_Space_36.2_MB_(100.00%)
4+
5+
@Suppress("kotlin:S107")
6+
class Solution {
7+
fun ambiguousCoordinates(s: String): List<String> {
8+
val sc = s.toCharArray()
9+
val result: MutableList<String> = ArrayList()
10+
val sb = StringBuilder()
11+
for (commaPos in 2 until sc.size - 1) {
12+
if (isValidNum(sc, 1, commaPos - 1)) {
13+
if (isValidNum(sc, commaPos, sc.size - 2)) {
14+
buildNumbs(result, sb, sc, commaPos - 1, 0, commaPos, sc.size - 2, 0)
15+
}
16+
for (dp2Idx in commaPos + 1 until sc.size - 1) {
17+
if (isValidDPNum(sc, commaPos, sc.size - 2, dp2Idx)) {
18+
buildNumbs(result, sb, sc, commaPos - 1, 0, commaPos, sc.size - 2, dp2Idx)
19+
}
20+
}
21+
}
22+
for (dp1Idx in 2 until commaPos) {
23+
if (isValidDPNum(sc, 1, commaPos - 1, dp1Idx)) {
24+
if (isValidNum(sc, commaPos, sc.size - 2)) {
25+
buildNumbs(result, sb, sc, commaPos - 1, dp1Idx, commaPos, sc.size - 2, 0)
26+
}
27+
for (dp2Idx in commaPos + 1 until sc.size - 1) {
28+
if (isValidDPNum(sc, commaPos, sc.size - 2, dp2Idx)) {
29+
buildNumbs(
30+
result,
31+
sb,
32+
sc,
33+
commaPos - 1,
34+
dp1Idx,
35+
commaPos,
36+
sc.size - 2,
37+
dp2Idx
38+
)
39+
}
40+
}
41+
}
42+
}
43+
}
44+
return result
45+
}
46+
47+
private fun isValidNum(sc: CharArray, startIdx: Int, lastIdx: Int): Boolean {
48+
return sc[startIdx] != '0' || lastIdx - startIdx == 0
49+
}
50+
51+
private fun isValidDPNum(sc: CharArray, startIdx: Int, lastIdx: Int, dpIdx: Int): Boolean {
52+
return (sc[startIdx] != '0' || dpIdx - startIdx == 1) && sc[lastIdx] != '0'
53+
}
54+
55+
private fun buildNumbs(
56+
result: MutableList<String>,
57+
sb: StringBuilder,
58+
sc: CharArray,
59+
last1Idx: Int,
60+
dp1Idx: Int,
61+
start2Idx: Int,
62+
last2Idx: Int,
63+
dp2Idx: Int
64+
) {
65+
sb.setLength(0)
66+
sb.append('(')
67+
if (dp1Idx == 0) {
68+
sb.append(sc, 1, last1Idx - 1 + 1)
69+
} else {
70+
sb.append(sc, 1, dp1Idx - 1).append('.').append(sc, dp1Idx, last1Idx - dp1Idx + 1)
71+
}
72+
sb.append(',').append(' ')
73+
if (dp2Idx == 0) {
74+
sb.append(sc, start2Idx, last2Idx - start2Idx + 1)
75+
} else {
76+
sb.append(sc, start2Idx, dp2Idx - start2Idx)
77+
.append('.')
78+
.append(sc, dp2Idx, last2Idx - dp2Idx + 1)
79+
}
80+
sb.append(')')
81+
result.add(sb.toString())
82+
}
83+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
816\. Ambiguous Coordinates
2+
3+
Medium
4+
5+
We had some 2-dimensional coordinates, like `"(1, 3)"` or `"(2, 0.5)"`. Then, we removed all commas, decimal points, and spaces and ended up with the string s.
6+
7+
* For example, `"(1, 3)"` becomes `s = "(13)"` and `"(2, 0.5)"` becomes `s = "(205)"`.
8+
9+
Return _a list of strings representing all possibilities for what our original coordinates could have been_.
10+
11+
Our original representation never had extraneous zeroes, so we never started with numbers like `"00"`, `"0.0"`, `"0.00"`, `"1.0"`, `"001"`, `"00.01"`, or any other number that can be represented with fewer digits. Also, a decimal point within a number never occurs without at least one digit occurring before it, so we never started with numbers like `".1"`.
12+
13+
The final answer list can be returned in any order. All coordinates in the final answer have exactly one space between them (occurring after the comma.)
14+
15+
**Example 1:**
16+
17+
**Input:** s = "(123)"
18+
19+
**Output:** ["(1, 2.3)","(1, 23)","(1.2, 3)","(12, 3)"]
20+
21+
**Example 2:**
22+
23+
**Input:** s = "(0123)"
24+
25+
**Output:** ["(0, 1.23)","(0, 12.3)","(0, 123)","(0.1, 2.3)","(0.1, 23)","(0.12, 3)"]
26+
27+
**Explanation:** 0.0, 00, 0001 or 00.01 are not allowed.
28+
29+
**Example 3:**
30+
31+
**Input:** s = "(00011)"
32+
33+
**Output:** ["(0, 0.011)","(0.001, 1)"]
34+
35+
**Constraints:**
36+
37+
* `4 <= s.length <= 12`
38+
* `s[0] == '('` and `s[s.length - 1] == ')'`.
39+
* The rest of `s` are digits.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0801_0900.s0817_linked_list_components
2+
3+
// #Medium #Hash_Table #Linked_List #2023_03_22_Time_239_ms_(100.00%)_Space_39.8_MB_(66.67%)
4+
5+
import com_github_leetcode.ListNode
6+
7+
/**
8+
* Example:
9+
* var li = ListNode(5)
10+
* var v = li.`val`
11+
* Definition for singly-linked list.
12+
* class ListNode(var `val`: Int) {
13+
* var next: ListNode? = null
14+
* }
15+
*/
16+
17+
@Suppress("NAME_SHADOWING")
18+
class Solution {
19+
fun numComponents(head: ListNode?, nums: IntArray): Int {
20+
var head = head
21+
val set: HashSet<Int> = HashSet()
22+
for (i in nums) {
23+
set.add(i)
24+
}
25+
var result = 0
26+
while (head != null) {
27+
if (set.contains(head.`val`)) {
28+
while (head != null && set.contains(head.`val`)) {
29+
head = head.next
30+
}
31+
result++
32+
} else {
33+
head = head.next
34+
}
35+
}
36+
return result
37+
}
38+
}

0 commit comments

Comments
 (0)