← 返回 amazon 的题目列表Count Distinct Number Islands
类型:online_judge
Given a grid representing a 2D map where each value is a number, you need to find the number of islands corresponding to each unique number in the grid. Islands are separated by water (0) and borders, and each island consists of horizontally or vertically neighboring identical numbers. Write a program to calculate the number of islands for each distinct number. For example, inputting [[1,2,0],[2,2,0],[1,0,0]] should return {1: 1, 2: 1}.
Input:
grid (List[List[int]]): A 2D list of non-negative integers.
Output:
dict: A dictionary where keys are distinct numbers from the grid, and values are the count of islands for each number.
Note: The size of the data can be large, so it's necessary to optimize the solution's complexity.
Example:
Input: [[1,2,0],[2,2,0],[1,0,0]]
Output: {1: 1, 2: 1}
Example
Input
[[1, 2, 0], [2, 2, 0], [1, 0, 0]]