Skip to content

Commit 65c17ca

Browse files
committed
Improved tasks
1 parent a248164 commit 65c17ca

File tree

3 files changed

+22
-26
lines changed
  • src/main/kotlin
    • g1501_1600/s1594_maximum_non_negative_product_in_a_matrix
    • g2201_2300/s2286_booking_concert_tickets_in_groups
    • g3001_3100/s3068_find_the_maximum_sum_of_node_values

3 files changed

+22
-26
lines changed

src/main/kotlin/g1501_1600/s1594_maximum_non_negative_product_in_a_matrix/Solution.kt

+18-23
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,38 @@ package g1501_1600.s1594_maximum_non_negative_product_in_a_matrix
66
class Solution {
77
private class Tuple(var max: Long, var min: Long)
88

9-
fun maxProductPath(grid: Array<IntArray?>?): Int {
9+
fun maxProductPath(grid: Array<IntArray>): Int {
1010
// DP
11-
if (grid == null || grid.size == 0 || grid[0] == null || grid[0]!!.size == 0) {
11+
if (grid.isEmpty() || grid[0].isEmpty()) {
1212
return 0
1313
}
1414
val rows = grid.size
15-
val cols = grid[0]!!.size
16-
val dp = Array(rows) { arrayOfNulls<Tuple>(cols) }
17-
for (i in 0 until rows) {
18-
for (j in 0 until cols) {
19-
dp[i][j] = Tuple(1, 1)
20-
}
21-
}
15+
val cols = grid[0].size
16+
val dp = Array(rows) { Array(cols) {Tuple(1, 1)} }
2217
// Init first row and column
23-
dp[0][0]!!.max = grid[0]!![0].toLong()
24-
dp[0][0]!!.min = grid[0]!![0].toLong()
18+
dp[0][0].max = grid[0][0].toLong()
19+
dp[0][0].min = grid[0][0].toLong()
2520
for (i in 1 until rows) {
26-
dp[i][0]!!.max = grid[i]!![0] * dp[i - 1][0]!!.max
27-
dp[i][0]!!.min = grid[i]!![0] * dp[i - 1][0]!!.min
21+
dp[i][0].max = grid[i][0] * dp[i - 1][0].max
22+
dp[i][0].min = grid[i][0] * dp[i - 1][0].min
2823
}
2924
for (i in 1 until cols) {
30-
dp[0][i]!!.max = grid[0]!![i] * dp[0][i - 1]!!.max
31-
dp[0][i]!!.min = grid[0]!![i] * dp[0][i - 1]!!.min
25+
dp[0][i].max = grid[0][i] * dp[0][i - 1].max
26+
dp[0][i].min = grid[0][i] * dp[0][i - 1].min
3227
}
3328
// DP
3429
for (i in 1 until rows) {
3530
for (j in 1 until cols) {
36-
val up1 = dp[i - 1][j]!!.max * grid[i]!![j]
37-
val up2 = dp[i - 1][j]!!.min * grid[i]!![j]
38-
val left1 = dp[i][j - 1]!!.max * grid[i]!![j]
39-
val left2 = dp[i][j - 1]!!.min * grid[i]!![j]
40-
dp[i][j]!!.max = Math.max(up1, Math.max(up2, Math.max(left1, left2)))
41-
dp[i][j]!!.min = Math.min(up1, Math.min(up2, Math.min(left1, left2)))
31+
val up1 = dp[i - 1][j].max * grid[i][j]
32+
val up2 = dp[i - 1][j].min * grid[i][j]
33+
val left1 = dp[i][j - 1].max * grid[i][j]
34+
val left2 = dp[i][j - 1].min * grid[i][j]
35+
dp[i][j].max = Math.max(up1, Math.max(up2, Math.max(left1, left2)))
36+
dp[i][j].min = Math.min(up1, Math.min(up2, Math.min(left1, left2)))
4237
}
4338
}
44-
return if (dp[rows - 1][cols - 1]!!.max < 0) {
39+
return if (dp[rows - 1][cols - 1].max < 0) {
4540
-1
46-
} else (dp[rows - 1][cols - 1]!!.max % (1e9 + 7)).toInt()
41+
} else (dp[rows - 1][cols - 1].max % (1e9 + 7)).toInt()
4742
}
4843
}

src/main/kotlin/g2201_2300/s2286_booking_concert_tickets_in_groups/BookMyShow.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package g2201_2300.s2286_booking_concert_tickets_in_groups
44
// #2023_06_28_Time_1292_ms_(100.00%)_Space_98.3_MB_(100.00%)
55

66
import java.util.ArrayDeque
7+
import java.util.Arrays
78
import java.util.Deque
89

910
@Suppress("NAME_SHADOWING")
@@ -34,8 +35,8 @@ class BookMyShow(n: Int, private val m: Int) {
3435
numZerosLeft = IntArray(this.n + 2)
3536
// initialize max and total, for max we firstly set values to m
3637
// segments of size 1 are placed starting from this.n - 1
37-
max.fill(this.n - 1, this.n + n - 1, m)
38-
total.fill((this.n - 1).toLong(), this.n + n - 1, m)
38+
Arrays.fill(max, this.n - 1, this.n + n - 1, m)
39+
Arrays.fill(total, this.n - 1, this.n + n - 1, m.toLong())
3940
// calculate values of max and total for segments based on values of their children
4041
var i = this.n - 2
4142
var i1 = i * 2 + 1

src/main/kotlin/g3001_3100/s3068_find_the_maximum_sum_of_node_values/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import kotlin.math.min
99

1010
@Suppress("UNUSED_PARAMETER")
1111
class Solution {
12-
fun maximumValueSum(nums: IntArray, k: Int, edges: Array<IntArray?>?): Long {
12+
fun maximumValueSum(nums: IntArray, k: Int, edges: Array<IntArray>): Long {
1313
var res: Long = 0
1414
var d = 1 shl 30
1515
var c = 0

0 commit comments

Comments
 (0)