Untitled

 avatar
unknown
plain_text
5 months ago
1.3 kB
1
Indexable
def find_local_maximums(matrix):
    rows = len(matrix)
    if rows == 0:
        return []
    cols = len(matrix[0])
    local_maximums = []

    for i in range(rows):
        for j in range(cols):
            if matrix[i][j] == 0:
                continue
            
            size = matrix[i][j]
            # Calculate the bounds of the region
            start_row = max(0, i - (size - 2) // 2)
            end_row = min(rows - 1, i + (size - 2) // 2)
            start_col = max(0, j - (size - 2) // 2)
            end_col = min(cols - 1, j + (size - 2) // 2)
            
            is_local_max = True
            
            # Check the region for local maximum condition
            for r in range(start_row, end_row + 1):
                for c in range(start_col, end_col + 1):
                    if matrix[r][c] >= matrix[i][j]:
                        is_local_max = False
                        break
                if not is_local_max:
                    break
            
            if is_local_max:
                local_maximums.append([i, j])

    # Sort results first by row, then by column
    local_maximums.sort(key=lambda x: (x[0], x[1]))
    return local_maximums

# Example usage:
matrix = [
    [1, 2, 3],
    [4, 5, 0],
    [0, 6, 7]
]
print(find_local_maximums(matrix))  # Output: [[1, 1], [2, 2]]
Editor is loading...
Leave a Comment