Skip to content

Instantly share code, notes, and snippets.

@pete-proton
Created March 22, 2021 09:46
Show Gist options
  • Save pete-proton/151d9360d2054fb373dc28c98350996a to your computer and use it in GitHub Desktop.
Save pete-proton/151d9360d2054fb373dc28c98350996a to your computer and use it in GitHub Desktop.
River Sizes Problem
package com.algoexpert.program
fun riverSizes(matrix: List<List<Int>>): List<Int> {
val m = matrix
.map { it.toMutableList() }
.toMutableList()
fun size(i: Int, j: Int): Int {
if (i < 0 || i >= m.size ||
j < 0 || j >= m[i].size ||
m[i][j] == 0) {
return 0
}
m[i][j] = 0
return 1 + size(i + 1, j) +
size(i - 1, j) +
size(i, j + 1) +
size(i, j - 1)
}
val result = mutableListOf<Int>()
for(i in m.indices){
for(j in m[i].indices){
var s = size(i, j)
if (s > 0) {
result.add(s)
}
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment