codeparrot/xlcost-text-to-code huggingface dataset:
Sum of heights of all individual nodes in a binary tree | C # program to find sum of heights of all nodes in a binary tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; Compute the " maxHeight " of a particular Node ; compute the height of each subtree ; use the larger one ; Helper function that allocates a new Node with the given data and NULL left and right pointers . ; Function to sum of heights of individual Nodes Uses Inorder traversal ; Driver code
using System ; class GfG { public class Node { public int data ; public Node left ; public Node right ; } static int getHeight ( Node Node ) { if ( Node == null ) return 0 ; else { int lHeight = getHeight ( Node . left ) ; int rHeight = getHeight ( Node . right ) ; if ( lHeight > rHeight ) return ( lHeight + 1 ) ; else return ( rHeight + 1 ) ; } } static Node newNode ( int data ) { Node Node = new Node ( ) ; Node . data = data ; Node . left = null ; Node . right = null ; return ( Node ) ; } static int getTotalHeight ( Node root ) { if ( root == null ) return 0 ; return getTotalHeight ( root . left ) + getHeight ( root ) + getTotalHeight ( root . right ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; Console . Write ( " Sum ▁ of ▁ heights ▁ of ▁ all ▁ Nodes ▁ = ▁ " + getTotalHeight ( root ) ) ; } }
Path with maximum average value | C # Code for Path with maximum average value ; method returns maximum average of all path of cost matrix ; Initialize first column of total cost ( dp ) array ; Initialize first row of dp array ; Construct rest of the dp array ; divide maximum sum by constant path length : ( 2 N - 1 ) for getting average ; Driver Code
using System ; class GFG { public static double maxAverageOfPath ( int [ , ] cost , int N ) { int [ , ] dp = new int [ N + 1 , N + 1 ] ; dp [ 0 , 0 ] = cost [ 0 , 0 ] ; for ( int i = 1 ; i < N ; i ++ ) dp [ i , 0 ] = dp [ i - 1 , 0 ] + cost [ i , 0 ] ; for ( int j = 1 ; j < N ; j ++ ) dp [ 0 , j ] = dp [ 0 , j - 1 ] + cost [ 0 , j ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 1 ; j < N ; j ++ ) dp [ i , j ] = Math . Max ( dp [ i - 1 , j ] , dp [ i , j - 1 ] ) + cost [ i , j ] ; return ( double ) dp [ N - 1 , N - 1 ] / ( 2 * N - 1 ) ; } public static void Main ( ) { int [ , ] cost = { { 1 , 2 , 3 } , { 6 , 5 , 4 } , { 7 , 3 , 9 } } ; Console . Write ( maxAverageOfPath ( cost , 3 ) ) ; } }
Maximum weight path ending at any element of last row in a matrix | C # Code for Maximum weight path ending at any element of last row in a matrix ; Function which return the maximum weight path sum ; create 2D matrix to store the sum of the path ; Initialize first column of total weight array ( dp [ i to N ] [ 0 ] ) ; Calculate rest path sum of weight matrix ; find the max weight path sum to reach the last row ; return maximum weight path sum ; Driver program to test above function
using System ; class GFG { public static int maxCost ( int [ , ] mat , int N ) { int [ , ] dp = new int [ N , N ] ; dp [ 0 , 0 ] = mat [ 0 , 0 ] ; for ( int i = 1 ; i < N ; i ++ ) dp [ i , 0 ] = mat [ i , 0 ] + dp [ i - 1 , 0 ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 1 ; j < i + 1 && j < N ; j ++ ) dp [ i , j ] = mat [ i , j ] + Math . Max ( dp [ i - 1 , j - 1 ] , dp [ i - 1 , j ] ) ; int result = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( result < dp [ N - 1 , i ] ) result = dp [ N - 1 , i ] ; return result ; } public static void Main ( ) { int [ , ] mat = { { 4 , 1 , 5 , 6 , 1 } , { 2 , 9 , 2 , 11 , 10 } , { 15 , 1 , 3 , 15 , 2 } , { 16 , 92 , 41 , 4 , 3 } , { 8 , 142 , 6 , 4 , 8 } } ; int N = 5 ; Console . Write ( " Maximum ▁ Path ▁ Sum ▁ : ▁ " + maxCost ( mat , N ) ) ; } }
Number of permutation with K inversions | C # program to find number of permutation with K inversion using Memoization ; Limit on N and K ; 2D array memo for stopping solving same problem again ; method recursively calculates permutation with K inversion ; base cases ; if already solved then return result directly ; calling recursively all subproblem of permutation size N - 1 ; Call recursively only if total inversion to be made are less than size ; store result into memo ; Driver Code
using System ; class GFG { static int M = 100 ; static int [ , ] memo = new int [ M , M ] ; static int numberOfPermWithKInversion ( int N , int K ) { if ( N == 0 ) return 0 ; if ( K == 0 ) return 1 ; if ( memo [ N , K ] != 0 ) return memo [ N , K ] ; int sum = 0 ; for ( int i = 0 ; i <= K ; i ++ ) { if ( i <= N - 1 ) sum += numberOfPermWithKInversion ( N - 1 , K - i ) ; } memo [ N , K ] = sum ; return sum ; } static public void Main ( ) { int N = 4 ; int K = 2 ; Console . WriteLine ( numberOfPermWithKInversion ( N , K ) ) ; } }
A Space Optimized DP solution for 0 | C # program of a space optimized DP solution for 0 - 1 knapsack problem . ; val [ ] is for storing maximum profit for each weight wt [ ] is for storing weights n number of item W maximum capacity of bag mat [ 2 , W + 1 ] to store final result ; matrix to store final result ; iterate through all items ; one by one traverse each element ; traverse all weights j <= W ; if i is odd that mean till now we have odd number of elements so we store result in 1 th indexed row ; check for each value ; include element ; exclude element ; if i is even that means till now we have even number of elements so we store result in 0 th indexed row ; Return mat [ 0 , W ] if n is odd , else mat [ 1 , W ] ; Driver Code
using System ; class GFG { static int KnapSack ( int [ ] val , int [ ] wt , int n , int W ) { int [ , ] mat = new int [ 2 , W + 1 ] ; int i = 0 ; while ( i < n ) { int j = 0 ; if ( i % 2 != 0 ) { while ( ++ j <= W ) { if ( wt [ i ] <= j ) { mat [ 1 , j ] = Math . Max ( val [ i ] + mat [ 0 , j - wt [ i ] ] , mat [ 0 , j ] ) ; } else { mat [ 1 , j ] = mat [ 0 , j ] ; } } } else { while ( ++ j <= W ) { if ( wt [ i ] <= j ) { mat [ 0 , j ] = Math . Max ( val [ i ] + mat [ 1 , j - wt [ i ] ] , mat [ 1 , j ] ) ; } else { mat [ 0 , j ] = mat [ 1 , j ] ; } } } i ++ ; } return ( n % 2 != 0 ) ? mat [ 0 , W ] : mat [ 1 , W ] ; } public static void Main ( String [ ] args ) { int [ ] val = { 7 , 8 , 4 } ; int [ ] wt = { 3 , 8 , 6 } ; int W = 10 , n = 3 ; Console . WriteLine ( KnapSack ( val , wt , n , W ) ) ; } }
Maximum profit by buying and selling a share at most k times | C # program to find out maximum profit by buying and selling a share atmost k times given stock price of n days ; Function to find out maximum profit by buying & selling / a share atmost k times given stock price of n days ; table to store results of subproblems profit [ t ] [ i ] stores maximum profit using atmost t transactions up to day i ( including day i ) ; For day 0 , you can 't earn money  irrespective of how many times you trade ; profit is 0 if we don 't do any transaction  (i.e. k =0) ; fill the table in bottom - up fashion ; Driver code to test above
using System ; class GFG { static int maxProfit ( int [ ] price , int n , int k ) { int [ , ] profit = new int [ k + 1 , n + 1 ] ; for ( int i = 0 ; i <= k ; i ++ ) profit [ i , 0 ] = 0 ; for ( int j = 0 ; j <= n ; j ++ ) profit [ 0 , j ] = 0 ; for ( int i = 1 ; i <= k ; i ++ ) { int prevDiff = int . MinValue ; for ( int j = 1 ; j < n ; j ++ ) { prevDiff = Math . Max ( prevDiff , profit [ i - 1 , j - 1 ] - price [ j - 1 ] ) ; profit [ i , j ] = Math . Max ( profit [ i , j - 1 ] , price [ j ] + prevDiff ) ; } } return profit [ k , n - 1 ] ; } public static void Main ( ) { int k = 3 ; int [ ] price = { 12 , 14 , 17 , 10 , 14 , 13 , 12 , 15 } ; int n = price . Length ; Console . Write ( " Maximum ▁ profit ▁ is : ▁ " + maxProfit ( price , n , k ) ) ; } }
Check if an array has a majority element | Hashing based C # program to find if there is a majority element in input array . ; Returns true if there is a majority element in a [ ] ; Insert all elements in a hash table ; Check if frequency of any element is n / 2 or more . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static Boolean isMajority ( int [ ] a , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( a [ i ] ) ) { var val = mp [ a [ i ] ] ; mp . Remove ( a [ i ] ) ; mp . Add ( a [ i ] , val + 1 ) ; } else { mp . Add ( a [ i ] , 1 ) ; } } foreach ( KeyValuePair < int , int > x in mp ) if ( x . Value >= n / 2 ) return true ; return false ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 3 , 9 , 2 , 2 } ; int n = a . Length ; if ( isMajority ( a , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Count even length binary sequences with same sum of first and second half bits | A memoization based C # program to count even length binary sequences such that the sum of first and second half bits is same ; A lookup table to store the results of subproblems ; dif is difference between sums of first n bits and last n bits i . e . , dif = ( Sum of first n bits ) - ( Sum of last n bits ) ; We can 't cover difference of  more than n with 2n bits ; n == 1 , i . e . , 2 bit long sequences ; Check if this subproblem is already solved n is added to dif to make sure index becomes positive ; int res = First bit is 0 & last bit is 1 ; First and last bits are same ; First bit is 1 & last bit is 0 ; Store result in lookup table and return the result ; A Wrapper over countSeqUtil ( ) . It mainly initializes lookup table , then calls countSeqUtil ( ) ; Initialize all entries of lookup table as not filled memset ( lookup , - 1 , sizeof ( lookup ) ) ; ; call countSeqUtil ( ) ; Driver program
using System ; class GFG { static int [ , ] lookup = new int [ 1000 , 1000 ] ; static int countSeqUtil ( int n , int dif ) { if ( Math . Abs ( dif ) > n ) return 0 ; if ( n == 1 && dif == 0 ) return 2 ; if ( n == 1 && Math . Abs ( dif ) == 1 ) return 1 ; if ( lookup [ n , n + dif ] != - 1 ) return lookup [ n , n + dif ] ; countSeqUtil ( n - 1 , dif + 1 ) + 2 * countSeqUtil ( n - 1 , dif ) + countSeqUtil ( n - 1 , dif - 1 ) ; return lookup [ n , n + dif ] = res ; } static int countSeq ( int n ) { for ( int k = 0 ; k < lookup . GetLength ( 0 ) ; k ++ ) { for ( int j = 0 ; j < lookup . GetLength ( 1 ) ; j ++ ) { lookup [ k , j ] = - 1 ; } } return countSeqUtil ( n , 0 ) ; } public static void Main ( ) { int n = 2 ; Console . WriteLine ( " Count ▁ of ▁ sequences ▁ is ▁ " + countSeq ( n ) ) ; } }
Boolean Parenthesization Problem | DP |  ; Count number of True in left Partition ; Count number of False in left Partition ; Count number of True in right Partition ; Count number of False in right Partition ; Evaluate AND operation ; Evaluate OR operation ; Evaluate XOR operation ; Driver code ; We obtain the string T | T & F ^ T ; There are 4 ways ( ( T T ) & ( F ^ T ) ) , ( T | ( T & ( F ^ T ) ) ) , ( ( ( T T ) & F ) ^ T ) and ( T | ( ( T & F ) ^ T ) )
using System ; class GFG { static int parenthesis_count ( string str , int i , int j , int isTrue , int [ , , ] dp ) { if ( i > j ) return 0 ; if ( i == j ) { if ( isTrue == 1 ) { return ( str [ i ] == ' T ' ) ? 1 : 0 ; } else { return ( str [ i ] == ' F ' ) ? 1 : 0 ; } } if ( dp [ i , j , isTrue ] != - 1 ) return dp [ i , j , isTrue ] ; int temp_ans = 0 ; int leftTrue , rightTrue , leftFalse , rightFalse ; for ( int k = i + 1 ; k <= j - 1 ; k = k + 2 ) { if ( dp [ i , k - 1 , 1 ] != - 1 ) leftTrue = dp [ i , k - 1 , 1 ] ; else { leftTrue = parenthesis_count ( str , i , k - 1 , 1 , dp ) ; } if ( dp [ i , k - 1 , 0 ] != - 1 ) leftFalse = dp [ i , k - 1 , 0 ] ; else { leftFalse = parenthesis_count ( str , i , k - 1 , 0 , dp ) ; } if ( dp [ k + 1 , j , 1 ] != - 1 ) rightTrue = dp [ k + 1 , j , 1 ] ; else { rightTrue = parenthesis_count ( str , k + 1 , j , 1 , dp ) ; } if ( dp [ k + 1 , j , 0 ] != - 1 ) rightFalse = dp [ k + 1 , j , 0 ] ; else { rightFalse = parenthesis_count ( str , k + 1 , j , 0 , dp ) ; } if ( str [ k ] == ' & ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightTrue ; } else { temp_ans = temp_ans + leftTrue * rightFalse + leftFalse * rightTrue + leftFalse * rightFalse ; } } else if ( str [ k ] == ' ▁ ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightTrue + leftTrue * rightFalse + leftFalse * rightTrue ; } else { temp_ans = temp_ans + leftFalse * rightFalse ; } } else if ( str [ k ] == ' ^ ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightFalse + leftFalse * rightTrue ; } else { temp_ans = temp_ans + leftTrue * rightTrue + leftFalse * rightFalse ; } } dp [ i , j , isTrue ] = temp_ans ; } return temp_ans ; } static int countWays ( int N , string S ) { int [ , , ] dp = new int [ N + 1 , N + 1 , 2 ] ; for ( int i = 0 ; i < ( N + 1 ) ; i ++ ) { for ( int j = 0 ; j < ( N + 1 ) ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) { dp [ i , j , k ] = - 1 ; } } } return parenthesis_count ( S , 0 , N - 1 , 1 , dp ) ; } static void Main ( ) { string symbols = " TTFT " ; string operators = " | & ^ " ; string S = " " ; int j = 0 ; for ( int i = 0 ; i < symbols . Length ; i ++ ) { S = S + symbols [ i ] ; if ( j < operators . Length ) S = S + operators [ j ++ ] ; } int N = S . Length ; Console . WriteLine ( countWays ( N , S ) ) ; } }
Remove minimum elements from either side such that 2 * min becomes more than max | C # implementation of above approach ; A utility function to find minimum of two numbers ; A utility function to find minimum in arr [ l . . h ] ; A utility function to find maximum in arr [ l . . h ] ; Returns the minimum number of removals from either end in arr [ l . . h ] so that 2 * min becomes greater than max . ; If there is 1 or less elements , return 0 For a single element , 2 * min > max ( Assumption : All elements are positive in arr [ ] ) ; 1 ) Find minimum and maximum in arr [ l . . h ] ; If the property is followed , no removals needed ; Otherwise remove a character from left end and recur , then remove a character from right end and recur , take the minimum of two is returned ; Driver Code
using System ; class GFG { static int min ( int a , int b ) { return ( a < b ) ? a : b ; } static int min ( int [ ] arr , int l , int h ) { int mn = arr [ l ] ; for ( int i = l + 1 ; i <= h ; i ++ ) if ( mn > arr [ i ] ) mn = arr [ i ] ; return mn ; } static int max ( int [ ] arr , int l , int h ) { int mx = arr [ l ] ; for ( int i = l + 1 ; i <= h ; i ++ ) if ( mx < arr [ i ] ) mx = arr [ i ] ; return mx ; } static int minRemovals ( int [ ] arr , int l , int h ) { if ( l >= h ) return 0 ; int mn = min ( arr , l , h ) ; int mx = max ( arr , l , h ) ; if ( 2 * mn > mx ) return 0 ; return min ( minRemovals ( arr , l + 1 , h ) , minRemovals ( arr , l , h - 1 ) ) + 1 ; } public static void Main ( ) { int [ ] arr = { 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 } ; int n = arr . Length ; Console . Write ( minRemovals ( arr , 0 , n - 1 ) ) ; } }
Sum of heights of all individual nodes in a binary tree | C # program to find sum of heights of all nodes in a binary tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new Node with the given data and null left and right pointers . ; Function to sum of heights of individual Nodes Uses Inorder traversal ; Driver code
using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node left ; public Node right ; } ; static int sum ; static Node newNode ( int data ) { Node Node = new Node ( ) ; Node . data = data ; Node . left = null ; Node . right = null ; return ( Node ) ; } static int getTotalHeightUtil ( Node root ) { if ( root == null ) { return 0 ; } int lh = getTotalHeightUtil ( root . left ) ; int rh = getTotalHeightUtil ( root . right ) ; int h = Math . Max ( lh , rh ) + 1 ; sum = sum + h ; return h ; } static int getTotalHeight ( Node root ) { sum = 0 ; getTotalHeightUtil ( root ) ; return sum ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; Console . Write ( " Sum ▁ of ▁ heights ▁ of ▁ all ▁ Nodes ▁ = ▁ { 0 } " , getTotalHeight ( root ) ) ; } }
Inorder Tree Traversal without Recursion | Non - recursive C # program for inorder traversal ; Class containing left and right child of current node and key value ; Class to print the inorder traversal ; traverse the tree ; Reach the left most Node of the curr Node ; place pointer to a tree node on the stack before traversing the node 's left subtree  ; Current must be NULL at this point ; we have visited the node and its left subtree . Now , it ' s ▁ right ▁ ▁ subtree ' s turn ; Driver program to test above functions ; creating a binary tree and entering the nodes
using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual void inorder ( ) { if ( root == null ) { return ; } Stack < Node > s = new Stack < Node > ( ) ; Node curr = root ; while ( curr != null s . Count > 0 ) { while ( curr != null ) { s . Push ( curr ) ; curr = curr . left ; } curr = s . Pop ( ) ; Console . Write ( curr . data + " ▁ " ) ; curr = curr . right ; } } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . inorder ( ) ; } }
Count all possible paths from top left to bottom right of a mXn matrix | A C # program to count all possible paths from top left to bottom right ; Returns count of possible paths to reach cell at row number m and column number n from the topmost leftmost cell ( cell at 1 , 1 ) ; Create a 2D table to store results of subproblems ; Count of paths to reach any cell in first column is 1 ; Count of paths to reach any cell in first column is 1 ; Calculate count of paths for other cells in bottom - up manner using the recursive solution ; By uncommenting the last part the code calculates the total possible paths if the diagonal Movements are allowed count [ i , j ] = count [ i - 1 , j ] + count [ i , j - 1 ] ; + count [ i - 1 ] [ j - 1 ] ; ; Driver program to test above function
using System ; public class GFG { static int numberOfPaths ( int m , int n ) { int [ , ] count = new int [ m , n ] ; for ( int i = 0 ; i < m ; i ++ ) count [ i , 0 ] = 1 ; for ( int j = 0 ; j < n ; j ++ ) count [ 0 , j ] = 1 ; for ( int i = 1 ; i < m ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) } return count [ m - 1 , n - 1 ] ; } static public void Main ( ) { Console . WriteLine ( numberOfPaths ( 3 , 3 ) ) ; } }
Split array into two equal length subsets such that all repetitions of a number lies in a single subset | C # program for the above approach ; Function to create the frequency array of the given array arr [ ] ; Hashmap to store the frequencies ; Store freq for each element ; Get the total frequencies ; Store frequencies in subset [ ] array ; Return frequency array ; Function to check is sum N / 2 can be formed using some subset ; dp [ i ] [ j ] store the answer to form sum j using 1 st i elements ; Initialize dp [ ] [ ] with true ; Fill the subset table in the bottom up manner ; If current element is less than j ; Update current state ; Return the result ; Function to check if the given array can be split into required sets ; Store frequencies of arr [ ] ; If size of arr [ ] is odd then print " Yes " ; Check if answer is true or not ; Print the result ; Driver code ; Given array arr [ ] ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int [ ] findSubsets ( int [ ] arr ) { Dictionary < int , int > M = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( M . ContainsKey ( arr [ i ] ) ) { M [ arr [ i ] ] ++ ; } else { M [ arr [ i ] ] = 1 ; } } int [ ] subsets = new int [ M . Count ] ; int I = 0 ; foreach ( KeyValuePair < int , int > playerEntry in M ) { subsets [ I ] = playerEntry . Value ; I ++ ; } return subsets ; } static bool subsetSum ( int [ ] subsets , int target ) { bool [ , ] dp = new bool [ subsets . Length + 1 , target + 1 ] ; for ( int i = 0 ; i < dp . GetLength ( 0 ) ; i ++ ) dp [ i , 0 ] = true ; for ( int i = 1 ; i <= subsets . Length ; i ++ ) { for ( int j = 1 ; j <= target ; j ++ ) { dp [ i , j ] = dp [ i - 1 , j ] ; if ( j >= subsets [ i - 1 ] ) { dp [ i , j ] |= dp [ i - 1 , j - subsets [ i - 1 ] ] ; } } } return dp [ subsets . Length , target ] ; } static void divideInto2Subset ( int [ ] arr ) { int [ ] subsets = findSubsets ( arr ) ; if ( ( arr . Length ) % 2 == 1 ) { Console . WriteLine ( " No " ) ; return ; } bool isPossible = subsetSum ( subsets , arr . Length / 2 ) ; if ( isPossible ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } static void Main ( ) { int [ ] arr = { 2 , 1 , 2 , 3 } ; divideInto2Subset ( arr ) ; } }
Assembly Line Scheduling | DP | A C # program to find minimum possible time by the car chassis to complete ; Utility function to find minimum of two numbers ; time taken to leave first station in line 1 ; time taken to leave first station in line 2 ; Fill tables T1 [ ] and T2 [ ] using the above given recursive relations ; Consider exit times and retutn minimum ; Driver code
using System ; class GFG { static int NUM_STATION = 4 ; static int min ( int a , int b ) { return a < b ? a : b ; } static int carAssembly ( int [ , ] a , int [ , ] t , int [ ] e , int [ ] x ) { int [ ] T1 = new int [ NUM_STATION ] ; int [ ] T2 = new int [ NUM_STATION ] ; int i ; T1 [ 0 ] = e [ 0 ] + a [ 0 , 0 ] ; T2 [ 0 ] = e [ 1 ] + a [ 1 , 0 ] ; for ( i = 1 ; i < NUM_STATION ; ++ i ) { T1 [ i ] = min ( T1 [ i - 1 ] + a [ 0 , i ] , T2 [ i - 1 ] + t [ 1 , i ] + a [ 0 , i ] ) ; T2 [ i ] = min ( T2 [ i - 1 ] + a [ 1 , i ] , T1 [ i - 1 ] + t [ 0 , i ] + a [ 1 , i ] ) ; } return min ( T1 [ NUM_STATION - 1 ] + x [ 0 ] , T2 [ NUM_STATION - 1 ] + x [ 1 ] ) ; } public static void Main ( ) { int [ , ] a = { { 4 , 5 , 3 , 2 } , { 2 , 10 , 1 , 4 } } ; int [ , ] t = { { 0 , 7 , 4 , 5 } , { 0 , 9 , 2 , 8 } } ; int [ ] e = { 10 , 12 } ; int [ ] x = { 18 , 7 } ; Console . Write ( carAssembly ( a , t , e , x ) ) ; } }
Longest Common Substring | DP | C # implementation of finding length of longest Common substring using Dynamic Programming ; Returns length of longest common substring of X [ 0. . m - 1 ] and Y [ 0. . n - 1 ] ; Create a table to store lengths of longest common suffixes of substrings . Note that LCSuff [ i ] [ j ] contains length of longest common suffix of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] . The first row and first column entries have no logical meaning , they are used only for simplicity of program ; To store length of the longest common substring ; Following steps build LCSuff [ m + 1 ] [ n + 1 ] in bottom up fashion ; Driver Code
using System ; class GFG { static int LCSubStr ( string X , string Y , int m , int n ) { int [ , ] LCStuff = new int [ m + 1 , n + 1 ] ; int result = 0 ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) LCStuff [ i , j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) { LCStuff [ i , j ] = LCStuff [ i - 1 , j - 1 ] + 1 ; result = Math . Max ( result , LCStuff [ i , j ] ) ; } else LCStuff [ i , j ] = 0 ; } } return result ; } public static void Main ( ) { String X = " OldSite : GeeksforGeeks . org " ; String Y = " NewSite : GeeksQuiz . com " ; int m = X . Length ; int n = Y . Length ; Console . Write ( " Length ▁ of ▁ Longest ▁ Common " + " ▁ Substring ▁ is ▁ " + LCSubStr ( X , Y , m , n ) ) ; } }