codeparrot/xlcost-text-to-code huggingface dataset:
Remove all nodes which don 't lie in any path with sum>= k | A utility function to create a new Binary Tree node with given data ; print the tree in LVR ( Inorder traversal ) way . ; Main function which truncates the binary tree . ; Base Case ; Initialize left and right Sums as Sum from root to this node ( including this node ) ; Recursively prune left and right subtrees ; Get the maximum of left and right Sums ; If maximum is smaller than k , then this node must be deleted ; A wrapper over pruneUtil ( ) ; Driver Code ; k is 45
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def Print ( root ) : NEW_LINE INDENT if ( root != None ) : NEW_LINE INDENT Print ( root . left ) NEW_LINE print ( root . data , end = " ▁ " ) NEW_LINE Print ( root . right ) NEW_LINE DEDENT DEDENT def pruneUtil ( root , k , Sum ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT lSum = [ Sum [ 0 ] + ( root . data ) ] NEW_LINE rSum = [ lSum [ 0 ] ] NEW_LINE root . left = pruneUtil ( root . left , k , lSum ) NEW_LINE root . right = pruneUtil ( root . right , k , rSum ) NEW_LINE Sum [ 0 ] = max ( lSum [ 0 ] , rSum [ 0 ] ) NEW_LINE if ( Sum [ 0 ] < k [ 0 ] ) : NEW_LINE INDENT root = None NEW_LINE DEDENT return root NEW_LINE DEDENT def prune ( root , k ) : NEW_LINE INDENT Sum = [ 0 ] NEW_LINE return pruneUtil ( root , k , Sum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT k = [ 45 ] NEW_LINE root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . right . left = newNode ( 6 ) NEW_LINE root . right . right = newNode ( 7 ) NEW_LINE root . left . left . left = newNode ( 8 ) NEW_LINE root . left . left . right = newNode ( 9 ) NEW_LINE root . left . right . left = newNode ( 12 ) NEW_LINE root . right . right . left = newNode ( 10 ) NEW_LINE root . right . right . left . right = newNode ( 11 ) NEW_LINE root . left . left . right . left = newNode ( 13 ) NEW_LINE root . left . left . right . right = newNode ( 14 ) NEW_LINE root . left . left . right . right . left = newNode ( 15 ) NEW_LINE print ( " Tree ▁ before ▁ truncation " ) NEW_LINE Print ( root ) NEW_LINE print ( ) NEW_LINE root = prune ( root , k ) NEW_LINE print ( " Tree ▁ after ▁ truncation " ) NEW_LINE Print ( root ) NEW_LINE DEDENT
Tree Traversals ( Inorder , Preorder and Postorder ) | A class that represents an individual node in a Binary Tree ; A function to do postorder tree traversal ; First recur on left child ; the recur on right child ; now print the data of node ; A function to do inorder tree traversal ; First recur on left child ; then print the data of node ; now recur on right child ; A function to do preorder tree traversal ; First print the data of node ; Then recur on left child ; Finally recur on right child ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . left = None NEW_LINE self . right = None NEW_LINE self . val = key NEW_LINE DEDENT DEDENT def printPostorder ( root ) : NEW_LINE INDENT if root : NEW_LINE INDENT printPostorder ( root . left ) NEW_LINE printPostorder ( root . right ) NEW_LINE print ( root . val ) , NEW_LINE DEDENT DEDENT def printInorder ( root ) : NEW_LINE INDENT if root : NEW_LINE INDENT printInorder ( root . left ) NEW_LINE print ( root . val ) , NEW_LINE printInorder ( root . right ) NEW_LINE DEDENT DEDENT def printPreorder ( root ) : NEW_LINE INDENT if root : NEW_LINE INDENT print ( root . val ) , NEW_LINE printPreorder ( root . left ) NEW_LINE printPreorder ( root . right ) NEW_LINE DEDENT DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE print " Preorder ▁ traversal ▁ of ▁ binary ▁ tree ▁ is " NEW_LINE printPreorder ( root ) NEW_LINE print   " NEW_LINE Inorder traversal of binary tree is " NEW_LINE printInorder ( root ) NEW_LINE print   " NEW_LINE Postorder traversal of binary tree is " NEW_LINE printPostorder ( root ) NEW_LINE
Maximum subarray sum possible after removing at most K array elements | Python3 program to implement the above approach ; Function to find the maximum subarray sum greater than or equal to 0 by removing K array elements ; Base case ; If overlapping subproblems already occurred ; Include current element in the subarray ; If K elements already removed from the subarray ; Remove current element from the subarray ; Utility function to find the maximum subarray sum by removing at most K array elements ; Stores maximum subarray sum by removing at most K elements ; Calculate maximum element in dp [ ] [ ] ; Update res ; If all array elements are negative ; Update res ; Driver Code
M = 100 NEW_LINE def mxSubSum ( i , arr , j ) : NEW_LINE INDENT global dp NEW_LINE if ( i == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( 0 , arr [ i ] ) NEW_LINE return dp [ i ] [ j ] NEW_LINE DEDENT if ( dp [ i ] [ j ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ j ] NEW_LINE DEDENT X = max ( 0 , arr [ i ] + mxSubSum ( i - 1 , arr , j ) ) NEW_LINE if ( j == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = X NEW_LINE return X NEW_LINE DEDENT Y = mxSubSum ( i - 1 , arr , j - 1 ) NEW_LINE dp [ i ] [ j ] = max ( X , Y ) NEW_LINE return dp [ i ] [ j ] NEW_LINE DEDENT def MaximumSubarraySum ( n , arr , k ) : NEW_LINE INDENT mxSubSum ( n - 1 , arr , k ) NEW_LINE res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( k + 1 ) : NEW_LINE INDENT res = max ( res , dp [ i ] [ j ] ) NEW_LINE DEDENT DEDENT if ( max ( arr ) < 0 ) : NEW_LINE INDENT res = max ( arr ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT dp = [ [ - 1 for i in range ( 100 ) ] for i in range ( 100 ) ] NEW_LINE arr = [ - 2 , 1 , 3 , - 2 , 4 , - 7 , 20 ] NEW_LINE K = 1 NEW_LINE N = len ( arr ) NEW_LINE print ( MaximumSubarraySum ( N , arr , K ) ) NEW_LINE DEDENT
Minimum number of operations required to make all elements of at least one row of given Matrix prime | Python 3 program to implement the above approach ; Function to generate all prime numbers using Sieve of Eratosthenes ; Function to check if a number is prime or not ; prime [ i ] : Check if i is a prime number or not ; Iterate over the range [ 2 , sqrt ( n ) ] ; If p is a prime number ; Mark all multiples of i to false ; Update i ; Function to find minimum operations to make all elements of at least one row of the matrix as prime numbers ; dp [ i ] : Stores minimum operations to get i prime numbers in a row ; Traverse the array ; Stores count of prime numbers in a i - th row ; Iterate over the range [ ( 1 << m ) - 1 , 0 ] ; If a row exist which contains j prime numbers ; Update dp [ j bitmask ] ; Update dp [ bitmask ] ; Return minimum operations to get a row of the matrix with all prime numbers ; Function to count prime numbers in a row ; i - th bit of bitmask check if i - th column is a prime or not ; Traverse the array ; if a [ i ] is a prime number ; Update bitmask ; Driver Code ; Stores count of columns in the matrix ; Stores length ; Calculate all prime numbers in range [ 1 , max ] using sieve ; Function Call
from math import sqrt NEW_LINE prime = [ True for i in range ( 10001 ) ] NEW_LINE def sieve ( n ) : NEW_LINE INDENT global prime NEW_LINE for p in range ( 2 , int ( sqrt ( 10000 ) ) + 1 , 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , 10001 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def MinWays ( a , n , m ) : NEW_LINE INDENT dp = [ n + 1 for i in range ( 1 << m ) ] NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT bitmask = BitMask ( a [ i ] ) NEW_LINE j = ( 1 << m ) - 1 NEW_LINE while ( j >= 0 ) : NEW_LINE INDENT if ( dp [ j ] != n + 1 ) : NEW_LINE INDENT dp [ j bitmask ] = min ( dp [ j bitmask ] , dp [ j ] + 1 ) NEW_LINE DEDENT j -= 1 NEW_LINE DEDENT dp [ bitmask ] = 1 NEW_LINE DEDENT if ( dp [ ( 1 << m ) - 1 ] - 1 ) == ( n + 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return ( dp [ ( 1 << m ) - 1 ] - 1 ) NEW_LINE DEDENT DEDENT def BitMask ( a ) : NEW_LINE INDENT global prime NEW_LINE bitmask = 0 NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT if ( prime [ a [ i ] ] ) : NEW_LINE INDENT bitmask |= ( 1 << i ) NEW_LINE DEDENT DEDENT return bitmask NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 4 , 6 , 5 , 8 ] , [ 2 , 9 , 12 , 14 ] , [ 32 , 7 , 18 , 16 ] , [ 12 , 4 , 35 , 17 ] ] NEW_LINE m = len ( mat [ 0 ] ) NEW_LINE n = len ( mat ) NEW_LINE max = 10000 NEW_LINE sieve ( max ) NEW_LINE print ( MinWays ( mat , n , m ) ) NEW_LINE DEDENT
Minimize cost to empty given array where cost of removing an element is its absolute difference with Time instant | Python3 program for the above approach ; Function to find the minimum cost to delete all array elements ; Sort the input array ; Store the maximum time to delete the array in the worst case ; Store the result in cost [ ] [ ] table ; Base Case ; Store the minimum of all cost values of the previous index ; Iterate from range [ 1 , n ] using variable i ; Update prev ; Iterate from range [ 1 , m ] using variable j ; Update cost [ i ] [ j ] ; Update the prev ; Store the minimum cost to delete all elements ; Find the minimum of all values of cost [ n ] [ j ] ; Print minimum cost ; Driver Code ; Function Call
INF = 10000 NEW_LINE def minCost ( arr , n ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE m = 2 * n NEW_LINE cost = [ [ INF for i in range ( m + 1 ) ] for i in range ( n + 1 ) ] NEW_LINE cost [ 0 ] [ 0 ] = 0 NEW_LINE prev = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT prev = cost [ i - 1 ] [ 0 ] NEW_LINE for j in range ( 1 , m + 1 ) : NEW_LINE INDENT cost [ i ] [ j ] = min ( cost [ i ] [ j ] , prev + abs ( j - arr [ i - 1 ] ) ) NEW_LINE prev = min ( prev , cost [ i - 1 ] [ j ] ) NEW_LINE DEDENT DEDENT minCost = INF NEW_LINE for j in range ( 1 , m + 1 ) : NEW_LINE INDENT minCost = min ( minCost , cost [ n ] [ j ] ) NEW_LINE DEDENT print ( minCost ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 2 , 4 , 4 , 5 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE minCost ( arr , N ) NEW_LINE DEDENT
Space optimization using bit manipulations | Python3 code to for marking multiples ; index >> 5 corresponds to dividing index by 32 index & 31 corresponds to modulo operation of index by 32 Function to check value of bit position whether it is zero or one ; Sets value of bit for corresponding index ; Driver code ; Size that will be used is actual_size / 32 ceil is used to initialize the array with positive number ; Array is dynamically initialized as we are calculating size at run time ; Iterate through every index from a to b and call setbit ( ) if it is a multiple of 2 or 5
import math NEW_LINE def checkbit ( array , index ) : NEW_LINE INDENT return array [ index >> 5 ] & ( 1 << ( index & 31 ) ) NEW_LINE DEDENT def setbit ( array , index ) : NEW_LINE INDENT array [ index >> 5 ] |= ( 1 << ( index & 31 ) ) NEW_LINE DEDENT a = 2 NEW_LINE b = 10 NEW_LINE size = abs ( b - a ) NEW_LINE size = math . ceil ( size / 32 ) NEW_LINE array = [ 0 for i in range ( size ) ] NEW_LINE for i in range ( a , b + 1 ) : NEW_LINE INDENT if ( i % 2 == 0 or i % 5 == 0 ) : NEW_LINE INDENT setbit ( array , i - a ) NEW_LINE DEDENT DEDENT print ( " MULTIPLES ▁ of ▁ 2 ▁ and ▁ 5 : " ) NEW_LINE for i in range ( a , b + 1 ) : NEW_LINE INDENT if ( checkbit ( array , i - a ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Longest Span with same Sum in two Binary arrays | Returns length of the longest common subarray with same sum ; Initialize result ; One by one pick all possible starting points of subarrays ; Initialize sums of current subarrays ; Conider all points for starting with arr [ i ] ; Update sums ; If sums are same and current length is more than maxLen , update maxLen ; Driver program to test above function
def longestCommonSum ( arr1 , arr2 , n ) : NEW_LINE INDENT maxLen = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum1 = 0 NEW_LINE sum2 = 0 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT sum1 += arr1 [ j ] NEW_LINE sum2 += arr2 [ j ] NEW_LINE if ( sum1 == sum2 ) : NEW_LINE INDENT len = j - i + 1 NEW_LINE if ( len > maxLen ) : NEW_LINE INDENT maxLen = len NEW_LINE DEDENT DEDENT DEDENT DEDENT return maxLen NEW_LINE DEDENT arr1 = [ 0 , 1 , 0 , 1 , 1 , 1 , 1 ] NEW_LINE arr2 = [ 1 , 1 , 1 , 1 , 1 , 0 , 1 ] NEW_LINE n = len ( arr1 ) NEW_LINE print ( " Length ▁ of ▁ the ▁ longest ▁ common ▁ span ▁ with ▁ same ▁ " " sum ▁ is " , longestCommonSum ( arr1 , arr2 , n ) ) NEW_LINE
Number of Longest Increasing Subsequences | Function to count the number of LIS in the array nums [ ] ; Base Case ; Initialize dp_l array with 1 s ; Initialize dp_c array with 1 s ; If current element is smaller ; Store the maximum element from dp_l ; Stores the count of LIS ; Traverse dp_l and dp_c simultaneously ; Update the count ; Return the count of LIS ; Given array arr [ ] ; Function Call
def findNumberOfLIS ( nums ) : NEW_LINE INDENT if not nums : NEW_LINE INDENT return 0 NEW_LINE DEDENT n = len ( nums ) NEW_LINE dp_l = [ 1 ] * n NEW_LINE dp_c = [ 1 ] * n NEW_LINE for i , num in enumerate ( nums ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT if nums [ i ] <= nums [ j ] : NEW_LINE INDENT continue NEW_LINE DEDENT if dp_l [ j ] + 1 > dp_l [ i ] : NEW_LINE INDENT dp_l [ i ] = dp_l [ j ] + 1 NEW_LINE dp_c [ i ] = dp_c [ j ] NEW_LINE DEDENT elif dp_l [ j ] + 1 == dp_l [ i ] : NEW_LINE INDENT dp_c [ i ] += dp_c [ j ] NEW_LINE DEDENT DEDENT DEDENT max_length = max ( x for x in dp_l ) NEW_LINE count = 0 NEW_LINE for l , c in zip ( dp_l , dp_c ) : NEW_LINE INDENT if l == max_length : NEW_LINE INDENT count += c NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 1 , 3 , 5 , 4 , 7 ] NEW_LINE print ( findNumberOfLIS ( arr ) ) NEW_LINE
Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels | ''Tree node ; ''Function to return new tree node ; ''Function to check if the tree is even-odd tree ; '' Stores nodes of each level ; '' Store the current level  of the binary tree ; '' Traverse until the  queue is empty ; '' Stores the number of nodes  present in the current level ; '' Insert left and right child  of node into the queue ; '' If the level is even ; '' If the nodes in this  level are in strictly  increasing order or not ; '' If the level is odd ; '' If the nodes in this  level are in strictly  decreasing order or not ; '' Increment the level count ; ''Driver code ; '' Construct a Binary Tree ; '' Check if the binary tree  is even-odd tree or not
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . left = None NEW_LINE self . right = None NEW_LINE self . val = data NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT temp = Node ( data ) NEW_LINE return temp NEW_LINE DEDENT def checkEvenOddLevel ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return True NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE level = 0 NEW_LINE while ( len ( q ) != 0 ) : NEW_LINE INDENT l = [ ] NEW_LINE size = len ( q ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT node = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( node . left != None ) : NEW_LINE INDENT q . append ( node . left ) ; NEW_LINE DEDENT if ( node . right != None ) : NEW_LINE INDENT q . append ( node . right ) ; NEW_LINE DEDENT if ( level % 2 == 0 ) : NEW_LINE INDENT for i in range ( len ( l ) - 1 ) : NEW_LINE INDENT if ( l [ i + 1 ] > l [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT return False NEW_LINE DEDENT DEDENT elif ( level % 2 == 1 ) : NEW_LINE INDENT for i in range ( len ( l ) - 1 ) : NEW_LINE INDENT if ( l [ i + 1 ] < l [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT return False NEW_LINE DEDENT DEDENT level += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = None NEW_LINE root = newNode ( 2 ) NEW_LINE root . left = newNode ( 6 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 7 ) NEW_LINE root . right . right = newNode ( 11 ) NEW_LINE root . left . left . left = newNode ( 10 ) NEW_LINE root . left . left . right = newNode ( 5 ) NEW_LINE root . left . right . right = newNode ( 1 ) NEW_LINE if ( checkEvenOddLevel ( root ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Longest Span with same Sum in two Binary arrays | Returns largest common subarray with equal number of 0 s and 1 s ; Find difference between the two ; Creates an empty hashMap hM ; Initialize sum of elements ; Initialize result ; Traverse through the given array ; Add current element to sum ; To handle sum = 0 at last index ; If this sum is seen before , then update max_len if required ; Else put this sum in hash table ; Driver code
def longestCommonSum ( arr1 , arr2 , n ) : NEW_LINE INDENT arr = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ i ] = arr1 [ i ] - arr2 [ i ] ; NEW_LINE DEDENT hm = { } NEW_LINE sum = 0 NEW_LINE max_len = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE if ( sum == 0 ) : NEW_LINE INDENT max_len = i + 1 NEW_LINE DEDENT if sum in hm : NEW_LINE INDENT max_len = max ( max_len , i - hm [ sum ] ) NEW_LINE DEDENT else : NEW_LINE INDENT hm [ sum ] = i NEW_LINE DEDENT DEDENT return max_len NEW_LINE DEDENT arr1 = [ 0 , 1 , 0 , 1 , 1 , 1 , 1 ] NEW_LINE arr2 = [ 1 , 1 , 1 , 1 , 1 , 0 , 1 ] NEW_LINE n = len ( arr1 ) NEW_LINE print ( longestCommonSum ( arr1 , arr2 , n ) ) NEW_LINE
Minimum length of Run Length Encoding possible by removing at most K characters from a given string | Python3 program to implement the above approach ; Function which solves the desired problem ; Base Case ; If the entire string has been traversed ; If precomputed subproblem occurred ; Minimum run length encoding by removing the current character ; Minimum run length encoding by retaining the current character ; If the current and the previous characters match ; Otherwise ; Function to return minimum run - length encoding for string s by removing atmost k characters ; Driver Code
maxN = 20 NEW_LINE dp = [ [ [ [ 0 for i in range ( maxN ) ] for j in range ( 27 ) ] for k in range ( 27 ) ] for l in range ( maxN ) ] NEW_LINE def solve ( s , n , idx , k , last , count ) : NEW_LINE INDENT if ( k < 0 ) : NEW_LINE INDENT return n + 1 NEW_LINE DEDENT if ( idx == n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = dp [ idx ] [ k ] [ ord ( last ) - ord ( ' a ' ) ] [ count ] NEW_LINE if ( ans != - 1 ) : NEW_LINE INDENT return ans NEW_LINE DEDENT ans = n + 1 NEW_LINE ans = min ( ans , solve ( s , n , idx + 1 , k - 1 , last , count ) ) NEW_LINE inc = 0 NEW_LINE if ( count == 1 or count == 9 or count == 99 ) : NEW_LINE INDENT inc = 1 NEW_LINE DEDENT if ( last == s [ idx ] ) : NEW_LINE INDENT ans = min ( ans , inc + solve ( s , n , idx + 1 , k , s [ idx ] , count + 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = max ( ans , 1 + solve ( s , n , idx + 1 , k , s [ idx ] , 1 ) ) NEW_LINE DEDENT dp [ idx ] [ k ] [ ord ( last ) - ord ( ' a ' ) ] [ count ] = ans NEW_LINE print ( ans ) NEW_LINE return dp [ idx ] [ k ] [ ord ( last ) - ord ( ' a ' ) ] [ count ] NEW_LINE DEDENT def MinRunLengthEncoding ( s , n , k ) : NEW_LINE INDENT for i in range ( maxN ) : NEW_LINE INDENT for j in range ( 27 ) : NEW_LINE INDENT for k in range ( 27 ) : NEW_LINE INDENT for l in range ( maxN ) : NEW_LINE INDENT dp [ i ] [ j ] [ k ] [ l ] = - 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return solve ( s , n , 0 , k , chr ( 123 ) , 0 ) - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abbbcdcdd " NEW_LINE N = 9 NEW_LINE K = 2 NEW_LINE print ( MinRunLengthEncoding ( S , N , K ) ) NEW_LINE DEDENT
Count unique paths is a matrix whose product of elements contains odd number of divisors | Python3 program for the above approach ; Function to calculate and store all the paths product in vector ; Base Case ; If reaches the bottom right corner and product is a perfect square ; Find square root ; If square root is an integer ; Move towards down in the matrix ; Move towards right in the matrix ; Driver Code ; Given matrix mat ; Function call
import math NEW_LINE countPaths = 0 ; NEW_LINE def CountUniquePaths ( a , i , j , m , n , ans ) : NEW_LINE INDENT if ( i >= m or j >= n ) : NEW_LINE INDENT return ; NEW_LINE DEDENT global countPaths ; NEW_LINE if ( i == m - 1 and j == n - 1 ) : NEW_LINE INDENT sr = math . sqrt ( ans * a [ i ] [ j ] ) ; NEW_LINE if ( ( sr - math . floor ( sr ) ) == 0 ) : NEW_LINE INDENT countPaths += 1 ; NEW_LINE DEDENT DEDENT CountUniquePaths ( a , i + 1 , j , m , n , ans * a [ i ] [ j ] ) ; NEW_LINE CountUniquePaths ( a , i , j + 1 , m , n , ans * a [ i ] [ j ] ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M = 3 ; N = 2 ; NEW_LINE mat = [ [ 1 , 1 ] , [ 3 , 1 ] , [ 3 , 1 ] ] ; NEW_LINE CountUniquePaths ( mat , 0 , 0 , M , N , 1 ) ; NEW_LINE print ( countPaths ) ; NEW_LINE DEDENT
Maximize sum by selecting M elements from the start or end of rows of a Matrix | Function to select m elements having maximum sum ; Base case ; If precomputed subproblem occurred ; Either skip the current row ; Iterate through all the possible segments of current row ; Check if it is possible to select elements from i to j ; Compuete the sum of i to j as calculated ; Store the computed answer and return ; Function to precompute the prefix sum for every row of the matrix ; Preprocessing to calculate sum from i to j ; Utility function to select m elements having maximum sum ; Preprocessing step ; Initialize dp array with - 1 ; Stores maximum sum of M elements ; Driver Code ; Given N ; Given M ; Given matrix ; Function call
def mElementsWithMaxSum ( matrix , M , block , dp ) : NEW_LINE INDENT if block == len ( matrix ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ block ] [ M ] != - 1 ) : NEW_LINE INDENT return dp [ block ] [ M ] NEW_LINE DEDENT ans = mElementsWithMaxSum ( matrix , M , block + 1 , dp ) NEW_LINE for i in range ( len ( matrix [ block ] ) ) : NEW_LINE INDENT for j in range ( i , len ( matrix [ block ] ) ) : NEW_LINE INDENT if ( j - i + 1 <= M ) : NEW_LINE INDENT x = 0 NEW_LINE if i - 1 >= 0 : NEW_LINE INDENT x = matrix [ block ] [ i - 1 ] NEW_LINE DEDENT ans = max ( ans , matrix [ block ] [ j ] - x + mElementsWithMaxSum ( matrix , M - j + i - 1 , block + 1 , dp ) ) NEW_LINE DEDENT DEDENT DEDENT dp [ block ] [ M ] = ans NEW_LINE return ans NEW_LINE DEDENT def preComputing ( matrix , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( len ( matrix [ i ] ) ) : NEW_LINE INDENT if j > 0 : NEW_LINE INDENT matrix [ i ] [ j ] = matrix [ i ] [ j - 1 ] NEW_LINE DEDENT DEDENT DEDENT return matrix NEW_LINE DEDENT def mElementsWithMaxSumUtil ( matrix , M , N ) : NEW_LINE INDENT matrix = preComputing ( matrix , N ) NEW_LINE sum = 20 NEW_LINE dp = [ [ - 1 for i in range ( M + 5 ) ] for i in range ( N + 5 ) ] NEW_LINE sum += mElementsWithMaxSum ( matrix , M , 0 , dp ) NEW_LINE print ( sum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE M = 4 NEW_LINE matrix = [ [ 2 , 3 , 5 ] , [ - 1 , 7 ] , [ 8 , 10 ] ] NEW_LINE mElementsWithMaxSumUtil ( matrix , M , N ) NEW_LINE DEDENT
Find the last remaining element after repeated removal of odd and even indexed elements alternately | Function to calculate the last remaining element from the sequence ; If dp [ n ] is already calculated ; Base Case : ; Recursive Call ; Return the value of dp [ n ] ; Given N ; Stores the ; Function Call
def lastRemaining ( n , dp ) : NEW_LINE INDENT if n in dp : NEW_LINE INDENT return dp [ n ] NEW_LINE DEDENT if n == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ n ] = 2 * ( 1 + n // 2 - lastRemaining ( n // 2 , dp ) ) NEW_LINE DEDENT return dp [ n ] NEW_LINE DEDENT N = 5 NEW_LINE dp = { } NEW_LINE print ( lastRemaining ( N , dp ) ) NEW_LINE