Created
          March 22, 2021 09:46 
        
      - 
      
 - 
        
Save pete-proton/151d9360d2054fb373dc28c98350996a to your computer and use it in GitHub Desktop.  
    River Sizes Problem 
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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