codeparrot/xlcost-text-to-code huggingface dataset:
Count of non | Python 3 program for the above approach ; Define the dp table globally ; Recursive function to calculate total number of valid non - decreasing strings ; If already calculated state ; Base Case ; Stores the total count of strings formed ; Fill the value in dp matrix ; Function to find the total number of non - decreasing string formed by replacing the '? ; Initialize all value of dp table with - 1 ; Left and Right limits ; Iterate through all the characters of the string S ; Change R to the current character ; Call the recursive function ; Change L to R and R to 9 ; Reinitialize the length of ? to 0 ; Increment the length of the segment ; Update the ans ; Return the total count ; Driver Code
MAXN = 100005 NEW_LINE dp = [ [ - 1 for x in range ( 10 ) ] for y in range ( MAXN ) ] NEW_LINE def solve ( len , gap ) : NEW_LINE INDENT if ( dp [ len ] [ gap ] != - 1 ) : NEW_LINE INDENT return dp [ len ] [ gap ] NEW_LINE DEDENT if ( len == 0 or gap == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( gap < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( gap + 1 ) : NEW_LINE INDENT ans += solve ( len - 1 , gap - i ) NEW_LINE DEDENT dp [ len ] [ gap ] = ans NEW_LINE return dp [ len ] [ gap ] NEW_LINE DEDENT ' NEW_LINE def countValidStrings ( S ) : NEW_LINE INDENT global dp NEW_LINE N = len ( S ) NEW_LINE L , R = 1 , 9 NEW_LINE cnt = 0 NEW_LINE ans = 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] != ' ? ' ) : NEW_LINE INDENT R = ord ( S [ i ] ) - ord ( '0' ) NEW_LINE ans *= solve ( cnt , R - L ) NEW_LINE L = R NEW_LINE R = 9 NEW_LINE cnt = 0 NEW_LINE DEDENT else : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT ans *= solve ( cnt , R - L ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "1 ? ? ?2" NEW_LINE print ( countValidStrings ( S ) ) NEW_LINE DEDENT
Count of binary arrays of size N with sum of product of adjacent pairs equal to K | Function to return the number of total possible combinations of 0 and 1 to form an array of size N having sum of product of consecutive elements K ; If value is greater than K , then return 0 as no combination is possible to have sum K ; Check if the result of this recursive call is memoised already , if it is then just return the previously calculated result ; Check if the value is equal to K at N , if it is then return 1 as this combination is possible . Otherwise return 0. ; If previous element is 1 ; If current element is 1 as well , then add 1 to value ; If current element is 0 , then value will remain same ; If previous element is 0 , then value will remain same irrespective of the current element ; Memoise and return the ans ; Driver Code ; As the array can be started by 0 or 1 , so take both cases while calculating the total possible combinations
def combinationsPossible ( N , idx , prev , val , K , dp ) : NEW_LINE INDENT if ( val > K ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ val ] [ idx ] [ prev ] != - 1 ) : NEW_LINE INDENT return dp [ val ] [ idx ] [ prev ] NEW_LINE DEDENT if ( idx == N - 1 ) : NEW_LINE INDENT if ( val == K ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT ans = 0 NEW_LINE if ( prev == 1 ) : NEW_LINE INDENT ans += combinationsPossible ( N , idx + 1 , 1 , val + 1 , K , dp ) NEW_LINE ans += combinationsPossible ( N , idx + 1 , 0 , val , K , dp ) NEW_LINE DEDENT else : NEW_LINE INDENT ans += combinationsPossible ( N , idx + 1 , 1 , val , K , dp ) NEW_LINE ans += combinationsPossible ( N , idx + 1 , 0 , val , K , dp ) NEW_LINE DEDENT dp [ val ] [ idx ] [ prev ] = ans NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE K = 3 NEW_LINE dp = [ [ [ - 1 for i in range ( 2 ) ] for j in range ( N + 1 ) ] for k in range ( K + 1 ) ] NEW_LINE print ( combinationsPossible ( N , 0 , 0 , 0 , K , dp ) + combinationsPossible ( N , 0 , 1 , 0 , K , dp ) ) NEW_LINE DEDENT
Count ways to split an array into subarrays such that sum of the i | Python3 program for the above approach ; Function to count ways to split an array into subarrays such that sum of the i - th subarray is divisible by i ; Stores the prefix sum of array ; Find the prefix sum ; Initialize dp [ ] [ ] array ; Stores the count of splitting ; Iterate over the range [ 0 , N ] ; Update the dp table ; If the last index is reached , then add it to the variable ans ; Return the possible count of splitting of array into subarrays ; Driver Code
import numpy as np NEW_LINE def countOfWays ( arr , N ) : NEW_LINE INDENT pre = [ 0 ] * ( N + 1 ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT pre [ i + 1 ] = pre [ i ] + arr [ i ] ; NEW_LINE DEDENT dp = np . zeros ( ( N + 2 , N + 2 ) ) ; NEW_LINE dp [ 1 ] [ 0 ] += 1 ; NEW_LINE ans = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N , 0 , - 1 ) : NEW_LINE INDENT dp [ j + 1 ] [ pre [ i + 1 ] % ( j + 1 ) ] += dp [ j ] [ pre [ i + 1 ] % j ] ; NEW_LINE if ( i == N - 1 ) : NEW_LINE INDENT ans += dp [ j ] [ pre [ i + 1 ] % j ] ; NEW_LINE DEDENT DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( countOfWays ( arr , N ) ) ; NEW_LINE DEDENT
Domino and Tromino tiling problem | Function to find the total number of ways to tile a 2 * N board using the given types of tile ; If N is less than 3 ; Store all dp - states ; Base Case ; Traverse the range [ 2 , N ] ; Update the value of dp [ i ] [ 0 ] ; Update the value of dp [ i ] [ 1 ] ; Update the value of dp [ i ] [ 2 ] ; Return the number of ways as the value of dp [ N ] [ 0 ] ; Driver Code
MOD = 1e9 + 7 NEW_LINE def numTilings ( N ) : NEW_LINE INDENT if ( N < 3 ) : NEW_LINE INDENT return N NEW_LINE DEDENT dp = [ [ 0 ] * 3 for i in range ( N + 1 ) ] NEW_LINE dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = 1 NEW_LINE dp [ 1 ] [ 1 ] = dp [ 1 ] [ 2 ] = 1 NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 2 ] [ 0 ] + dp [ i - 2 ] [ 1 ] + dp [ i - 2 ] [ 2 ] ) % MOD NEW_LINE dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 2 ] ) % MOD NEW_LINE dp [ i ] [ 2 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] ) % MOD NEW_LINE DEDENT return int ( dp [ N ] [ 0 ] ) NEW_LINE DEDENT N = 3 NEW_LINE print ( numTilings ( N ) ) NEW_LINE
Count of N | Store the recurring recursive states ; Function to find the number of strings of length N such that it is a concatenation it substrings ; Single character cant be repeated ; Check if this state has been already calculated ; Stores the resultant count for the current recursive calls ; Iterate over all divisors ; Non - Repeated = Total - Repeated ; Non - Repeated = Total - Repeated ; Store the result for the further calculation ; Return resultant count ; Driver Code ; Function Call
dp = { } NEW_LINE def countStrings ( N ) : NEW_LINE INDENT if N == 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT if dp . get ( N , - 1 ) != - 1 : NEW_LINE INDENT return dp [ N ] NEW_LINE DEDENT ret = 0 NEW_LINE for div in range ( 1 , int ( N ** .5 ) + 1 ) : NEW_LINE INDENT if N % div == 0 : NEW_LINE INDENT ret += ( 1 << div ) - countStrings ( div ) NEW_LINE div2 = N // div NEW_LINE if div2 != div and div != 1 : NEW_LINE INDENT ret += ( 1 << div2 ) - countStrings ( div2 ) NEW_LINE DEDENT DEDENT DEDENT dp [ N ] = ret NEW_LINE return ret NEW_LINE DEDENT N = 6 NEW_LINE print ( countStrings ( N ) ) NEW_LINE
Count N | python program for the above approach ; Function to count N - digit numbers such that each position is divisible by the digit occurring at that position ; Stores the answer . ; Iterate from indices 1 to N ; Stores count of digits that can be placed at the current index ; Iterate from digit 1 to 9 ; If index is divisible by digit ; Multiply answer with possible choices ; Given Input ; Function call
mod = 1000000000 + 7 NEW_LINE def countOfNumbers ( N ) : NEW_LINE INDENT ans = 1 NEW_LINE for index in range ( 1 , N + 1 ) : NEW_LINE INDENT choices = 0 NEW_LINE for digit in range ( 1 , 10 ) : NEW_LINE INDENT if ( index % digit == 0 ) : NEW_LINE INDENT choices += 1 NEW_LINE DEDENT DEDENT ans = ( ans * choices ) % mod NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT N = 5 NEW_LINE countOfNumbers ( N ) NEW_LINE
Maximum number of groups that can receive fresh donuts distributed in batches of size K | Recursive function to find the maximum number of groups that will receive fresh donuts ; Store the result for the current state ; Check if the leftover donuts from the previous batch is 0 ; If true , then one by one give the fresh donuts to each group ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] ; Otherwise , traverse the given array , arr [ ] ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] ; Return the value of q ; Function to find the maximum number of groups that will receive fresh donuts ; Stores count of remainder by K ; Traverse the array arr [ ] ; Stores maximum number of groups ; Return the answer ; Driver Code
def dfs ( arr , left , K ) : NEW_LINE INDENT q = 0 NEW_LINE if ( left == 0 ) : NEW_LINE INDENT for i in range ( 1 , K , 1 ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT arr [ i ] -= 1 NEW_LINE q = max ( q , 1 + dfs ( arr , K - i , K ) ) NEW_LINE arr [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in range ( 1 , K , 1 ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT arr [ i ] -= 1 NEW_LINE nleft = left - i if i <= left else K + left - i NEW_LINE q = max ( q , dfs ( arr , nleft , K ) ) NEW_LINE arr [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT return q NEW_LINE DEDENT def maxGroups ( K , arr , n ) : NEW_LINE INDENT V = [ 0 for i in range ( K ) ] NEW_LINE for x in range ( n ) : NEW_LINE INDENT V [ arr [ x ] % K ] += 1 NEW_LINE DEDENT ans = V [ 0 ] + dfs ( V , 0 , K ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( maxGroups ( K , arr , n ) ) NEW_LINE DEDENT
Maximum jumps to reach end of Array with condition that index i can make arr [ i ] jumps | Function to find the maximum jumps to reach end of array ; Stores the jumps needed to reach end from each index ; Traverse the array ; Check if j is less than N ; Add dp [ j ] to the value of dp [ i ] ; Update the value of ans ; Print the value of ans ; Driver Code
def findMaxJumps ( arr , N ) : NEW_LINE INDENT dp = [ 0 for i in range ( N ) ] NEW_LINE ans = 0 NEW_LINE i = N - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT dp [ i ] = arr [ i ] NEW_LINE j = i + arr [ i ] NEW_LINE if ( j < N ) : NEW_LINE INDENT dp [ i ] = dp [ i ] + dp [ j ] NEW_LINE DEDENT ans = max ( ans , dp [ i ] ) NEW_LINE i -= 1 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 7 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE findMaxJumps ( arr , N ) NEW_LINE DEDENT
Longest subsequence with non negative prefix sum at each position | Function to find the length of the longest subsequence with non negative prefix sum at each position ; Initialize dp array with - 1 ; Maximum subsequence sum by including no elements till position 'i ; Maximum subsequence sum by including first element at first position ; Iterate over all the remaining positions ; If the current element is excluded ; If current element is included and if total sum is positive or not ; Select the maximum j by which a non negative prefix sum subsequence can be obtained ; Print the answer ; Driver Code
def longestSubsequence ( arr , N ) : NEW_LINE INDENT dp = [ [ - 1 for i in range ( N + 1 ) ] for i in range ( N ) ] NEW_LINE DEDENT ' NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT dp [ i ] [ 0 ] = 0 NEW_LINE DEDENT dp [ 0 ] [ 1 ] = arr [ 0 ] if arr [ 0 ] >= 0 else - 1 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( 1 , i + 2 ) : NEW_LINE INDENT if ( dp [ i - 1 ] [ j ] != - 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j ] ) NEW_LINE DEDENT if ( dp [ i - 1 ] [ j - 1 ] >= 0 and dp [ i - 1 ] [ j - 1 ] + arr [ i ] >= 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - 1 ] + arr [ i ] ) NEW_LINE DEDENT DEDENT DEDENT ans = 0 NEW_LINE for j in range ( N + 1 ) : NEW_LINE INDENT if ( dp [ N - 1 ] [ j ] >= 0 ) : NEW_LINE INDENT ans = j NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT arr = [ 4 , - 4 , 1 , - 3 , 1 , - 3 ] NEW_LINE N = len ( arr ) NEW_LINE longestSubsequence ( arr , N ) NEW_LINE
Count of strictly increasing N | Declaration of dp table ; Function to find the count of all N digit numbers such that all the digit is less than its adjacent digits ; Base Case : If i = n , then return 1 as valid number has been formed ; If the state has already been computed , then return it ; Stores the total count of ways for the current recursive call ; If i = 0 , any digit from [ 1 - 9 ] can be placed and also if N = 1 , then 0 can also be placed ; If i = 1 , any digit from [ 0 - 9 ] can be placed such that digit is not equal to previous digit ; If the current digit is not same as the prev ; Place the current digit such that it is less than the previous digit ; Place current digit such that it is more than the previous digit ; Return the resultant total count ; Function to find all N - digit numbers satisfying the given criteria ; Function call to count all possible ways ; Driver Code
dp = [ [ [ - 1 for x in range ( 2 ) ] for y in range ( 10 ) ] for z in range ( 100 ) ] NEW_LINE def solve ( i , n , prev , sign ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT val = dp [ i ] [ prev ] [ sign ] NEW_LINE if ( val != - 1 ) : NEW_LINE INDENT return val NEW_LINE DEDENT val = 0 NEW_LINE if ( i == 0 ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT digit = 0 NEW_LINE DEDENT else : NEW_LINE INDENT digit = 1 NEW_LINE DEDENT while digit <= 9 : NEW_LINE INDENT val += solve ( i + 1 , n , digit , sign ) NEW_LINE digit += 1 NEW_LINE DEDENT DEDENT elif ( i == 1 ) : NEW_LINE INDENT for digit in range ( 10 ) : NEW_LINE INDENT if ( digit != prev ) : NEW_LINE INDENT val += solve ( i + 1 , n , digit , ( digit > prev ) ) NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT if ( sign == 1 ) : NEW_LINE INDENT for digit in range ( prev - 1 , - 1 , - 1 ) : NEW_LINE INDENT val += solve ( i + 1 , n , digit , 0 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for digit in range ( prev + 1 , 10 ) : NEW_LINE INDENT val += solve ( i + 1 , n , digit , 1 ) NEW_LINE DEDENT DEDENT DEDENT return val NEW_LINE DEDENT def countNdigitNumber ( N ) : NEW_LINE INDENT print ( solve ( 0 , N , 0 , 0 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE countNdigitNumber ( N ) NEW_LINE DEDENT
Total count of sorted numbers upto N digits in range [ L , R ] ( Magnificent necklace combinatorics problem ) | Function to count total number of ways ; Stores all DP - states ; Stores the result ; Traverse the range [ 0 , N ] ; Traverse the range [ 1 , R - L ] ; Update dp [ i ] [ j ] ; Assign dp [ 0 ] [ R - L ] to ans ; Traverse the range [ 1 , N ] ; Traverse the range [ 1 , R - L ] ; Update dp [ i ] [ j ] ; Increment ans by dp [ i - 1 ] [ j ] ; Return ans ; Driver Code ; Input ; Function call
def Count ( N , L , R ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( R - L + 1 ) ] for i in range ( N ) ] NEW_LINE ans = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT dp [ i ] [ 0 ] = 1 NEW_LINE DEDENT for i in range ( 1 , len ( dp [ 0 ] ) ) : NEW_LINE INDENT dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + 1 NEW_LINE DEDENT ans = dp [ 0 ] [ R - L ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( 1 , len ( dp [ 0 ] ) ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] NEW_LINE DEDENT ans += dp [ i ] [ R - L ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE L = 6 NEW_LINE R = 9 NEW_LINE print ( Count ( N , L , R ) ) NEW_LINE DEDENT
Length of largest common subarray in all the rows of given Matrix | Function to find longest common subarray in all the rows of the matrix ; Array to store the position of element in every row ; Traverse the matrix ; Store the position of every element in every row ; Variable to store length of largest common Subarray ; Traverse through the matrix column ; Variable to check if every row has arr [ i ] [ j ] next to arr [ i - 1 ] [ j ] or not ; Traverse through the matrix rows ; Check if arr [ i ] [ j ] is next to arr [ i ] [ j - 1 ] in every row or not ; If every row has arr [ 0 ] [ j ] next to arr [ 0 ] [ j - 1 ] increment len by 1 and update the value of ans ; Driver Code ; Given Input ; Function Call
def largestCommonSubarray ( arr , n , m ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( m + 1 ) ] for j in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT dp [ i ] [ arr [ i ] [ j ] ] = j NEW_LINE DEDENT DEDENT ans = 1 NEW_LINE len1 = 1 NEW_LINE for i in range ( 1 , m , 1 ) : NEW_LINE INDENT check = True NEW_LINE for j in range ( 1 , n , 1 ) : NEW_LINE INDENT if ( dp [ j ] [ arr [ 0 ] [ i - 1 ] ] + 1 != dp [ j ] [ arr [ 0 ] [ i ] ] ) : NEW_LINE INDENT check = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( check ) : NEW_LINE INDENT len1 += 1 NEW_LINE ans = max ( ans , len1 ) NEW_LINE DEDENT else : NEW_LINE INDENT len1 = 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE m = 5 NEW_LINE arr = [ [ 4 , 5 , 1 , 2 , 3 , 6 , 7 ] , [ 1 , 2 , 4 , 5 , 7 , 6 , 3 ] , [ 2 , 7 , 3 , 4 , 5 , 1 , 6 ] ] NEW_LINE N = len ( arr ) NEW_LINE M = len ( arr [ 0 ] ) NEW_LINE print ( largestCommonSubarray ( arr , N , M ) ) NEW_LINE DEDENT
Maximize sum that can be obtained from two given arrays based on given conditions | Function to find the maximum sum that can be obtained from two given arrays based on the following conditions ; Stores the maximum sum from 0 to i ; Initialize the value of dp [ 0 ] [ 0 ] and dp [ 0 ] [ 1 ] ; Traverse the array A [ ] and B [ ] ; If A [ i ] is considered ; If B [ i ] is not considered ; If B [ i ] is considered ; If i = 1 , then consider the value of dp [ i ] [ 1 ] as b [ i ] ; Return maximum sum ; Driver code ; Given input ; Function call
def MaximumSum ( a , b , n ) : NEW_LINE INDENT dp = [ [ - 1 for j in range ( 2 ) ] for i in range ( n ) ] NEW_LINE dp [ 0 ] [ 0 ] = a [ 0 ] NEW_LINE dp [ 0 ] [ 1 ] = b [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT dp [ i ] [ 0 ] = max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + a [ i ] NEW_LINE dp [ i ] [ 1 ] = max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) NEW_LINE if ( i - 2 >= 0 ) : NEW_LINE INDENT dp [ i ] [ 1 ] = max ( dp [ i ] [ 1 ] , max ( dp [ i - 2 ] [ 0 ] , dp [ i - 2 ] [ 1 ] ) + b [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ 1 ] = max ( dp [ i ] [ 1 ] , b [ i ] ) NEW_LINE DEDENT DEDENT return max ( dp [ n - 1 ] [ 0 ] , dp [ n - 1 ] [ 1 ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 10 , 1 , 10 , 10 ] NEW_LINE B = [ 5 , 50 , 1 , 5 ] NEW_LINE N = len ( A ) NEW_LINE print ( MaximumSum ( A , B , N ) ) NEW_LINE DEDENT
Lexicographically Kth | Python3 program for the above approach ; Function to fill dp array ; Initialize all the entries with 0 ; Update dp [ 0 ] [ 0 ] to 1 ; Traverse the dp array ; Update the value of dp [ i ] [ j ] ; Recursive function to find the Kth lexicographical smallest string ; Handle the base cases ; If there are more than or equal to K strings which start with a , then the first character is 'a ; Otherwise the first character of the resultant string is 'b ; Function to find the Kth lexicographical smallest string ; Function call to fill the dp array ; Print the resultant ; Given Input ; Function Call
from typing import Mapping NEW_LINE MAX = 30 NEW_LINE def findNumString ( X , Y , dp ) : NEW_LINE INDENT for i in range ( 0 , MAX ) : NEW_LINE INDENT for j in range ( 0 , MAX ) : NEW_LINE INDENT dp [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT dp [ 0 ] [ 0 ] = 1 NEW_LINE for i in range ( 0 , X + 1 ) : NEW_LINE INDENT for j in range ( 0 , Y + 1 ) : NEW_LINE INDENT if ( i > 0 ) : NEW_LINE INDENT dp [ i ] [ j ] += dp [ i - 1 ] [ j ] NEW_LINE DEDENT if ( j > 0 ) : NEW_LINE INDENT dp [ i ] [ j ] += dp [ i ] [ j - 1 ] NEW_LINE DEDENT DEDENT DEDENT DEDENT def kthString ( X , Y , K , dp ) : NEW_LINE INDENT x1 = " " NEW_LINE y1 = " " NEW_LINE for i in range ( 0 , Y ) : NEW_LINE INDENT x1 += ' b ' NEW_LINE DEDENT for i in range ( 0 , X ) : NEW_LINE INDENT y1 += ' a ' NEW_LINE DEDENT if ( X == 0 ) : NEW_LINE INDENT return x1 NEW_LINE DEDENT if ( Y == 0 ) : NEW_LINE INDENT return y1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( K <= dp [ X - 1 ] [ Y ] ) : NEW_LINE INDENT return " a " + kthString ( X - 1 , Y , K , dp ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT else : NEW_LINE INDENT return " b " + kthString ( X , Y - 1 , K - dp [ X - 1 ] [ Y ] , dp ) NEW_LINE DEDENT DEDENT def kthStringUtil ( X , Y , K ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( MAX ) ] for col in range ( MAX ) ] NEW_LINE findNumString ( X , Y , dp ) NEW_LINE print ( kthString ( X , Y , K , dp ) ) NEW_LINE DEDENT X = 4 NEW_LINE Y = 3 NEW_LINE K = 4 NEW_LINE kthStringUtil ( X , Y , K ) NEW_LINE
Maximum Tip Calculator | Function that finds the maximum tips from the given arrays as per the given conditions ; Base Condition ; If both have non - zero count then return max element from both array ; Traverse first array , as y count has become 0 ; Traverse 2 nd array , as x count has become 0 ; Drive Code ; Function Call
def maximumTip ( arr1 , arr2 , n , x , y ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT if x != 0 and y != 0 : NEW_LINE INDENT return max ( arr1 [ n - 1 ] + maximumTip ( arr1 , arr2 , n - 1 , x - 1 , y ) , arr2 [ n - 1 ] + maximumTip ( arr1 , arr2 , n - 1 , x , y - 1 ) ) NEW_LINE DEDENT if y == 0 : NEW_LINE INDENT return arr1 [ n - 1 ] + maximumTip ( arr1 , arr2 , n - 1 , x - 1 , y ) NEW_LINE DEDENT else : NEW_LINE INDENT return arr2 [ n - 1 ] + maximumTip ( arr1 , arr2 , n - 1 , x , y - 1 ) NEW_LINE DEDENT DEDENT N = 5 NEW_LINE X = 3 NEW_LINE Y = 3 NEW_LINE A = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE B = [ 5 , 4 , 3 , 2 , 1 ] NEW_LINE print ( maximumTip ( A , B , N , X , Y ) ) NEW_LINE
Number of ways such that only K bars are visible from the left | Function to calculate the number of bars that are visible from the left for a particular arrangement ; If current element is greater than the last greater element , it is visible ; Function to calculate the number of rearrangements where K bars are visiblef from the left ; Vector to store current permutation ; Check for all permutations ; If current permutation meets the conditions , increment answer ; Driver code ; Input ; Function call
def noOfbarsVisibleFromLeft ( v ) : NEW_LINE INDENT last = 0 NEW_LINE ans = 0 NEW_LINE for u in v : NEW_LINE INDENT if ( last < u ) : NEW_LINE INDENT ans += 1 NEW_LINE last = u NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def nextPermutation ( nums ) : NEW_LINE INDENT i = len ( nums ) - 2 NEW_LINE while i > - 1 : NEW_LINE INDENT if nums [ i ] < nums [ i + 1 ] : NEW_LINE INDENT j = len ( nums ) - 1 NEW_LINE while j > i : NEW_LINE INDENT if nums [ j ] > nums [ i ] : NEW_LINE INDENT break NEW_LINE DEDENT j -= 1 NEW_LINE DEDENT nums [ i ] , nums [ j ] = nums [ j ] , nums [ i ] NEW_LINE break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT nums [ i + 1 : ] = reversed ( nums [ i + 1 : ] ) NEW_LINE return nums NEW_LINE DEDENT def KvisibleFromLeft ( N , K ) : NEW_LINE INDENT v = [ 0 ] * ( N ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT v [ i ] = i + 1 NEW_LINE DEDENT ans = 0 NEW_LINE temp = list ( v ) NEW_LINE while True : NEW_LINE INDENT if ( noOfbarsVisibleFromLeft ( v ) == K ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT v = nextPermutation ( v ) NEW_LINE if v == temp : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE K = 2 NEW_LINE print ( KvisibleFromLeft ( N , K ) ) NEW_LINE DEDENT