• + 0 comments

    AMONG ALL THE METHODS ONLY THIS METHOD GET TO PASS ALL TEST CASES AND IT TAKES ME 2 DAYS TO FIGURE THIS OUT EVEN THOUGH WHEN I STARTED IT FEELS SO EASY BUT COUNTINEUSLY ITS WAS EITHER GETING FIRST 2 TEST CASE PASS OR ONLY 1

    Extract layers

    for layer in range(num_layers):
        current_layer = []
    
        # Top row
        for j in range(layer, m - layer):
            current_layer.append(matrix[layer][j])
    
        # Right column
        for j in range(layer + 1, n - layer - 1):
            current_layer.append(matrix[j][m - layer - 1])
    
        # Bottom row
        for j in range(m - layer - 1, layer - 1, -1):
            if n - layer - 1 > layer:  # Avoid duplicates in single row
                current_layer.append(matrix[n - layer - 1][j])
    
        # Left column
        for j in range(n - layer - 2, layer, -1):
            if m - layer - 1 > layer:  # Avoid duplicates in single column
                current_layer.append(matrix[j][layer])
    
        layers.append(current_layer)