Menu
Coddy logo textTech

Recap - First Valid Row

Part of the Logic & Flow section of Coddy's Kotlin journey. Lesson 45 of 63.

challenge icon

Challenge

Medium

Complete firstValidRow with parameters grid: List<List<Int>>, minimumTotal: Int. Return the index of the first row that contains no negative values and has a sum at least minimumTotal. Empty rows have sum zero and contain no negatives. Return -1 if none qualifies.

Unless stricter bounds are stated above, collections contain at most 100 elements at each level, and integer arguments and integer collection values are between -1000 and 1000. Text supplied for parsing can include invalid or out-of-range representations as described.

Return a value of type Int. Keep the supplied input/output code. It reads scalar arguments one per line; a list starts with its count followed by its elements, and a map starts with its entry count followed by each key and value. Nested lists repeat the count-and-elements format for each row. The supplied main prints the return value followed by one newline. Lists use Kotlin's standard bracketed format; print no additional labels.

Try it yourself

fun firstValidRow(grid: List<List<Int>>, minimumTotal: Int): Int {
    // Write your solution here.
    return 0
}
fun main() {
    val gridCount = readln().toInt()
    val grid = mutableListOf<List<Int>>()
    for (i0 in 0 until gridCount) {
        val gridItemCount = readln().toInt()
        val gridItem = mutableListOf<Int>()
        for (i1 in 0 until gridItemCount) {
            val gridItemItem = readln().toInt()
            gridItem.add(gridItemItem)
        }
        grid.add(gridItem)
    }
    val minimumTotal = readln().toInt()
    println(firstValidRow(grid, minimumTotal))
}

All lessons in Logic & Flow

Practice on your own: Kotlin playground