Minimum number of bricks that can be intersected | C # program for the above approach ; Function to find a line across a wall such that it intersects minimum number of bricks ; Declare a hashmap ; Store the maximum number of brick ending a point on x - axis ; Iterate over all the rows ; Initialize width as 0 ; Iterate over individual bricks ; Add the width of the current brick to the total width ; Increment number of bricks ending at this width position ; Update the variable , res ; Print the answer ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void leastBricks ( List < List < int > > wall ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; int res = 0 ; foreach ( List < int > subList in wall ) { int width = 0 ; for ( int i = 0 ; i < subList . Count - 1 ; i ++ ) { width += subList [ i ] ; if ( map . ContainsKey ( width ) ) map [ width ] ++ ; else map . Add ( width , 1 ) ; res = Math . Max ( res , map [ width ] ) ; } } Console . Write ( wall . Count - res ) ; } public static void Main ( ) { List < List < int > > myList = new List < List < int > > ( ) ; myList . Add ( new List < int > { 1 , 2 , 2 , 1 } ) ; myList . Add ( new List < int > { 3 , 1 , 2 } ) ; myList . Add ( new List < int > { 1 , 3 , 2 } ) ; myList . Add ( new List < int > { 2 , 4 } ) ; myList . Add ( new List < int > { 3 , 1 , 2 } ) ; myList . Add ( new List < int > { 1 , 3 , 1 , 1 } ) ; leastBricks ( myList ) ; } }