Maximum number of plates that can be placed from top to bottom in increasing order of size | Function to count and return the max number of plates that can be placed ; Stores the maximum number of plates ; For each i - th plate , traverse all the previous plates ; If i - th plate is smaller than j - th plate ; Include the j - th plate only if current count exceeds the previously stored count ; Update the maximum count ; Driver code ; Sorting plates in decreasing order of area
function countPlates ( plates , n ) { var maximum_plates = 1 ; var dp = Array ( n ) . fill ( 1 ) ; for ( var i = 1 ; i < n ; i ++ ) { var cur = dp [ i ] ; for ( var j = i - 1 ; j >= 0 ; j -- ) { if ( plates [ i ] [ 0 ] < plates [ j ] [ 0 ] && plates [ i ] [ 1 ] < plates [ j ] [ 1 ] ) { if ( cur + dp [ j ] > dp [ i ] ) { dp [ i ] = cur + dp [ j ] ; maximum_plates = Math . max ( maximum_plates , dp [ i ] ) ; } } } } return maximum_plates ; } var plates = [ [ 6 , 4 ] , [ 5 , 7 ] , [ 1 , 2 ] , [ 3 , 3 ] , [ 7 , 9 ] ] ; var n = plates . length ; plates . sort ( ( v1 , v2 ) => { return ( ( v2 [ 0 ] * v2 [ 1 ] ) - ( v1 [ 0 ] * v1 [ 1 ] ) ) ; } ) ; document . write ( countPlates ( plates , n ) ) ;