codeparrot/xlcost-text-to-code huggingface dataset:
Maximum value obtained by performing given operations in an Array | C # implementation of the above approach ; A function to calculate the maximum value ; basecases ; Loop to iterate and add the max value in the dp array ; Driver Code
using System ; class GFG { static void findMax ( int [ ] a , int n ) { int [ , ] dp = new int [ n , 2 ] ; int i , j ; for ( i = 0 ; i < n ; i ++ ) for ( j = 0 ; j < 2 ; j ++ ) dp [ i , j ] = 0 ; dp [ 0 , 0 ] = a [ 0 ] + a [ 1 ] ; dp [ 0 , 1 ] = a [ 0 ] * a [ 1 ] ; for ( i = 1 ; i <= n - 2 ; i ++ ) { dp [ i , 0 ] = Math . Max ( dp [ i - 1 , 0 ] , dp [ i - 1 , 1 ] ) + a [ i + 1 ] ; dp [ i , 1 ] = dp [ i - 1 , 0 ] - a [ i ] + a [ i ] * a [ i + 1 ] ; } Console . WriteLine ( Math . Max ( dp [ n - 2 , 0 ] , dp [ n - 2 , 1 ] ) ) ; } public static void Main ( ) { int [ ] arr = { 5 , - 1 , - 5 , - 3 , 2 , 9 , - 4 } ; findMax ( arr , 7 ) ; } }
Optimal strategy for a Game with modifications | C # implementation of the above approach ; Function to return sum of subarray from l to r ; calculate sum by a loop from l to r ; dp to store the values of sub problems ; if length of the array is less than k return the sum ; if the value is previously calculated ; else calculate the value ; select all the sub array of length len_r ; get the sum of that sub array ; check if it is the maximum or not ; store it in the table ; Driver code
using System ; class GFG { static int sum ( int [ ] arr , int l , int r ) { int s = 0 ; for ( int i = l ; i <= r ; i ++ ) { s += arr [ i ] ; } return s ; } static int [ , , ] dp = new int [ 101 , 101 , 101 ] ; static int solve ( int [ ] arr , int l , int r , int k ) { if ( r - l + 1 <= k ) return sum ( arr , l , r ) ; if ( dp [ l , r , k ] != 0 ) return dp [ l , r , k ] ; int sum_ = sum ( arr , l , r ) ; int len_r = ( r - l + 1 ) - k ; int len = ( r - l + 1 ) ; int ans = 0 ; for ( int i = 0 ; i < len - len_r + 1 ; i ++ ) { int sum_sub = sum ( arr , i + l , i + l + len_r - 1 ) ; ans = Math . Max ( ans , ( sum_ - sum_sub ) + ( sum_sub - solve ( arr , i + l , i + l + len_r - 1 , k ) ) ) ; } dp [ l , r , k ] = ans ; return ans ; } public static void Main ( ) { int [ ] arr = { 10 , 15 , 20 , 9 , 2 , 5 } ; int k = 2 ; int n = arr . Length ; Console . WriteLine ( solve ( arr , 0 , n - 1 , k ) ) ; } }
Find the minimum difference path from ( 0 , 0 ) to ( N | C # implementation of the approach ; Function to return the minimum difference path from ( 0 , 0 ) to ( N - 1 , M - 1 ) ; Terminating case ; Base case ; If it is already visited ; Recursive calls ; Return the value ; Driver code ; Function call
using System ; class GFG { static int MAXI = 50 ; static int [ , , ] dp = new int [ MAXI , MAXI , MAXI * MAXI ] ; static int n , m ; static int INT_MAX = int . MaxValue ; static int minDifference ( int x , int y , int k , int [ , ] b , int [ , ] c ) { int diff = 0 ; if ( x >= n y >= m ) return INT_MAX ; if ( x == n - 1 && y == m - 1 ) { diff = b [ x , y ] - c [ x , y ] ; return Math . Min ( Math . Abs ( k - diff ) , Math . Abs ( k + diff ) ) ; } int ans = dp [ x , y , k ] ; if ( ans != - 1 ) return ans ; ans = INT_MAX ; diff = b [ x , y ] - c [ x , y ] ; ans = Math . Min ( ans , minDifference ( x + 1 , y , Math . Abs ( k + diff ) , b , c ) ) ; ans = Math . Min ( ans , minDifference ( x , y + 1 , Math . Abs ( k + diff ) , b , c ) ) ; ans = Math . Min ( ans , minDifference ( x + 1 , y , Math . Abs ( k - diff ) , b , c ) ) ; ans = Math . Min ( ans , minDifference ( x , y + 1 , Math . Abs ( k - diff ) , b , c ) ) ; return ans ; } public static void Main ( ) { n = 2 ; m = 2 ; int [ , ] b = { { 1 , 4 } , { 2 , 4 } } ; int [ , ] c = { { 3 , 2 } , { 3 , 1 } } ; for ( int i = 0 ; i < MAXI ; i ++ ) { for ( int j = 0 ; j < MAXI ; j ++ ) { for ( int k = 0 ; k < MAXI * MAXI ; k ++ ) { dp [ i , j , k ] = - 1 ; } } } Console . WriteLine ( minDifference ( 0 , 0 , 0 , b , c ) ) ; } }
Longest subsequence having difference atmost K | C # program for the above approach ; Function to find the longest Special Sequence ; Creating a list with all 0 's of size  equal to the length of String ; Supporting list with all 0 's of size 26 since  the given String consists  of only lower case alphabets ; Converting the ascii value to list indices ; Determining the lower bound ; Determining the upper bound ; Filling the dp array with values ; Filling the max_length array with max length of subsequence till now ; return the max length of subsequence ; Driver Code
using System ; class GFG { static int longest_subseq ( int n , int k , String s ) { int [ ] dp = new int [ n ] ; int [ ] max_length = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { int curr = s [ i ] - ' a ' ; int lower = Math . Max ( 0 , curr - k ) ; int upper = Math . Min ( 25 , curr + k ) ; for ( int j = lower ; j < upper + 1 ; j ++ ) { dp [ i ] = Math . Max ( dp [ i ] , max_length [ j ] + 1 ) ; } max_length [ curr ] = Math . Max ( dp [ i ] , max_length [ curr ] ) ; } int ans = 0 ; foreach ( int i in dp ) ans = Math . Max ( i , ans ) ; return ans ; } public static void Main ( String [ ] args ) { String s = " geeksforgeeks " ; int n = s . Length ; int k = 3 ; Console . Write ( longest_subseq ( n , k , s ) ) ; } }
Maximum possible array sum after performing the given operation | C # implementation of the approach ; Function to return the maximum possible sum after performing the given operation ; Dp vector to store the answer ; Base value ; Return the maximum sum ; Driver code
using System ; class GFG { static int max_sum ( int [ ] a , int n ) { int [ , ] dp = new int [ n + 1 , 2 ] ; dp [ 0 , 0 ] = 0 ; dp [ 0 , 1 ] = - 999999 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { dp [ i + 1 , 0 ] = Math . Max ( dp [ i , 0 ] + a [ i ] , dp [ i , 1 ] - a [ i ] ) ; dp [ i + 1 , 1 ] = Math . Max ( dp [ i , 0 ] - a [ i ] , dp [ i , 1 ] + a [ i ] ) ; } return dp [ n , 0 ] ; } public static void Main ( String [ ] args ) { int [ ] a = { - 10 , 5 , - 4 } ; int n = a . Length ; Console . WriteLine ( max_sum ( a , n ) ) ; } }
Find the number of ways to reach Kth step in stair case | C # implementation of the approach ; Function to return the number of ways to reach the kth step ; Create the dp array ; Broken steps ; Calculate the number of ways for the rest of the positions ; If it is a blocked position ; Number of ways to get to the ith step ; Return the required answer ; Driver code
using System ; class GFG { static readonly int MOD = 1000000007 ; static int number_of_ways ( int [ ] arr , int n , int k ) { if ( k == 1 ) return 1 ; int [ ] dp = new int [ k + 1 ] ; int i ; for ( i = 0 ; i < k + 1 ; i ++ ) dp [ i ] = - 1 ; for ( i = 0 ; i < n ; i ++ ) dp [ arr [ i ] ] = 0 ; dp [ 0 ] = 1 ; dp [ 1 ] = ( dp [ 1 ] == - 1 ) ? 1 : dp [ 1 ] ; for ( i = 2 ; i <= k ; ++ i ) { if ( dp [ i ] == 0 ) continue ; dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] ; dp [ i ] %= MOD ; } return dp [ k ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 } ; int n = arr . Length ; int k = 6 ; Console . WriteLine ( number_of_ways ( arr , n , k ) ) ; } }
Minimum number of coins that can generate all the values in the given range | C # program to find minimum number of coins ; Function to find minimum number of coins ; Driver code
using System ; class GFG { static int findCount ( int n ) { return ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) + 1 ; } public static void Main ( ) { int N = 10 ; Console . Write ( findCount ( N ) ) ; } }
Calculate the number of set bits for every number from 0 to N | C # implementation of the approach ; Function to find the count of set bits in all the integers from 0 to n ; Driver code
using System ; class GFG { static int count ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } static void findSetBits ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) Console . Write ( count ( i ) + " ▁ " ) ; } public static void Main ( String [ ] args ) { int n = 5 ; findSetBits ( n ) ; } }
Count number of ways to arrange first N numbers | C # implementation of the above approach ; Function to return the count of required arrangements ; Create a vector ; Store numbers from 1 to n ; To store the count of ways ; Generate all the permutations using next_permutation in STL ; Initialize flag to true if first element is 1 else false ; Checking if the current permutation satisfies the given conditions ; If the current permutation is invalid then set the flag to false ; If valid arrangement ; Generate the next permutation ;  ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int countWays ( int n ) { List < int > a = new List < int > ( ) ; int i = 1 ; while ( i <= n ) a . Add ( i ++ ) ; int ways = 0 ; do { bool flag = ( a [ 0 ] == 1 ) ; for ( int j = 1 ; j < n ; j ++ ) { if ( Math . Abs ( a [ j ] - a [ j - 1 ] ) > 2 ) flag = false ; } if ( flag ) ways ++ ; } while ( next_permutation ( a ) ) ; return ways ; } static bool next_permutation ( List < int > p ) { for ( int a = p . Count - 2 ; a >= 0 ; -- a ) if ( p [ a ] < p [ a + 1 ] ) for ( int b = p . Count - 1 ; ; -- b ) if ( p [ b ] > p [ a ] ) { int t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . Count - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } return false ; } public static void Main ( String [ ] args ) { int n = 6 ; Console . Write ( countWays ( n ) ) ; } }
Count number of ways to arrange first N numbers | C # implementation of the approach ; Function to return the count of required arrangements ; Create the dp array ; Initialize the base cases as explained above ; ( 12 ) as the only possibility ; Generate answer for greater values ; dp [ n ] contains the desired answer ; Driver code
using System ; class GFG { static int countWays ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; dp [ 2 ] = 1 ; for ( int i = 3 ; i <= n ; i ++ ) { dp [ i ] = dp [ i - 1 ] + dp [ i - 3 ] + 1 ; } return dp [ n ] ; } public static void Main ( ) { int n = 6 ; Console . WriteLine ( countWays ( n ) ) ; } }
Number of shortest paths to reach every cell from bottom | C # program to find number of shortest paths ; Function to find number of shortest paths ; Compute the grid starting from the bottom - left corner ; Print the grid ; Driver code ; Function call
using System ; class GFG { static void NumberOfShortestPaths ( int n , int m ) { int [ , ] a = new int [ n , m ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < m ; j ++ ) { if ( j == 0 i == n - 1 ) a [ i , j ] = 1 ; else a [ i , j ] = a [ i , j - 1 ] + a [ i + 1 , j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { Console . Write ( a [ i , j ] + " ▁ " ) ; } Console . Write ( " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int n = 5 , m = 2 ; NumberOfShortestPaths ( n , m ) ; } }
Maximum sum combination from two arrays | C # program to maximum sum combination from two arrays ; Function to maximum sum combination from two arrays ; To store dp value ; For loop to calculate the value of dp ; Return the required answer ; Driver code ; Function call
using System ; class GFG { static int Max_Sum ( int [ ] arr1 , int [ ] arr2 , int n ) { int [ , ] dp = new int [ n , 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { dp [ i , 0 ] = arr1 [ i ] ; dp [ i , 1 ] = arr2 [ i ] ; continue ; } dp [ i , 0 ] = Math . Max ( dp [ i - 1 , 0 ] , dp [ i - 1 , 1 ] + arr1 [ i ] ) ; dp [ i , 1 ] = Math . Max ( dp [ i - 1 , 1 ] , dp [ i - 1 , 0 ] + arr2 [ i ] ) ; } return Math . Max ( dp [ n - 1 , 0 ] , dp [ n - 1 , 1 ] ) ; } public static void Main ( ) { int [ ] arr1 = { 9 , 3 , 5 , 7 , 3 } ; int [ ] arr2 = { 5 , 8 , 1 , 4 , 5 } ; int n = arr1 . Length ; Console . WriteLine ( Max_Sum ( arr1 , arr2 , n ) ) ; } }
Partition the array in K segments such that bitwise AND of individual segment sum is maximized | C # program to find maximum possible AND ; Function to check whether a k - segment partition is possible such that bitwise AND is ' mask ' ; dp [ i , j ] stores whether it is possible to partition first i elements into j - segments such that all j - segments are ' good ' ; Initialising dp ; Filling dp in bottom - up manner ; Finding a cut such that first l elements can be partitioned into j - 1 ' good ' segments and arr [ l + 1 ] + ... + arr [ i ] is a ' good ' segment ; Function to find maximum possible AND ; Array to store prefix sums ; Maximum no of bits in the possible answer ; This will store the final answer ; Constructing answer greedily selecting from the higher most bit ; Checking if array can be partitioned such that the bitwise AND is ans | ( 1 << i ) ; if possible , update the answer ; Return the final answer ; Driver code ; n = 11 , first element is zero to make array 1 based indexing . So , number of elements are 10 ; Function call
using System ; class GFG { static Boolean checkpossible ( int mask , int [ ] arr , int [ ] prefix , int n , int k ) { int i , j ; Boolean [ , ] dp = new Boolean [ n + 1 , k + 1 ] ; for ( i = 0 ; i < n + 1 ; i ++ ) { for ( j = 0 ; j < k + 1 ; j ++ ) { dp [ i , j ] = false ; } } dp [ 0 , 0 ] = true ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= k ; j ++ ) { for ( int l = i - 1 ; l >= 0 ; -- l ) { if ( dp [ l , j - 1 ] && ( ( ( prefix [ i ] - prefix [ l ] ) & mask ) == mask ) ) { dp [ i , j ] = true ; break ; } } } } return dp [ n , k ] ; } static int Partition ( int [ ] arr , int n , int k ) { int [ ] prefix = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ; } int LOGS = 20 ; int ans = 0 ; for ( int i = LOGS ; i >= 0 ; -- i ) { if ( checkpossible ( ans | ( 1 << i ) , arr , prefix , n , k ) ) { ans = ans | ( 1 << i ) ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 2 , 7 , 10 , 23 , 21 , 6 , 8 , 7 , 3 } ; int k = 2 ; int n = arr . Length - 1 ; Console . WriteLine ( Partition ( arr , n , k ) ) ; } }
Cost Based Tower of Hanoi | C # implementation of the approach ; Function to initialize the dp table ; Initialize with maximum value ; Function to return the minimum cost ; Base case ; If problem is already solved , return the pre - calculated answer ; Number of the auxiliary disk ; Initialize the minimum cost as Infinity ; Calculationg the cost for first case ; Calculating the cost for second case ; Minimum of both the above cases ; Store it in the dp table ; Return the minimum cost ; Driver code
using System ; class GFG { static int RODS = 3 ; static int N = 3 ; static int [ , , ] dp = new int [ N + 1 , RODS + 1 , RODS + 1 ] ; static void initialize ( ) { for ( int i = 0 ; i <= N ; i += 1 ) { for ( int j = 1 ; j <= RODS ; j ++ ) { for ( int k = 1 ; k <= RODS ; k += 1 ) { dp [ i , j , k ] = int . MaxValue ; } } } } static int mincost ( int idx , int src , int dest , int [ , ] costs ) { if ( idx > N ) return 0 ; if ( dp [ idx , src , dest ] != int . MaxValue ) return dp [ idx , src , dest ] ; int rem = 6 - ( src + dest ) ; int ans = int . MaxValue ; int case1 = costs [ src - 1 , dest - 1 ] + mincost ( idx + 1 , src , rem , costs ) + mincost ( idx + 1 , rem , dest , costs ) ; int case2 = costs [ src - 1 , rem - 1 ] + mincost ( idx + 1 , src , dest , costs ) + mincost ( idx + 1 , dest , src , costs ) + costs [ rem - 1 , dest - 1 ] + mincost ( idx + 1 , src , dest , costs ) ; ans = Math . Min ( case1 , case2 ) ; dp [ idx , src , dest ] = ans ; return ans ; } public static void Main ( String [ ] args ) { int [ , ] costs = { { 0 , 1 , 2 } , { 2 , 0 , 1 } , { 3 , 2 , 0 } } ; initialize ( ) ; Console . WriteLine ( mincost ( 1 , 1 , 3 , costs ) ) ; } }
Minimum time required to rot all oranges | Dynamic Programming | C # implementation of the above approach ; DP table to memoize the values ; Visited array to keep track of visited nodes in order to avoid infinite loops ; Function to return the minimum of four numbers ; Function to return the minimum distance to any rotten orange from [ i , j ] ; If i , j lie outside the array ; If 0 then it can 't lead  to any path so return  INT_MAX ; If 2 then we have reached our rotten oranges so return from here ; If this node is already visited then return to avoid infinite loops ; Mark the current node as visited ; Check in all four possible directions ; Take the minimum of all ; If result already exists in the table check if min_value is less than existing value ; Function to return the minimum time required to rot all the oranges ; Calculate the minimum distances to any rotten orange from all the fresh oranges ; Pick the maximum distance of fresh orange to some rotten orange ; If all oranges can be rotten ; Driver Code
using System ; class GFG { static int C = 5 ; static int R = 3 ; static int INT_MAX = 10000000 ; static int [ , ] table = new int [ R , C ] ; static int [ , ] visited = new int [ R , C ] ; static int min ( int p , int q , int r , int s ) { int temp1 = p < q ? p : q ; int temp2 = r < s ? r : s ; if ( temp1 < temp2 ) return temp1 ; return temp2 ; } static int Distance ( int [ , ] arr , int i , int j ) { if ( i >= R j >= C i < 0 j < 0 ) return INT_MAX ; else if ( arr [ i , j ] == 0 ) { table [ i , j ] = INT_MAX ; return INT_MAX ; } else if ( arr [ i , j ] == 2 ) { table [ i , j ] = 0 ; return 0 ; } else if ( visited [ i , j ] == 1 ) { return INT_MAX ; } else { visited [ i , j ] = 1 ; int temp1 = Distance ( arr , i + 1 , j ) ; int temp2 = Distance ( arr , i - 1 , j ) ; int temp3 = Distance ( arr , i , j + 1 ) ; int temp4 = Distance ( arr , i , j - 1 ) ; int min_value = 1 + min ( temp1 , temp2 , temp3 , temp4 ) ; if ( table [ i , j ] > 0 && table [ i , j ] < INT_MAX ) { if ( min_value < table [ i , j ] ) table [ i , j ] = min_value ; } else table [ i , j ] = min_value ; visited [ i , j ] = 0 ; } return table [ i , j ] ; } static int minTime ( int [ , ] arr ) { int max = 0 ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { if ( arr [ i , j ] == 1 ) Distance ( arr , i , j ) ; } } for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { if ( arr [ i , j ] == 1 && table [ i , j ] > max ) max = table [ i , j ] ; } } if ( max < INT_MAX ) return max ; return - 1 ; } public static void Main ( string [ ] args ) { int [ , ] arr = { { 2 , 1 , 0 , 2 , 1 } , { 0 , 0 , 1 , 2 , 1 } , { 1 , 0 , 0 , 2 , 1 } } ; Console . Write ( minTime ( arr ) ) ; } }
Maximum sum of non | C # program to implement above approach ; Variable to store states of dp ; Variable to check if a given state has been solved ; Function to find the maximum sum subsequence such that no two elements are adjacent ; Base case ; To check if a state has been solved ; Variable to store prefix sum for sub - array { i , j } ; Required recurrence relation ; Returning the value ; Driver code ; Input array
using System ; class GFG { static int maxLen = 10 ; static int [ ] dp = new int [ maxLen ] ; static bool [ ] visit = new bool [ maxLen ] ; static int maxSum ( int [ ] arr , int i , int n , int k ) { if ( i >= n ) return 0 ; if ( visit [ i ] ) return dp [ i ] ; visit [ i ] = true ; int tot = 0 ; dp [ i ] = maxSum ( arr , i + 1 , n , k ) ; for ( int j = i ; j < ( i + k ) && ( j < n ) ; j ++ ) { tot += arr [ j ] ; dp [ i ] = Math . Max ( dp [ i ] , tot + maxSum ( arr , j + 2 , n , k ) ) ; } return dp [ i ] ; } static public void Main ( ) { int [ ] arr = { - 1 , 2 , - 3 , 4 , 5 } ; int k = 2 ; int n = arr . Length ; Console . WriteLine ( maxSum ( arr , 0 , n , k ) ) ; } }
Maximum subset sum such that no two elements in set have same digit in them | C # implementation of above approach ; Function to create mask for every number ; Recursion for Filling DP array ; Base Condition ; Recurrence Relation ; Function to find Maximum Subset Sum ; Initialize DP array ; Iterate over all possible masks of 10 bit number ; Driver Code
using System ; class GFG { static int [ ] dp = new int [ 1024 ] ; static int get_binary ( int u ) { int ans = 0 ; while ( u > 0 ) { int rem = u % 10 ; ans |= ( 1 << rem ) ; u /= 10 ; } return ans ; } static int recur ( int u , int [ ] array , int n ) { if ( u == 0 ) return 0 ; if ( dp [ u ] != - 1 ) return dp [ u ] ; for ( int i = 0 ; i < n ; i ++ ) { int mask = get_binary ( array [ i ] ) ; if ( ( mask u ) == u ) { dp [ u ] = Math . Max ( Math . Max ( 0 , dp [ u ^ mask ] ) + array [ i ] , dp [ u ] ) ; } } return dp [ u ] ; } static int solve ( int [ ] array , int n ) { for ( int i = 0 ; i < ( 1 << 10 ) ; i ++ ) { dp [ i ] = - 1 ; } int ans = 0 ; for ( int i = 0 ; i < ( 1 << 10 ) ; i ++ ) { ans = Math . Max ( ans , recur ( i , array , n ) ) ; } return ans ; } static public void Main ( ) { int [ ] array = { 22 , 132 , 4 , 45 , 12 , 223 } ; int n = array . Length ; Console . WriteLine ( solve ( array , n ) ) ; } }
Minimize the sum after choosing elements from the given three arrays | C # implementation of the above approach ; Function to return the minimized sum ; If all the indices have been used ; If this value is pre - calculated then return its value from dp array instead of re - computing it ; If A [ i - 1 ] was chosen previously then only B [ i ] or C [ i ] can chosen now choose the one which leads to the minimum sum ; If B [ i - 1 ] was chosen previously then only A [ i ] or C [ i ] can chosen now choose the one which leads to the minimum sum ; If C [ i - 1 ] was chosen previously then only A [ i ] or B [ i ] can chosen now choose the one which leads to the minimum sum ; Driver code ; Initialize the dp [ ] [ ] array ; min ( start with A [ 0 ] , start with B [ 0 ] , start with C [ 0 ] )
using System ; class GFG { static int SIZE = 3 ; static int N = 3 ; static int minSum ( int [ ] A , int [ ] B , int [ ] C , int i , int n , int curr , int [ , ] dp ) { if ( n <= 0 ) return 0 ; if ( dp [ n , curr ] != - 1 ) return dp [ n , curr ] ; if ( curr == 0 ) { return dp [ n , curr ] = Math . Min ( B [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 1 , dp ) , C [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 2 , dp ) ) ; } if ( curr == 1 ) return dp [ n , curr ] = Math . Min ( A [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 0 , dp ) , C [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 2 , dp ) ) ; return dp [ n , curr ] = Math . Min ( A [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 0 , dp ) , B [ i ] + minSum ( A , B , C , i + 1 , n - 1 , 1 , dp ) ) ; } public static void Main ( ) { int [ ] A = { 1 , 50 , 1 } ; int [ ] B = { 50 , 50 , 50 } ; int [ ] C = { 50 , 50 , 50 } ; int [ , ] dp = new int [ SIZE , N ] ; for ( int i = 0 ; i < SIZE ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) dp [ i , j ] = - 1 ; Console . WriteLine ( Math . Min ( A [ 0 ] + minSum ( A , B , C , 1 , SIZE - 1 , 0 , dp ) , Math . Min ( B [ 0 ] + minSum ( A , B , C , 1 , SIZE - 1 , 1 , dp ) , C [ 0 ] + minSum ( A , B , C , 1 , SIZE - 1 , 2 , dp ) ) ) ) ; } }
Maximise matrix sum by following the given Path | C # implementation of the approach ; To store the states of the DP ; Function to return the maximum of the three integers ; Function to return the maximum score ; Base cases ; If the state has already been solved then return it ; Marking the state as solved ; Growing phase ; } Shrinking phase ; Returning the solved state ; Driver code
using System ; class GFG { static int n = 3 ; static int [ , , ] dp = new int [ n , n , 2 ] ; static bool [ , , ] v = new bool [ n , n , 2 ] ; static int max ( int a , int b , int c ) { int m = a ; if ( m < b ) { m = b ; } if ( m < c ) { m = c ; } return m ; } static int maxScore ( int [ , ] arr , int i , int j , int s ) { if ( i > n - 1 i < 0 j > n - 1 ) { return 0 ; } if ( ( i == 0 ) && ( j == ( n - 1 ) ) ) { return arr [ i , j ] ; } if ( v [ i , j , s ] ) { return dp [ i , j , s ] ; } v [ i , j , s ] = true ; if ( s != 1 ) { dp [ i , j , s ] = arr [ i , j ] + Math . Max ( maxScore ( arr , i + 1 , j , s ) , Math . Max ( maxScore ( arr , i , j + 1 , s ) , maxScore ( arr , i - 1 , j , ( s == 1 ) ? 0 : 1 ) ) ) ; else { dp [ i , j , s ] = arr [ i , j ] + Math . Max ( maxScore ( arr , i - 1 , j , s ) , maxScore ( arr , i , j + 1 , s ) ) ; } return dp [ i , j , s ] ; } static public void Main ( ) { int [ , ] arr = { { 1 , 1 , 1 } , { 1 , 5 , 1 } , { 1 , 1 , 1 } } ; Console . WriteLine ( maxScore ( arr , 0 , 0 , 0 ) ) ; } }