codeparrot/xlcost-text-to-code huggingface dataset:
Minimize cost to reduce array to a single element by replacing K consecutive elements by their sum | Function to find the minimum cost to reduce given array to a single element by replacing consecutive K array elements ; Stores length of arr ; If ( N - 1 ) is not multiple of ( K - 1 ) ; Store prefix sum of the array ; Iterate over the range [ 1 , N ] ; Update prefixSum [ i ] ; dp [ i ] [ j ] : Store minimum cost to merge array elements interval [ i , j ] ; L : Stores length of interval [ i , j ] ; Iterate over each interval [ i , j ] of length L in in [ 0 , N ] ; Stores index of last element of the interval [ i , j ] ; If L is greater than K ; Update dp [ i ] [ j ] ; If ( L - 1 ) is multiple of ( K - 1 ) ; Update dp [ i ] [ j ] ; Return dp [ 0 ] [ N - 1 ] ; Driver Code ; Function Call
def minimumCostToMergeK ( arr , K ) : NEW_LINE INDENT N = len ( arr ) NEW_LINE if ( N - 1 ) % ( K - 1 ) != 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT prefixSum = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT prefixSum [ i ] = ( prefixSum [ i - 1 ] + arr [ i - 1 ] ) NEW_LINE DEDENT dp = [ [ 0 ] * N for _ in range ( N ) ] NEW_LINE for L in range ( K , N + 1 ) : NEW_LINE INDENT for i in range ( N - L + 1 ) : NEW_LINE INDENT j = i + L - 1 NEW_LINE if L > K : NEW_LINE INDENT dp [ i ] [ j ] = ( min ( [ dp [ i ] [ x ] + dp [ x + 1 ] [ j ] for x in range ( i , j , K - 1 ) ] ) ) NEW_LINE DEDENT if ( L - 1 ) % ( K - 1 ) == 0 : NEW_LINE INDENT dp [ i ] [ j ] += ( prefixSum [ j + 1 ] - prefixSum [ i ] ) NEW_LINE DEDENT DEDENT DEDENT return dp [ 0 ] [ N - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 5 , 1 , 2 , 6 ] NEW_LINE K = 3 NEW_LINE DEDENT print ( minimumCostToMergeK ( arr , K ) ) NEW_LINE
Check if end of a sorted Array can be reached by repeated jumps of one more , one less or same number of indices as previous jump | Python3 program for the above approach ; Utility function to check if it is possible to move from index 1 to N - 1 ; Successfully reached end index ; memo [ i ] [ j ] is already calculated ; Check if there is any index having value of A [ i ] + j - 1 , A [ i ] + j or A [ i ] + j + 1 ; If A [ k ] > A [ i ] + j + 1 , can 't make a move further ; It 's possible to move A[k] ; Check is it possible to move from index k having previously taken A [ k ] - A [ i ] steps ; If yes then break the loop ; Store value of flag in memo ; Return memo [ i ] [ j ] ; Function to check if it is possible to move from index 1 to N - 1 ; Stores the memoized state ; Initialize all values as - 1 ; Initially , starting index = 1 ; Function call ; Driver Code ; Function Call
N = 8 NEW_LINE def check ( memo , i , j , A ) : NEW_LINE INDENT if ( i == N - 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( memo [ i ] [ j ] != - 1 ) : NEW_LINE INDENT return memo [ i ] [ j ] NEW_LINE DEDENT flag = 0 NEW_LINE for k in range ( i + 1 , N ) : NEW_LINE INDENT if ( A [ k ] - A [ i ] > j + 1 ) : NEW_LINE break NEW_LINE if ( A [ k ] - A [ i ] >= j - 1 and A [ k ] - A [ i ] <= j + 1 ) : NEW_LINE flag = check ( memo , k , A [ k ] - A [ i ] , A ) NEW_LINE if ( flag != 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT memo [ i ] [ j ] = flag NEW_LINE return memo [ i ] [ j ] NEW_LINE DEDENT def checkEndReach ( A , K ) : NEW_LINE INDENT memo = [ [ 0 ] * N ] * N NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , N ) : NEW_LINE memo [ i ] [ j ] = - 1 NEW_LINE DEDENT startIndex = 1 NEW_LINE if ( check ( memo , startIndex , K , A ) != 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 0 , 1 , 3 , 5 , 6 , 8 , 12 , 17 ] NEW_LINE K = 1 NEW_LINE checkEndReach ( A , K ) NEW_LINE DEDENT
Maximum subsequence sum such that no K elements are consecutive | Function to find the maximum sum of a subsequence consisting of no K consecutive array elements ; Stores states of dp ; Stores the prefix sum ; Update the prefix sum ; Base case for i < K ; For indices less than k take all the elements ; For i >= K case ; Skip each element from i to ( i - K + 1 ) to ensure that no K elements are consecutive ; Update the current dp state ; dp [ N ] stores the maximum sum ; Driver Code ; Given array arr [ ] ; Function call
def Max_Sum ( arr , K , N ) : NEW_LINE INDENT dp = [ 0 ] * ( N + 1 ) NEW_LINE prefix = [ None ] * ( N + 1 ) NEW_LINE prefix [ 0 ] = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT prefix [ i ] = prefix [ i - 1 ] + arr [ i - 1 ] NEW_LINE DEDENT dp [ 0 ] = 0 NEW_LINE for i in range ( 1 , K ) : NEW_LINE INDENT dp [ i ] = prefix [ i ] NEW_LINE DEDENT for i in range ( K , N + 1 ) : NEW_LINE INDENT for j in range ( i , i - K , - 1 ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , dp [ j - 1 ] + prefix [ i ] - prefix [ j ] ) NEW_LINE DEDENT DEDENT return dp [ N ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 12 , 22 , 18 , 34 , 12 , 25 ] NEW_LINE N = len ( arr ) NEW_LINE K = 5 NEW_LINE print ( Max_Sum ( arr , K , N ) ) NEW_LINE DEDENT
Count possible splits of sum N into K integers such that the minimum is at least P | Function that finds the value of the Binomial Coefficient C ( n , k ) ; Stores the value of Binomial Coefficient in bottom up manner ; Base Case ; Find the value using previously stored values ; Return the value of C ( N , K ) ; Function that count the number of ways to divide N into K integers >= P such that their sum is N ; Update the value of N ; Find the binomial coefficient recursively ; Given K , N , and P
def binomialCoeff ( n , k ) : NEW_LINE INDENT C = [ [ 0 for x in range ( k + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , k ) + 1 ) : NEW_LINE INDENT if ( j == 0 or j == i ) : NEW_LINE INDENT C [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT return C [ n ] [ k ] NEW_LINE DEDENT def waysToSplitN ( k , n , P ) : NEW_LINE INDENT new_N = n - k * P NEW_LINE return binomialCoeff ( new_N + k - 1 , new_N ) NEW_LINE DEDENT K = 3 NEW_LINE N = 8 NEW_LINE P = 2 NEW_LINE print ( waysToSplitN ( K , N , P ) ) NEW_LINE
Minimum pair merge operations required to make Array non | Function to find the minimum operations to make the array Non - increasing ; Size of the array ; Dp table initialization ; dp [ i ] : Stores minimum number of operations required to make subarray { A [ i ] , ... , A [ N ] } non - increasing ; Increment the value of j ; Add current value to sum ; Update the dp tables ; Return the answer ; Driver Code
def solve ( a ) : NEW_LINE INDENT n = len ( a ) NEW_LINE dp = [ 0 ] * ( n + 1 ) NEW_LINE val = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT sum = a [ i ] NEW_LINE j = i NEW_LINE while ( j + 1 < n and sum < val [ j + 1 ] ) : NEW_LINE INDENT j += 1 NEW_LINE sum += a [ j ] NEW_LINE DEDENT dp [ i ] = ( j - i ) + dp [ j + 1 ] NEW_LINE val [ i ] = sum NEW_LINE DEDENT return dp [ 0 ] NEW_LINE DEDENT arr = [ 1 , 5 , 3 , 9 , 1 ] NEW_LINE print ( solve ( arr ) ) NEW_LINE
Count full nodes in a Binary tree ( Iterative and Recursive ) | A node structure ; Iterative Method to count full nodes of binary tree ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root and initialize count ; initialize count for full nodes ; Enqueue left child ; Enqueue right child ; 2 / \ 7 5 \ \ 6 9 / \ / 1 11 4 Let us create Binary Tree as shown
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def getfullCount ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT queue = [ ] NEW_LINE queue . append ( root ) NEW_LINE count = 0 NEW_LINE while ( len ( queue ) > 0 ) : NEW_LINE INDENT node = queue . pop ( 0 ) NEW_LINE if node . left is not None and node . right is not None : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT if node . left is not None : NEW_LINE INDENT queue . append ( node . left ) NEW_LINE DEDENT if node . right is not None : NEW_LINE INDENT queue . append ( node . right ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT root = Node ( 2 ) NEW_LINE root . left = Node ( 7 ) NEW_LINE root . right = Node ( 5 ) NEW_LINE root . left . right = Node ( 6 ) NEW_LINE root . left . right . left = Node ( 1 ) NEW_LINE root . left . right . right = Node ( 11 ) NEW_LINE root . right . right = Node ( 9 ) NEW_LINE root . right . right . left = Node ( 4 ) NEW_LINE print " % d " % ( getfullCount ( root ) ) NEW_LINE
Count of Distinct Substrings occurring consecutively in a given String | Function to count the distinct substrings placed consecutively in the given string ; Length of the string ; If length of the string does not exceed 1 ; Initialize a DP - table ; Stores the distinct substring ; Iterate from end of the string ; Iterate backward until dp table is all computed ; If character at i - th index is same as character at j - th index ; Update dp [ i ] [ j ] based on previously computed value ; Otherwise ; Condition for consecutively placed similar substring ; Return the count ; Driver Code
def distinctSimilarSubstrings ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE if ( n <= 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT dp = [ [ 0 for x in range ( n + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE substrings = set ( ) NEW_LINE for j in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT for i in range ( j - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( str [ i ] == str [ j ] ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i + 1 ] [ j + 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = 0 NEW_LINE DEDENT if ( dp [ i ] [ j ] >= j - i ) : NEW_LINE INDENT substrings . add ( str [ i : j - i ] ) NEW_LINE DEDENT DEDENT DEDENT return len ( substrings ) NEW_LINE DEDENT str = " geeksgeeksforgeeks " NEW_LINE print ( distinctSimilarSubstrings ( str ) ) NEW_LINE
Finding shortest path between any two nodes using Floyd Warshall Algorithm | Initializing the distance and Next array ; No edge between node i and j ; Function construct the shortest path between u and v ; If there 's no path between  node u and v, simply return  an empty array ; Storing the path in a vector ; Standard Floyd Warshall Algorithm with little modification Now if we find that dis [ i ] [ j ] > dis [ i ] [ k ] + dis [ k ] [ j ] then we modify next [ i ] [ j ] = next [ i ] [ k ] ; We cannot travel through edge that doesn 't exist ; Print the shortest path ; Driver code ; Function to initialise the distance and Next array ; Calling Floyd Warshall Algorithm , this will update the shortest distance as well as Next array ; Path from node 1 to 3 ; Path from node 0 to 2 ; Path from node 3 to 2
def initialise ( V ) : NEW_LINE INDENT global dis , Next NEW_LINE for i in range ( V ) : NEW_LINE INDENT for j in range ( V ) : NEW_LINE INDENT dis [ i ] [ j ] = graph [ i ] [ j ] NEW_LINE if ( graph [ i ] [ j ] == INF ) : NEW_LINE INDENT Next [ i ] [ j ] = - 1 NEW_LINE DEDENT else : NEW_LINE INDENT Next [ i ] [ j ] = j NEW_LINE DEDENT DEDENT DEDENT DEDENT def constructPath ( u , v ) : NEW_LINE INDENT global graph , Next NEW_LINE if ( Next [ u ] [ v ] == - 1 ) : NEW_LINE INDENT return { } NEW_LINE DEDENT path = [ u ] NEW_LINE while ( u != v ) : NEW_LINE INDENT u = Next [ u ] [ v ] NEW_LINE path . append ( u ) NEW_LINE DEDENT return path NEW_LINE DEDENT def floydWarshall ( V ) : NEW_LINE INDENT global dist , Next NEW_LINE for k in range ( V ) : NEW_LINE INDENT for i in range ( V ) : NEW_LINE INDENT for j in range ( V ) : NEW_LINE INDENT if ( dis [ i ] [ k ] == INF or dis [ k ] [ j ] == INF ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( dis [ i ] [ j ] > dis [ i ] [ k ] + dis [ k ] [ j ] ) : NEW_LINE INDENT dis [ i ] [ j ] = dis [ i ] [ k ] + dis [ k ] [ j ] NEW_LINE Next [ i ] [ j ] = Next [ i ] [ k ] NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def printPath ( path ) : NEW_LINE INDENT n = len ( path ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT print ( path [ i ] , end = " ▁ - > ▁ " ) NEW_LINE DEDENT print ( path [ n - 1 ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT MAXM , INF = 100 , 10 ** 7 NEW_LINE dis = [ [ - 1 for i in range ( MAXM ) ] for i in range ( MAXM ) ] NEW_LINE Next = [ [ - 1 for i in range ( MAXM ) ] for i in range ( MAXM ) ] NEW_LINE V = 4 NEW_LINE graph = [ [ 0 , 3 , INF , 7 ] , [ 8 , 0 , 2 , INF ] , [ 5 , INF , 0 , 1 ] , [ 2 , INF , INF , 0 ] ] NEW_LINE initialise ( V ) NEW_LINE floydWarshall ( V ) NEW_LINE path = [ ] NEW_LINE print ( " Shortest ▁ path ▁ from ▁ 1 ▁ to ▁ 3 : ▁ " , end = " " ) NEW_LINE path = constructPath ( 1 , 3 ) NEW_LINE printPath ( path ) NEW_LINE print ( " Shortest ▁ path ▁ from ▁ 0 ▁ to ▁ 2 : ▁ " , end = " " ) NEW_LINE path = constructPath ( 0 , 2 ) NEW_LINE printPath ( path ) NEW_LINE print ( " Shortest ▁ path ▁ from ▁ 3 ▁ to ▁ 2 : ▁ " , end = " " ) NEW_LINE path = constructPath ( 3 , 2 ) NEW_LINE printPath ( path ) NEW_LINE DEDENT
Count sequences of length K having each term divisible by its preceding term | Stores the factors of i - th element in v [ i ] ; Function to find all the factors of N ; Iterate upto sqrt ( N ) ; Function to return the count of sequences of length K having all terms divisible by its preceding term ; Calculate factors of i ; Initialize dp [ 0 ] [ i ] = 0 : No subsequence of length 0 ending with i - th element exists ; Initialize dp [ 0 ] [ i ] = 1 : Only 1 subsequence of length 1 ending with i - th element exists ; Iterate [ 2 , K ] to obtain sequences of each length ; Calculate sum of all dp [ i - 1 ] [ vp [ j ] [ k ] ] ; vp [ j ] [ k ] stores all factors of j ; Store the sum in A [ i ] [ j ] ; Sum of all dp [ K ] [ j ] obtain all K length sequences ending with j ; Driver code
vp = [ [ ] for i in range ( 2009 ) ] NEW_LINE def finding_factors ( n ) : NEW_LINE INDENT i = 1 NEW_LINE a = 0 NEW_LINE global vp NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( i * i == n ) : NEW_LINE INDENT vp [ n ] . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT vp [ n ] . append ( i ) NEW_LINE vp [ n ] . append ( int ( n / i ) ) NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT def countSeq ( N , K ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE k = 0 NEW_LINE dp = [ [ 0 for i in range ( 109 ) ] for j in range ( 109 ) ] NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT finding_factors ( i ) NEW_LINE dp [ 0 ] [ i ] = 0 NEW_LINE dp [ 1 ] [ i ] = 1 NEW_LINE DEDENT for i in range ( 2 , K + 1 ) : NEW_LINE INDENT for j in range ( 1 , N + 1 ) : NEW_LINE INDENT Sum = 0 NEW_LINE for k in range ( len ( vp [ j ] ) ) : NEW_LINE INDENT Sum += dp [ i - 1 ] [ vp [ j ] [ k ] ] NEW_LINE DEDENT dp [ i ] [ j ] = Sum NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for j in range ( 1 , N + 1 ) : NEW_LINE INDENT ans += dp [ K ] [ j ] NEW_LINE DEDENT return ans NEW_LINE DEDENT N = 3 NEW_LINE K = 2 NEW_LINE print ( countSeq ( N , K ) ) NEW_LINE
Largest possible square submatrix with maximum AND value | Function to calculate and return the length of square submatrix with maximum AND value ; Extract dimensions ; Auxiliary array Initialize auxiliary array ; c : Stores the maximum value in the matrix p : Stores the number of elements in the submatrix having maximum AND value ; Iterate over the matrix to fill the auxiliary matrix ; Find the max element in the matrix side by side ; Fill first row and column with 1 's ; For every cell , check if the elements at the left , top and top left cells from the current cell are equal or not ; Store the minimum possible submatrix size these elements are part of ; Store 1 otherwise ; Checking maximum value ; If the maximum AND value occurs more than once ; Update the maximum size of submatrix ; Final output ; Driver Code
def MAX_value ( arr ) : NEW_LINE INDENT row = len ( arr ) NEW_LINE col = len ( arr ) NEW_LINE dp = [ [ 0 for i in range ( col ) ] for j in range ( row ) ] NEW_LINE i , j = 0 , 0 NEW_LINE c , p = arr [ 0 ] [ 0 ] , 0 NEW_LINE d = row NEW_LINE for i in range ( d ) : NEW_LINE INDENT for j in range ( d ) : NEW_LINE INDENT if ( c < arr [ i ] [ j ] ) : NEW_LINE INDENT c = arr [ i ] [ j ] NEW_LINE DEDENT if ( i == 0 or j == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( arr [ i - 1 ] [ j - 1 ] == arr [ i ] [ j ] and arr [ i - 1 ] [ j ] == arr [ i ] [ j ] and arr [ i ] [ j - 1 ] == arr [ i ] [ j ] ) : NEW_LINE INDENT dp [ i ] [ j ] = min ( dp [ i - 1 ] [ j - 1 ] , min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ) + 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT for i in range ( d ) : NEW_LINE INDENT for j in range ( d ) : NEW_LINE INDENT if ( arr [ i ] [ j ] == c ) : NEW_LINE INDENT if ( p < dp [ i ] [ j ] ) : NEW_LINE INDENT p = dp [ i ] [ j ] NEW_LINE DEDENT DEDENT DEDENT DEDENT return p * p NEW_LINE DEDENT arr = [ [ 9 , 9 , 3 , 3 , 4 , 4 ] , [ 9 , 9 , 7 , 7 , 7 , 4 ] , [ 1 , 2 , 7 , 7 , 7 , 4 ] , [ 4 , 4 , 7 , 7 , 7 , 4 ] , [ 5 , 5 , 1 , 1 , 2 , 7 ] , [ 2 , 7 , 1 , 1 , 4 , 4 ] ] NEW_LINE print ( MAX_value ( arr ) ) NEW_LINE
Smallest power of 2 consisting of N digits | Python3 program to implement the above approach ; Function to return smallest power of 2 with N digits ; Iterate through all powers of 2 ; Driver Code
from math import log10 , log NEW_LINE def smallestNum ( n ) : NEW_LINE INDENT res = 1 NEW_LINE i = 2 NEW_LINE while ( True ) : NEW_LINE INDENT length = int ( log10 ( i ) + 1 ) NEW_LINE if ( length == n ) : NEW_LINE INDENT return int ( log ( i ) // log ( 2 ) ) NEW_LINE DEDENT i *= 2 NEW_LINE DEDENT DEDENT n = 4 NEW_LINE print ( smallestNum ( n ) ) NEW_LINE
Count full nodes in a Binary tree ( Iterative and Recursive ) | A binary tree Node has data , pointer to left child and a pointer to right child ; Function to get the count of full Nodes in a binary tree ; Driver code ; 2 / \ 7 5 \ \ 6 9 / \ / 1 11 4 Let us create Binary Tree as shown
class newNode ( ) : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def getfullCount ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT res = 0 NEW_LINE if ( root . left and root . right ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT res += ( getfullCount ( root . left ) + getfullCount ( root . right ) ) NEW_LINE return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 2 ) NEW_LINE root . left = newNode ( 7 ) NEW_LINE root . right = newNode ( 5 ) NEW_LINE root . left . right = newNode ( 6 ) NEW_LINE root . left . right . left = newNode ( 1 ) NEW_LINE root . left . right . right = newNode ( 11 ) NEW_LINE root . right . right = newNode ( 9 ) NEW_LINE root . right . right . left = newNode ( 4 ) NEW_LINE print ( getfullCount ( root ) ) NEW_LINE DEDENT
Count Possible Decodings of a given Digit Sequence in O ( N ) time and Constant Auxiliary space | A Dynamic programming based function to count decodings in digit sequence ; For base condition "01123" should return 0 ; Using last two calculated values , calculate for ith index ; Change boolean to int ; Return the required answer ; Driver Code ; Function call
def countDecodingDP ( digits , n ) : NEW_LINE INDENT if ( digits [ 0 ] == '0' ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT count0 = 1 ; count1 = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT dig1 = 0 ; dig3 = 0 ; NEW_LINE if ( digits [ i - 1 ] != '0' ) : NEW_LINE INDENT dig1 = 1 ; NEW_LINE DEDENT if ( digits [ i - 2 ] == '1' ) : NEW_LINE INDENT dig2 = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT dig2 = 0 ; NEW_LINE DEDENT if ( digits [ i - 2 ] == '2' and digits [ i - 1 ] < '7' ) : NEW_LINE INDENT dig3 = 1 ; NEW_LINE DEDENT count2 = dig1 * count1 + NEW_LINE INDENT dig2 + dig3 * count0 ; NEW_LINE DEDENT count0 = count1 ; NEW_LINE count1 = count2 ; NEW_LINE DEDENT return count1 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT digits = "1234" ; NEW_LINE n = len ( digits ) ; NEW_LINE print ( countDecodingDP ( digits , n ) ) ; NEW_LINE DEDENT
Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix | Function to find the maximum distance ; Set up top row and left column ; Pass one : top left to bottom right ; Check if there was no " One " Cell ; Set up top row and left column ; Past two : bottom right to top left ; Driver code
def maxDistance ( grid ) : NEW_LINE INDENT if ( len ( grid ) == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT N = len ( grid ) NEW_LINE INF = 1000000 NEW_LINE if grid [ 0 ] [ 0 ] == 1 : NEW_LINE INDENT grid [ 0 ] [ 0 ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT grid [ 0 ] [ 0 ] = INF NEW_LINE DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT if grid [ 0 ] [ i ] == 1 : NEW_LINE INDENT grid [ 0 ] [ i ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT grid [ 0 ] [ i ] = grid [ 0 ] [ i - 1 ] + 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT if grid [ i ] [ 0 ] == 1 : NEW_LINE INDENT grid [ i ] [ 0 ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT grid [ i ] [ 0 ] = grid [ i - 1 ] [ 0 ] + 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( 1 , N ) : NEW_LINE INDENT if grid [ i ] [ j ] == 1 : NEW_LINE INDENT grid [ i ] [ j ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT grid [ i ] [ j ] = min ( grid [ i - 1 ] [ j ] , grid [ i ] [ j - 1 ] + 1 ) NEW_LINE DEDENT DEDENT DEDENT if ( grid [ N - 1 ] [ N - 1 ] >= INF ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT maxi = grid [ N - 1 ] [ N - 1 ] NEW_LINE for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT grid [ N - 1 ] [ i ] = min ( grid [ N - 1 ] [ i ] , grid [ N - 1 ] [ i + 1 ] + 1 ) NEW_LINE maxi = max ( grid [ N - 1 ] [ i ] , maxi ) NEW_LINE DEDENT for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT grid [ i ] [ N - 1 ] = min ( grid [ i ] [ N - 1 ] , grid [ i + 1 ] [ N - 1 ] + 1 ) NEW_LINE maxi = max ( grid [ i ] [ N - 1 ] , maxi + 1 ) NEW_LINE DEDENT for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT grid [ i ] [ j ] = min ( grid [ i ] [ j ] , min ( grid [ i + 1 ] [ j ] + 1 , grid [ i ] [ j + 1 ] + 1 ) ) NEW_LINE maxi = max ( grid [ i ] [ j ] , maxi ) NEW_LINE DEDENT DEDENT if maxi == 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return maxi NEW_LINE DEDENT DEDENT arr = [ [ 0 , 0 , 1 ] , [ 0 , 0 , 0 ] , [ 0 , 0 , 0 ] ] NEW_LINE print ( maxDistance ( arr ) ) NEW_LINE
Minimize total cost without repeating same task in two consecutive iterations | Function to return the minimum cost for N iterations ; Construct the dp table ; 1 st row of dp table will be equal to the 1 st of cost matrix ; Iterate through all the rows ; To iterate through the columns of current row ; Initialize val as infinity ; To iterate through the columns of previous row ; Fill the dp matrix ; Returning the minimum value ; Driver Code ; Number of iterations ; Number of tasks ; Cost matrix
def findCost ( cost_mat , N , M ) : NEW_LINE INDENT dp = [ [ 0 ] * M for _ in range ( M ) ] NEW_LINE dp [ 0 ] = cost_mat [ 0 ] NEW_LINE for row in range ( 1 , N ) : NEW_LINE INDENT for curr_col in range ( M ) : NEW_LINE INDENT val = 999999999 NEW_LINE for prev_col in range ( M ) : NEW_LINE INDENT if curr_col != prev_col : NEW_LINE INDENT val = min ( val , dp [ row - 1 ] [ prev_col ] ) NEW_LINE DEDENT DEDENT dp [ row ] [ curr_col ] = val + cost_mat [ row ] [ curr_col ] NEW_LINE DEDENT DEDENT return min ( dp [ - 1 ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE M = 4 NEW_LINE cost_mat = [ [ 4 , 5 , 3 , 2 ] , [ 6 , 2 , 8 , 1 ] , [ 6 , 2 , 2 , 1 ] , [ 0 , 5 , 5 , 1 ] ] NEW_LINE print ( findCost ( cost_mat , N , M ) ) NEW_LINE DEDENT