• 695. Max Area of Island (BFS)


    package LeetCode_695
    
    import java.util.*
    
    /**
     * 695. Max Area of Island
     *
     * Given a non-empty 2D array grid of 0's and 1's,
     * an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.)
     * You may assume all four edges of the grid are surrounded by water.
    Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)
    
    Example 1:
    [[0,0,1,0,0,0,0,1,0,0,0,0,0],
    [0,0,0,0,0,0,0,1,1,1,0,0,0],
    [0,1,1,0,1,0,0,0,0,0,0,0,0],
    [0,1,0,0,1,1,0,0,1,0,1,0,0],
    [0,1,0,0,1,1,0,0,1,1,1,0,0],
    [0,0,0,0,0,0,0,0,0,0,1,0,0],
    [0,0,0,0,0,0,0,1,1,1,0,0,0],
    [0,0,0,0,0,0,0,1,1,0,0,0,0]]
    Given the above grid, return 6.
    Note the answer is not 11, because the island must be connected 4-directionally.
    
    Example 2:
    [[0,0,0,0,0,0,0,0]]
    Given the above grid, return 0.
    
    Note: The length of each dimension in the given grid does not exceed 50.
     * */
    class Solution {
        /*
        * solution : DFS and BFS, find out the largest connected component,
        * Time complexity:O(mn), Space complexity:O(mn)
        * */
         fun maxAreaOfIsland(grid: Array<IntArray>): Int {
            if (grid == null || grid.isEmpty()) {
                return 0
            }
            val m = grid.size
            val n = grid[0].size
            for (i in 0 until m) {
                for (j in 0 until n) {
                    //start from land
                    if (grid[i][j] == 1) {
                       max = Math.max(max, bfs(grid, i, j))
                    }
                }
            }
            return max
        }
    
        private fun bfs(grid: Array<IntArray>, x: Int, y: Int): Int {
            val m = grid.size
            val n = grid[0].size
            val queue = LinkedList<Pair<Int, Int>>()
            val directions = intArrayOf(0, 1, 0, -1, 0)
            //because we start from 1,so current area is 1
            var currentArea = 1
            grid[x][y] = -1//set to visited
            queue.offer(Pair(x, y))
            while (queue.isNotEmpty()) {
                val cur = queue.pop()
                for (d in 0 until 4) {
                    val newX = cur.first + directions[d]
                    val newY = cur.second + directions[d + 1]
                    if (newX < 0 || newX >= m || newY < 0 || newY >= n || grid[newX][newY] != 1) {
                        continue
                    }
                    currentArea++
                    grid[newX][newY] = -1
                    queue.offer(Pair(newX, newY))
                }
            }
            return currentArea
        }
    }
  • 相关阅读:
    教务管理系统(node+express+mysql)
    poj 2485 Highways 超级大水题 kruscal
    HDU 1874 畅通工程续 + HDU 2544 最短路 最短路水题,floyd水
    HEX格式转BIN格式 MOT格式转BIN格式
    html的标签一共有多少个?
    同时存在n个线程(n>5),需要写入或者读取一个名为test.txt的文件
    poj 1258 AgriNet 水题三连发。。。又是kruscal
    招投标专家库
    poj 1789 kruscal水题
    仿Word自动套用格式,用CSS设置表格样式
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13975368.html
Copyright © 2020-2023  润新知