zoukankan      html  css  js  c++  java
  • 764. Largest Plus Sign

    package LeetCode_764
    
    /**
     * 764. Largest Plus Sign
     * https://leetcode.com/problems/largest-plus-sign/
     *
     * In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the given list mines which are 0.
     * What is the largest axis-aligned plus sign of 1s contained in the grid? Return the order of the plus sign.
     * If there is none, return 0.
    An "axis-aligned plus sign of 1s of order k" has some center grid[x][y] = 1 along with 4 arms of length k-1
    going up, down, left, and right, and made of 1s.
    This is demonstrated in the diagrams below.
    Note that there could be 0s or 1s beyond the arms of the plus sign, only the relevant area of the plus sign is checked for 1s.
    
    Examples of Axis-Aligned Plus Signs of Order k:
    
    Order 1:
    000
    010
    000
    
    Order 2:
    00000
    00100
    01110
    00100
    00000
    
    Order 3:
    0000000
    0001000
    0001000
    0111110
    0001000
    0001000
    0000000
    
    Example 1:
    Input: N = 5, mines = [[4, 2]]
    Output: 2
    Explanation:
    11111
    11111
    11111
    11111
    11011
    In the above grid, the largest plus sign can only be order 2.  One of them is marked in bold.
    
    Example 2:
    Input: N = 2, mines = []
    Output: 1
    Explanation:
    There is no plus sign of order 2, but there is of order 1.
    
    Example 3:
    Input: N = 1, mines = [[0, 0]]
    Output: 0
    Explanation:
    There is no plus sign, so return 0.
     * */
    class Solution {
        /*
        * solution 1: bruce force, start scan from each 1, than expand its four directions: left, up, right, down,
        * Time:O(n^3), Space:O(n^2)
        * */
        fun orderOfLargestPlusSign(N: Int, mines: Array<IntArray>): Int {
            //init grid
            val grid = Array(N) { IntArray(N) { 1 } }
            //update value in grid to 0 by mines, because mines contains the position that is 0
            for (item in mines) {
                grid[item[0]][item[1]] = 0
            }
            var count = 0
            for (i in 0 until N) {
                for (j in 0 until N) {
                    //k represent the step can expand for current 1
                    var k = 0
                    while (canExpand(grid, N, i, j, k)) {
                        k++
                    }
                    count = Math.max(count, k)
                }
            }
            return count
        }
    
        private fun canExpand(grid: Array<IntArray>, N: Int, x: Int, y: Int, k: Int): Boolean {
            if (x - k < 0 || y - k < 0 || x + k >= N || y + k >= N) {
                return false
            }
            return grid[x-k][y]==1 && grid[x+k][y]==1 && grid[x][y-k]==1 && grid[x][y+k]==1
        }
    }
  • 相关阅读:
    Linux: 安装和启用firefox浏览器的java
    Latex: beamer
    时频分析:窗口傅立叶变换
    Python: 面向对象
    Linux: 安装NVIDIA显卡驱动
    Matlab: 路径的操作
    python--文件读写
    python--函数
    python--数据类型
    网络基础——网络协议
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/14066754.html
Copyright © 2011-2022 走看看