Go from bottom left to top right and find the minimum
int res = int . MaxValue ; for ( int i = n , j = 0 ; i >= 0 ; i -- , j ++ ) { res = Math . Min ( res , dp [ i , j ] ) ; if ( i < n ) res = Math . Min ( res , dp [ i + 1 , j ] ) ; if ( i > 0 ) res = Math . Min ( res , dp [ i - 1 , j ] ) ; } return res ; } static String Reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; }