Minimize cost to cover floor using tiles of dimensions 1 * 1 and 1 * 2 | Function to find the minimum cost of flooring with the given tiles ; Store the size of the 2d array ; Stores the minimum cost of flooring ; Traverse the 2d array row - wise ; If the current character is ' * ' , then skip it ; Choose the 1 * 1 tile if j is m - 1 ; If consecutive ' . ' are present , the greedily choose tile with the minimum cost ; Otherwise choose the 1 * 1 tile ; Print the minimum cost ; Driver Code
function minCost ( arr , A , B ) { let n = arr . length ; let m = arr [ 0 ] . length ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { if ( arr [ i ] [ j ] == ' ' ) continue ; if ( j == m - 1 ) ans += A ; else { if ( arr [ i ] [ j + 1 ] == ' ' ) { ans += Math . min ( 2 * A , B ) ; j ++ ; } else ans += A ; } } } document . write ( ans ) ; } var arr = [ [ ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' ] ] ; let A = 2 , B = 10 ; minCost ( arr , A , B ) ;