Skip to content

Commit c56d48c

Browse files
authored
Improved task 2920
1 parent 34e0877 commit c56d48c

File tree

16 files changed

+52
-162
lines changed

16 files changed

+52
-162
lines changed

src/main/kotlin/g2901_3000/s2920_maximum_points_after_collecting_coins_from_all_nodes/Solution.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,19 @@ package g2901_3000.s2920_maximum_points_after_collecting_coins_from_all_nodes
66
import kotlin.math.max
77

88
class Solution {
9-
private lateinit var adjList: Array<MutableList<Int>?>
9+
private lateinit var adjList: Array<MutableList<Int>>
1010
private lateinit var coins: IntArray
1111
private var k = 0
1212
private lateinit var dp: Array<IntArray>
1313

1414
private fun init(edges: Array<IntArray>, coins: IntArray, k: Int) {
1515
val n = coins.size
16-
adjList = arrayOfNulls(n)
17-
for (v in 0 until n) {
18-
adjList[v] = ArrayList()
19-
}
16+
adjList = Array(n) { ArrayList() }
2017
for (edge in edges) {
2118
val u = edge[0]
2219
val v = edge[1]
23-
adjList[u]?.add(v)
24-
adjList[v]?.add(u)
20+
adjList[u].add(v)
21+
adjList[v].add(u)
2522
}
2623
this.coins = coins
2724
this.k = k
@@ -41,7 +38,7 @@ class Solution {
4138
val coinsV = coins[v] / (1 shl numOfWay2Parents)
4239
var s0 = coinsV - k
4340
var s1 = coinsV / 2
44-
for (child in adjList[v]!!) {
41+
for (child in adjList[v]) {
4542
if (child != p) {
4643
s0 += rec(child, v, numOfWay2Parents)
4744
s1 += rec(child, v, numOfWay2Parents + 1)

src/test/kotlin/com_github_leetcode/CommonUtils.kt

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,6 @@
11
package com_github_leetcode
22

3-
import java.util.Collections
4-
53
object CommonUtils {
6-
fun printArray(nums: IntArray) {
7-
for (i in nums) {
8-
print("$i, ")
9-
}
10-
println()
11-
}
12-
13-
fun printArray(nums: DoubleArray) {
14-
for (i in nums) {
15-
print("$i, ")
16-
}
17-
println()
18-
}
19-
204
fun compareArray(arr1: IntArray, arr2: IntArray): Boolean {
215
for (i in arr1) {
226
var include = false
@@ -65,40 +49,6 @@ object CommonUtils {
6549
return true
6650
}
6751

68-
fun convertLeetCodeRegular2DCharArrayInputIntoJavaArray(input: String): Array<CharArray>? {
69-
/*
70-
* LeetCode 2-d char array usually comes in like this:
71-
* ["#"," ","#"],[" "," ","#"],["#","c"," "] which is wrapped in double quotes instead
72-
* of single quotes which makes it not usable in Java code.
73-
* This method helps with the conversion.
74-
*/
75-
val arrays = input.split("],\\[".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
76-
val m = arrays.size
77-
val n = arrays[1].split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray().size
78-
val ans = Array(m) { CharArray(n) }
79-
for (i in 0 until m) {
80-
if (i == 0) {
81-
val str = arrays[i].substring(1)
82-
val strs = str.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
83-
for (j in strs.indices) {
84-
ans[i][j] = strs[j][1]
85-
}
86-
} else if (i == m - 1) {
87-
val str = arrays[i].substring(0, arrays[i].length - 1)
88-
val strs = str.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
89-
for (j in strs.indices) {
90-
ans[i][j] = strs[j][1]
91-
}
92-
} else {
93-
val strs = arrays[i].split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
94-
for (j in strs.indices) {
95-
ans[i][j] = strs[j][1]
96-
}
97-
}
98-
}
99-
return ans
100-
}
101-
10252
fun convertLeetCodeRegularRectangleArrayInputIntoJavaArray(input: String): Array<IntArray> {
10353
/*
10454
* LeetCode 2-d array input usually comes like this: it's a REGULAR rectangle
@@ -185,61 +135,4 @@ object CommonUtils {
185135
}
186136
return output.filterNotNull().toTypedArray()
187137
}
188-
189-
fun convertLeetCode2DStringArrayInputIntoJavaArray(input: String): MutableList<MutableList<String?>> {
190-
/*
191-
* How to copy LeetCode 2-d String array into this method:
192-
* 1. remove the beginning and ending quotes;
193-
* 2. put double quotes into this method parameter;
194-
* 3. copy the input into the double quotes.
195-
*
196-
* LeetCode 2-d array input usually comes like this: each row could have different length
197-
* [["A","B"],["C"],["B","C"],["D"]]
198-
* The expected input for this method is: "[\"A\",\"B\"],[\"C\"],[\"B\",\"C\"],[\"D\"]"
199-
* just copy the LeetCode input: ["A","B"],["C"],["B","C"],["D"] into double quotes in Java,
200-
* it'll auto escape the double quotes.
201-
* i.e. strip off the beginning and ending square brackets, that's it.
202-
* The output of this method will be a standard Java 2-d array.
203-
* */
204-
val arrays = input.split("],\\[".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
205-
val result: MutableList<MutableList<String?>> = ArrayList()
206-
for (i in arrays.indices) {
207-
val level: MutableList<String?> = ArrayList()
208-
val strings: Array<String?> = if (i == 0) {
209-
arrays[i].substring(1).split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
210-
} else if (i == arrays.size - 1) {
211-
arrays[i].substring(0, arrays[i].length - 1).split(",".toRegex()).dropLastWhile { it.isEmpty() }
212-
.toTypedArray()
213-
} else {
214-
arrays[i].split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
215-
}
216-
Collections.addAll(level, *strings)
217-
result.add(level)
218-
}
219-
return result
220-
}
221-
222-
fun convertLeetCode1DStringArrayInputIntoJavaArray(input: String): List<String>? {
223-
/*
224-
* LeetCode 2-d array input usually comes like this: each row could have different length
225-
* ["A","B","C"]
226-
* The expected input for this method is: "[\"A\",\"B\",\"C\"]"
227-
* just copy the LeetCode input: ["A","B","C"] into double quotes in Java,
228-
* it'll auto escape the double quotes.
229-
* The output of this method will be a standard Java 1-d array.
230-
* */
231-
val arrays = input.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
232-
val result: MutableList<String> = ArrayList()
233-
for (i in arrays.indices) {
234-
val word: String = if (i == 0) {
235-
arrays[i].substring(1)
236-
} else if (i == arrays.size - 1) {
237-
arrays[i].substring(0, arrays[i].length - 1)
238-
} else {
239-
arrays[i]
240-
}
241-
result.add(word)
242-
}
243-
return result
244-
}
245138
}

src/test/kotlin/g1901_2000/s1968_array_with_elements_not_equal_to_average_of_neighbors/SolutionTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g1901_2000.s1968_array_with_elements_not_equal_to_average_of_neighbors
22

3-
import com_github_leetcode.CommonUtils.compareArray
3+
import com_github_leetcode.CommonUtils
44
import org.hamcrest.CoreMatchers.equalTo
55
import org.hamcrest.MatcherAssert.assertThat
66
import org.junit.jupiter.api.Test
@@ -9,7 +9,7 @@ internal class SolutionTest {
99
@Test
1010
fun rearrangeArray() {
1111
assertThat(
12-
compareArray(
12+
CommonUtils.compareArray(
1313
Solution().rearrangeArray(intArrayOf(1, 2, 3, 4, 5)), intArrayOf(1, 2, 4, 5, 3)
1414
),
1515
equalTo(true)
@@ -19,7 +19,7 @@ internal class SolutionTest {
1919
@Test
2020
fun rearrangeArray2() {
2121
assertThat(
22-
compareArray(
22+
CommonUtils.compareArray(
2323
Solution().rearrangeArray(intArrayOf(6, 2, 0, 9, 7)), intArrayOf(6, 2, 0, 9, 7)
2424
),
2525
equalTo(true)

src/test/kotlin/g2401_2500/s2477_minimum_fuel_cost_to_report_to_the_capital/SolutionTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g2401_2500.s2477_minimum_fuel_cost_to_report_to_the_capital
22

3-
import com_github_leetcode.CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray
3+
import com_github_leetcode.CommonUtils
44
import org.hamcrest.CoreMatchers.equalTo
55
import org.hamcrest.MatcherAssert.assertThat
66
import org.junit.jupiter.api.Test
@@ -11,7 +11,7 @@ internal class SolutionTest {
1111
assertThat(
1212
Solution()
1313
.minimumFuelCost(
14-
convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
14+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
1515
"[0,1],[0,2],[0,3]"
1616
),
1717
5
@@ -25,7 +25,7 @@ internal class SolutionTest {
2525
assertThat(
2626
Solution()
2727
.minimumFuelCost(
28-
convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
28+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
2929
"[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]"
3030
),
3131
2

src/test/kotlin/g2401_2500/s2482_difference_between_ones_and_zeros_in_row_and_column/SolutionTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g2401_2500.s2482_difference_between_ones_and_zeros_in_row_and_column
22

3-
import com_github_leetcode.CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray
3+
import com_github_leetcode.CommonUtils
44
import org.hamcrest.CoreMatchers.equalTo
55
import org.hamcrest.MatcherAssert.assertThat
66
import org.junit.jupiter.api.Test
@@ -11,12 +11,12 @@ internal class SolutionTest {
1111
assertThat(
1212
Solution()
1313
.onesMinusZeros(
14-
convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
14+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
1515
"[0,1,1],[1,0,1],[0,0,1]"
1616
)
1717
),
1818
equalTo(
19-
convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
19+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
2020
"[0,0,4],[0,0,4],[-2,-2,2]"
2121
)
2222
)
@@ -28,12 +28,12 @@ internal class SolutionTest {
2828
assertThat(
2929
Solution()
3030
.onesMinusZeros(
31-
convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
31+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
3232
"[1,1,1],[1,1,1]"
3333
)
3434
),
3535
equalTo(
36-
convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
36+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
3737
"[5,5,5],[5,5,5]"
3838
)
3939
)

src/test/kotlin/g2401_2500/s2492_minimum_score_of_a_path_between_two_cities/SolutionTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g2401_2500.s2492_minimum_score_of_a_path_between_two_cities
22

3-
import com_github_leetcode.CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray
3+
import com_github_leetcode.CommonUtils
44
import org.hamcrest.CoreMatchers.equalTo
55
import org.hamcrest.MatcherAssert.assertThat
66
import org.junit.jupiter.api.Test
@@ -12,7 +12,7 @@ internal class SolutionTest {
1212
Solution()
1313
.minScore(
1414
4,
15-
convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
15+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
1616
"[1,2,9],[2,3,6],[2,4,5],[1,4,7]"
1717
)
1818
),
@@ -26,7 +26,7 @@ internal class SolutionTest {
2626
Solution()
2727
.minScore(
2828
4,
29-
convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
29+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
3030
"[1,2,2],[1,3,4],[3,4,7]"
3131
)
3232
),

src/test/kotlin/g2401_2500/s2493_divide_nodes_into_the_maximum_number_of_groups/SolutionTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g2401_2500.s2493_divide_nodes_into_the_maximum_number_of_groups
22

3-
import com_github_leetcode.CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray
3+
import com_github_leetcode.CommonUtils
44
import org.hamcrest.CoreMatchers.equalTo
55
import org.hamcrest.MatcherAssert.assertThat
66
import org.junit.jupiter.api.Test
@@ -12,7 +12,7 @@ internal class SolutionTest {
1212
Solution()
1313
.magnificentSets(
1414
6,
15-
convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
15+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
1616
"[1,2],[1,4],[1,5],[2,6],[2,3],[4,6]"
1717
)
1818
),
@@ -26,7 +26,7 @@ internal class SolutionTest {
2626
Solution()
2727
.magnificentSets(
2828
3,
29-
convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
29+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
3030
"[1,2],[2,3],[3,1]"
3131
)
3232
),

src/test/kotlin/g2501_2600/s2538_difference_between_maximum_and_minimum_price_sum/SolutionTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g2501_2600.s2538_difference_between_maximum_and_minimum_price_sum
22

3-
import com_github_leetcode.CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray
3+
import com_github_leetcode.CommonUtils
44
import org.hamcrest.CoreMatchers.equalTo
55
import org.hamcrest.MatcherAssert.assertThat
66
import org.junit.jupiter.api.Test
@@ -12,7 +12,7 @@ internal class SolutionTest {
1212
Solution()
1313
.maxOutput(
1414
6,
15-
convertLeetCodeRegularRectangleArrayInputIntoJavaArray(
15+
CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray(
1616
"[0,1],[1,2],[1,3],[3,4],[3,5]"
1717
),
1818
intArrayOf(9, 8, 7, 6, 10, 5)
@@ -27,7 +27,7 @@ internal class SolutionTest {
2727
Solution()
2828
.maxOutput(
2929
3,
30-
convertLeetCodeRegularRectangleArrayInputIntoJavaArray(
30+
CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray(
3131
"[0,1],[1,2]"
3232
),
3333
intArrayOf(1, 1, 1)

src/test/kotlin/g2801_2900/s2846_minimum_edge_weight_equilibrium_queries_in_a_tree/SolutionTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g2801_2900.s2846_minimum_edge_weight_equilibrium_queries_in_a_tree
22

3-
import com_github_leetcode.CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray
3+
import com_github_leetcode.CommonUtils
44
import org.hamcrest.CoreMatchers.equalTo
55
import org.hamcrest.MatcherAssert.assertThat
66
import org.junit.jupiter.api.Test
@@ -12,10 +12,10 @@ internal class SolutionTest {
1212
Solution()
1313
.minOperationsQueries(
1414
7,
15-
convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
15+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
1616
"[0,1,1],[1,2,1],[2,3,1],[3,4,2],[4,5,2],[5,6,2]"
1717
),
18-
convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
18+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
1919
"[0,3],[3,6],[2,6],[0,6]"
2020
)
2121
),
@@ -29,10 +29,10 @@ internal class SolutionTest {
2929
Solution()
3030
.minOperationsQueries(
3131
8,
32-
convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
32+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
3333
"[1,2,6],[1,3,4],[2,4,6],[2,5,3],[3,6,6],[3,0,8],[7,0,2]"
3434
),
35-
convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
35+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
3636
"[4,6],[0,4],[6,5],[7,4]"
3737
)
3838
),

src/test/kotlin/g2801_2900/s2850_minimum_moves_to_spread_stones_over_grid/SolutionTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g2801_2900.s2850_minimum_moves_to_spread_stones_over_grid
22

3-
import com_github_leetcode.CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray
3+
import com_github_leetcode.CommonUtils
44
import org.hamcrest.CoreMatchers.equalTo
55
import org.hamcrest.MatcherAssert.assertThat
66
import org.junit.jupiter.api.Test
@@ -11,7 +11,7 @@ internal class SolutionTest {
1111
assertThat(
1212
Solution()
1313
.minimumMoves(
14-
convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
14+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
1515
"[1,1,0],[1,1,1],[1,2,1]"
1616
)
1717
),
@@ -24,7 +24,7 @@ internal class SolutionTest {
2424
assertThat(
2525
Solution()
2626
.minimumMoves(
27-
convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
27+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
2828
"[1,3,0],[1,0,0],[1,0,3]"
2929
)
3030
),

0 commit comments

Comments
 (0)