Count Isolated Warehouse Items
Zepto's success hinges on efficient warehouse management and precise inventory tracking. Our warehouses utilize a grid-based layout where each cell represents a storage slot. Identifying items that are unexpectedly isolated from others can signal potential misplacement or inefficiencies in storage. This problem asks you to analyze a warehouse grid to count such isolated items.
Implement the function countIsolatedItems that takes a 2D array (matrix) representing a warehouse grid. Your task is to:
1), determine if it is "isolated".1) directly adjacent to it horizontally or vertically (i.e., its top, bottom, left, and right neighbors are either 0 or outside the grid boundaries).Input:
grid: A number[][] (2D array of numbers) representing the warehouse layout. Each element grid[i][j] will be either 0 (empty slot) or 1 (contains an item).Output:
number representing the total count of isolated items in the grid.grid[0][0] only needs to check grid[0][1] (right) and grid[1][0] (bottom) to determine if it's isolated.1x1 grid containing 1 will result in 1 isolated item. A 1x1 grid containing 0 will result in 0 isolated items.0s, the result will be 0. If the grid contains only 1s, the result will be 0 (unless it's a 1x1 grid), as every item would have adjacent items.Examples
Example 1
Input:
grid = [[0,1,0],[1,0,1],[0,1,0]]
Output:
4
Note:
Items at (0,1), (1,0), (1,2), and (2,1) are all surrounded by zeros or boundaries, making them isolated. Total 4.
Example 2
Input:
grid = [[1,1,0],[0,1,0],[0,0,1]]
Output:
1
Note:
Item at (0,0) is adjacent to (0,1). Item at (0,1) is adjacent to (0,0) and (1,1). Item at (1,1) is adjacent to (0,1). Only the item at (2,2) has no adjacent items (horizontally or vertically). Total 1.
Example 3
Input:
grid = [[1]]
Output:
1
Note:
The single item at (0,0) has no adjacent items, making it isolated. Total 1.
Constraints
1 <= grid.length <= 100 (number of rows)1 <= grid[i].length <= 100 (number of columns, grid[i].length will be consistent for all rows)grid[i][j] will be either 0 or 1.Loading editor...