codeparrot/xlcost-text-to-code huggingface dataset:
Divide an array into K subarray with the given condition | Function to divide an array into k parts such that the summ of difference of every element with the maximum element of that part is minimum ; Dp to store the values ; Fill up the dp table ; Intitilize maximum value ; Max element and the summ ; Run a loop from i to n ; Find the maximum number from i to l and the summ from i to l ; Find the summ of difference of every element with the maximum element ; If the array can be divided ; Returns the minimum summ in K parts ; Driver code
def divideArray ( arr , n , k ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( 500 ) ] for i in range ( 500 ) ] NEW_LINE k -= 1 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( 0 , k + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = 10 ** 9 NEW_LINE max_ = - 1 NEW_LINE summ = 0 NEW_LINE for l in range ( i , n ) : NEW_LINE INDENT max_ = max ( max_ , arr [ l ] ) NEW_LINE summ += arr [ l ] NEW_LINE diff = ( l - i + 1 ) * max_ - summ NEW_LINE if ( j > 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = min ( dp [ i ] [ j ] , diff + dp [ l + 1 ] [ j - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = diff NEW_LINE DEDENT DEDENT DEDENT DEDENT return dp [ 0 ] [ k ] NEW_LINE DEDENT arr = [ 2 , 9 , 5 , 4 , 8 , 3 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE print ( divideArray ( arr , n , k ) ) NEW_LINE
Count of subsets not containing adjacent elements | Function to return the count of possible subsets ; Total possible subsets of n sized array is ( 2 ^ n - 1 ) ; To store the required count of subsets ; Run from i 000. .0 to 111. .1 ; If current subset has consecutive elements from the array ; Driver code
def cntSubsets ( arr , n ) : NEW_LINE INDENT max = pow ( 2 , n ) NEW_LINE result = 0 NEW_LINE for i in range ( max ) : NEW_LINE INDENT counter = i NEW_LINE if ( counter & ( counter >> 1 ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT result += 1 NEW_LINE DEDENT return result NEW_LINE DEDENT arr = [ 3 , 5 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( cntSubsets ( arr , n ) ) NEW_LINE
Count of subsets not containing adjacent elements | Function to return the count of possible subsets ; If previous element was 0 then 0 as well as 1 can be appended ; If previous element was 1 then only 0 can be appended ; Store the count of all possible subsets ; Driver code
def cntSubsets ( arr , n ) : NEW_LINE INDENT a = [ 0 ] * n NEW_LINE b = [ 0 ] * n ; NEW_LINE a [ 0 ] = b [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; NEW_LINE b [ i ] = a [ i - 1 ] ; NEW_LINE DEDENT result = a [ n - 1 ] + b [ n - 1 ] ; NEW_LINE return result ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 5 , 7 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( cntSubsets ( arr , n ) ) ; NEW_LINE DEDENT
Optimal Strategy for a Game | Set 3 | Calculates the maximum score possible for P1 If only the bags from beg to ed were available ; Length of the game ; Which turn is being played ; Base case i . e last turn ; if it is P1 's turn ; if P2 's turn ; Player picks money from the left end ; Player picks money from the right end ; If it is player 1 's turn then  current picked score added to  his total. choose maximum of  the two scores as P1 tries to  maximize his score. ; If player 2 ' s ▁ turn ▁ don ' t add current picked bag score to total . choose minimum of the two scores as P2 tries to minimize P1 's score. ; Input array ; Function Calling
def maxScore ( money , beg , ed ) : NEW_LINE INDENT totalTurns = len ( money ) NEW_LINE turnsTillNow = beg + ( ( totalTurns - 1 ) - ed ) NEW_LINE if ( beg == ed ) : NEW_LINE INDENT if ( turnsTillNow % 2 == 0 ) : NEW_LINE INDENT return [ money [ beg ] , " L " ] NEW_LINE DEDENT else : NEW_LINE INDENT return [ 0 , " L " ] NEW_LINE DEDENT DEDENT scoreOne = maxScore ( money , beg + 1 , ed ) NEW_LINE scoreTwo = maxScore ( money , beg , ed - 1 ) NEW_LINE if ( turnsTillNow % 2 == 0 ) : NEW_LINE INDENT if ( money [ beg ] + scoreOne [ 0 ] > money [ ed ] + scoreTwo [ 0 ] ) : NEW_LINE INDENT return [ money [ beg ] + scoreOne [ 0 ] , " L " + scoreOne [ 1 ] ] NEW_LINE DEDENT else : NEW_LINE INDENT return [ money [ ed ] + scoreTwo [ 0 ] , " R " + scoreTwo [ 1 ] ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( scoreOne [ 0 ] < scoreTwo [ 0 ] ) : NEW_LINE INDENT return [ scoreOne [ 0 ] , " L " + scoreOne [ 1 ] ] NEW_LINE DEDENT else : NEW_LINE INDENT return [ scoreTwo [ 0 ] , " R " + scoreTwo [ 1 ] ] NEW_LINE DEDENT DEDENT DEDENT ar = [ 10 , 80 , 90 , 30 ] NEW_LINE arraySize = len ( ar ) NEW_LINE bags = ar NEW_LINE ans = maxScore ( bags , 0 , arraySize - 1 ) NEW_LINE print ( ans [ 0 ] , ans [ 1 ] ) NEW_LINE
Minimum number of Fibonacci jumps to reach end | A Dynamic Programming based Python3 program to find minimum number of jumps to reach Destination ; Function that returns the min number of jump to reach the destination ; We consider only those Fibonacci numbers which are less than n , where we can consider fib [ 30 ] to be the upper bound as this will cross 10 ^ 5 ; DP [ i ] will be storing the minimum number of jumps required for the position i . So DP [ N + 1 ] will have the result we consider 0 as source and N + 1 as the destination ; Base case ( Steps to reach source is ) ; Initialize all table values as Infinite ; Go through each positions till destination . ; Calculate the minimum of that position if all the Fibonacci numbers are considered ; If the position is safe or the position is the destination then only we calculate the minimum otherwise the cost is MAX as default ; - 1 denotes if there is no path possible ; Driver Code
MAX = 1e9 NEW_LINE def minJumps ( arr , N ) : NEW_LINE INDENT fib = [ 0 for i in range ( 30 ) ] NEW_LINE fib [ 0 ] = 0 NEW_LINE fib [ 1 ] = 1 NEW_LINE for i in range ( 2 , 30 ) : NEW_LINE INDENT fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] NEW_LINE DEDENT DP = [ 0 for i in range ( N + 2 ) ] NEW_LINE DP [ 0 ] = 0 NEW_LINE for i in range ( 1 , N + 2 ) : NEW_LINE INDENT DP [ i ] = MAX NEW_LINE DEDENT for i in range ( 1 , N + 2 ) : NEW_LINE INDENT for j in range ( 1 , 30 ) : NEW_LINE INDENT if ( ( arr [ i - 1 ] == 1 or i == N + 1 ) and i - fib [ j ] >= 0 ) : NEW_LINE INDENT DP [ i ] = min ( DP [ i ] , 1 + DP [ i - fib [ j ] ] ) NEW_LINE DEDENT DEDENT DEDENT if ( DP [ N + 1 ] != MAX ) : NEW_LINE INDENT return DP [ N + 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT arr = [ 0 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minJumps ( arr , n - 1 ) ) NEW_LINE
Subsequence X of length K such that gcd ( X [ 0 ] , X [ 1 ] ) + ( X [ 2 ] , X [ 3 ] ) + ... is maximized | Python3 program to find the sum of the addition of all possible subsets ; Recursive function to find the maximum value of the given recurrence ; If we get K elements ; If we have reached the end and K elements are not there ; If the state has been visited ; Iterate for every element as the next possible element and take the element which gives the maximum answer ; If this element is the first element in the individual pair in the subsequence then simply recurrence with the last element as i - th index ; If this element is the second element in the individual pair , the find gcd with the previous element and add to the answer and recur for the next element ; Driver Code
from math import gcd as __gcd NEW_LINE MAX = 100 NEW_LINE def recur ( ind , cnt , last , a , n , k , dp ) : NEW_LINE INDENT if ( cnt == k ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( ind == n ) : NEW_LINE INDENT return - 10 ** 9 NEW_LINE DEDENT if ( dp [ ind ] [ cnt ] != - 1 ) : NEW_LINE INDENT return dp [ ind ] [ cnt ] NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( ind , n ) : NEW_LINE INDENT if ( cnt % 2 == 0 ) : NEW_LINE INDENT ans = max ( ans , recur ( i + 1 , cnt + 1 , i , a , n , k , dp ) ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = max ( ans , __gcd ( a [ last ] , a [ i ] ) + recur ( i + 1 , cnt + 1 , 0 , a , n , k , dp ) ) NEW_LINE DEDENT DEDENT dp [ ind ] [ cnt ] = ans NEW_LINE return ans NEW_LINE DEDENT a = [ 4 , 5 , 3 , 7 , 8 , 10 , 9 , 8 ] NEW_LINE n = len ( a ) NEW_LINE k = 4 ; NEW_LINE dp = [ [ - 1 for i in range ( MAX ) ] for i in range ( n ) ] NEW_LINE print ( recur ( 0 , 0 , 0 , a , n , k , dp ) ) NEW_LINE
Maximum number of given operations to remove the entire string | Function to return the maximum number of given operations required to remove the given entirely ; If length of the is zero ; Single operation can delete the entire string ; To store the prefix of the string which is to be deleted ; Prefix s [ 0. . i ] ; To store the subs [ i + 1. . .2 * i + 1 ] ; If the prefix s [ 0. . . i ] can be deleted ; 1 operation to remove the current prefix and then recursively find the count of operations for the subs [ i + 1. . . n - 1 ] ; Entire has to be deleted ; Driver code
def find ( s ) : NEW_LINE INDENT if ( len ( s ) == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT c = 1 NEW_LINE d = " " NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT d += s [ i ] NEW_LINE s2 = s [ i + 1 : i + 1 + len ( d ) ] NEW_LINE if ( s2 == d ) : NEW_LINE INDENT c = 1 + find ( s [ i + 1 : ] ) NEW_LINE break NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT s = " abababab " NEW_LINE print ( find ( s ) ) NEW_LINE
Sum of the distances from every node to all other nodes is maximum | Python3 program to implement the above approach ; Function to add an edge to the tree ; Function to run DFS and calculate the height of the subtree below it ; Initially initialize with 1 ; Traverse for all nodes connected to node ; If node is not parent then recall dfs ; Add the size of the subtree beneath it ; Function to assign weights to edges to maximize the final sum ; Initialize it which stores the height of the subtree beneath it ; Call the DFS function to ; Sort the given array ; Stores the number of times an edge is part of a path ; Iterate for all edges and find the number of nodes on the left and on the right ; Node 1 ; Node 2 ; If the number of nodes below is less then the other will be n - dp [ node ] ; Second condition ; Sort the number of times an edges occurs in the path ; Find the summation of all those paths and return ; Driver code ; Add an edge 1 - 2 in the tree ; Add an edge 2 - 3 in the tree ; Add an edge 3 - 4 in the tree ; Add an edge 3 - 5 in the tree ; Array which gives the edges weight to be assigned
edges = [ [ ] for i in range ( 100 ) ] NEW_LINE tree = [ [ ] for i in range ( 100 ) ] NEW_LINE def addEdge ( x , y ) : NEW_LINE INDENT edges . append ( [ x , y ] ) NEW_LINE tree [ x ] . append ( y ) NEW_LINE tree [ y ] . append ( x ) NEW_LINE DEDENT def dfs ( node , parent , dp ) : NEW_LINE INDENT dp [ node ] = 1 NEW_LINE for it in tree [ node ] : NEW_LINE INDENT if ( it != parent ) : NEW_LINE INDENT dfs ( it , node , dp ) NEW_LINE dp [ node ] += dp [ it ] NEW_LINE DEDENT DEDENT DEDENT def maximizeSum ( a , n ) : NEW_LINE INDENT dp = [ 0 for i in range ( n + 1 ) ] NEW_LINE dfs ( 1 , 0 , dp ) NEW_LINE a = sorted ( a [ : - 1 ] ) NEW_LINE ans = [ ] NEW_LINE for it in edges : NEW_LINE INDENT if len ( it ) > 0 : NEW_LINE INDENT x = it [ 0 ] NEW_LINE y = it [ 1 ] NEW_LINE if ( dp [ x ] < dp [ y ] ) : NEW_LINE INDENT fi = dp [ x ] NEW_LINE sec = n - dp [ x ] NEW_LINE ans . append ( fi * sec ) NEW_LINE DEDENT else : NEW_LINE INDENT fi = dp [ y ] NEW_LINE sec = n - dp [ y ] NEW_LINE ans . append ( fi * sec ) NEW_LINE DEDENT DEDENT DEDENT ans = sorted ( ans ) NEW_LINE res = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT res += ans [ i ] * a [ i ] NEW_LINE DEDENT return res NEW_LINE DEDENT n = 5 NEW_LINE addEdge ( 1 , 2 ) NEW_LINE addEdge ( 1 , 3 ) NEW_LINE addEdge ( 3 , 4 ) NEW_LINE addEdge ( 3 , 5 ) NEW_LINE a = [ 6 , 3 , 1 , 9 , 3 ] NEW_LINE print ( maximizeSum ( a , n ) ) NEW_LINE
Divide the array in K segments such that the sum of minimums is maximized | Python 3 program to find the sum of the minimum of all the segments ; Function to maximize the sum of the minimums ; If k segments have been divided ; If we are at the end ; If we donot reach the end then return a negative number that cannot be the sum ; If at the end but k segments are not formed ; If the state has been visited already ; If the state has not been visited ; Get the minimum element in the segment ; Iterate and try to break at every index and create a segment ; Find the minimum element in the segment ; Find the sum of all the segments trying all the possible combinations ; Return the answer by memoizing it ; Driver Code ; Initialize dp array with - 1
MAX = 10 NEW_LINE def maximizeSum ( a , n , ind , k , dp ) : NEW_LINE INDENT if ( k == 0 ) : NEW_LINE INDENT if ( ind == n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return - 1e9 NEW_LINE DEDENT DEDENT elif ( ind == n ) : NEW_LINE INDENT return - 1e9 NEW_LINE DEDENT elif ( dp [ ind ] [ k ] != - 1 ) : NEW_LINE INDENT return dp [ ind ] [ k ] NEW_LINE DEDENT else : NEW_LINE INDENT ans = 0 NEW_LINE mini = a [ ind ] NEW_LINE for i in range ( ind , n , 1 ) : NEW_LINE INDENT mini = min ( mini , a [ i ] ) NEW_LINE ans = max ( ans , maximizeSum ( \ a , n , i + 1 , k - 1 , dp ) + mini ) NEW_LINE DEDENT dp [ ind ] [ k ] = ans NEW_LINE return ans NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 5 , 7 , 4 , 2 , 1 , 6 ] NEW_LINE k = 3 NEW_LINE n = len ( a ) NEW_LINE dp = [ [ - 1 for i in range ( MAX ) ] \ for j in range ( MAX ) ] NEW_LINE print ( maximizeSum ( a , n , 0 , k , dp ) ) NEW_LINE DEDENT
Optimally accommodate 0 s and 1 s from a Binary String into K buckets | Function to find the minimum required sum using dynamic programming ; If both start and bucket reached end then return 0 else that arrangement is not possible so return INT_MAX ; Corner case ; If state if already calculated then return its answer ; Start filling zeroes and ones which to be accommodated in jth bucket then ans for current arrangement will be ones * zeroes + recur ( i + 1 , bucket + 1 ) ; If this arrangement is not possible then don 't calculate further ; Function to initialize the dp and call solveUtil ( ) method to get the answer ; Start with 0 - th index and 1 bucket ; Driver code ; K buckets
def solveUtil ( start , bucket , str1 , K , dp ) : NEW_LINE INDENT N = len ( str1 ) NEW_LINE if ( start == N ) : NEW_LINE INDENT if ( bucket == K ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return 10 ** 9 NEW_LINE DEDENT if ( bucket == K ) : NEW_LINE INDENT return 10 ** 9 NEW_LINE DEDENT if ( dp [ start ] [ bucket ] != - 1 ) : NEW_LINE INDENT return dp [ start ] [ bucket ] NEW_LINE DEDENT zeroes = 0 NEW_LINE ones = 0 NEW_LINE ans = 10 ** 9 NEW_LINE for i in range ( start , N ) : NEW_LINE INDENT if ( str1 [ i ] == '1' ) : NEW_LINE INDENT ones += 1 NEW_LINE DEDENT else : NEW_LINE INDENT zeroes += 1 NEW_LINE DEDENT if ( ones * zeroes > ans ) : NEW_LINE INDENT break NEW_LINE DEDENT temp = solveUtil ( i + 1 , bucket + 1 , str1 , K , dp ) NEW_LINE if ( temp != 10 ** 9 ) : NEW_LINE INDENT ans = min ( ans , temp + ( ones * zeroes ) ) NEW_LINE DEDENT DEDENT dp [ start ] [ bucket ] = ans NEW_LINE return ans NEW_LINE DEDENT def solve ( str1 , K ) : NEW_LINE INDENT N = len ( str1 ) NEW_LINE dp = [ [ - 1 for i in range ( K ) ] for i in range ( N ) ] NEW_LINE ans = solveUtil ( 0 , 0 , str1 , K , dp ) NEW_LINE if ans == 10 ** 9 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return ans NEW_LINE DEDENT DEDENT s = "0101" NEW_LINE S = [ i for i in s ] NEW_LINE K = 2 NEW_LINE print ( solve ( S , K ) ) NEW_LINE
Probability of getting more heads than tails when N biased coins are tossed | Python3 implementation of the above approach ; Function to return the probability when number of heads is greater than the number of tails ; Declaring the DP table ; Base case ; Iterating for every coin ; j represents the numbers of heads ; If number of heads is equal to zero there there is only one possibility ; When the number of heads is greater than ( n + 1 ) / 2 it means that heads are greater than tails as no of tails + no of heads is equal to n for any permutation of heads and tails ; Driver Code ; 1 based indexing ; Number of coins ; Function call
import numpy as np NEW_LINE def Probability ( p , n ) : NEW_LINE INDENT dp = np . zeros ( ( n + 1 , n + 1 ) ) ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = 0.0 NEW_LINE DEDENT DEDENT dp [ 0 ] [ 0 ] = 1.0 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( i + 1 ) : NEW_LINE INDENT if ( j == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] * ( 1.0 - p [ i ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = ( dp [ i - 1 ] [ j ] * ( 1.0 - p [ i ] ) + dp [ i - 1 ] [ j - 1 ] * p [ i ] ) ; NEW_LINE DEDENT DEDENT DEDENT ans = 0.0 ; NEW_LINE for i in range ( ( n + 1 ) // 2 , n + 1 ) : NEW_LINE INDENT ans += dp [ n ] [ i ] ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT p = [ 0.0 , 0.3 , 0.4 , 0.7 ] ; NEW_LINE n = len ( p ) - 1 ; NEW_LINE print ( Probability ( p , n ) ) ; NEW_LINE DEDENT
Number of ways to get a given sum with n number of m | Python3 function to calculate the number of ways to achieve sum x in n no of throws ; Function to calculate recursively the number of ways to get sum in given throws and [ 1. . m ] values ; Base condition 1 ; Base condition 2 ; If value already calculated dont move into re - computation ; Recursively moving for sum - i in throws - 1 no of throws left ; Inserting present values in dp ; Driver function
import numpy as np NEW_LINE mod = 1000000007 ; NEW_LINE dp = np . zeros ( ( 55 , 55 ) ) ; NEW_LINE def NoofWays ( face , throws , sum ) : NEW_LINE INDENT if ( sum == 0 and throws == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT if ( sum < 0 or throws == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( dp [ throws ] [ sum ] != - 1 ) : NEW_LINE INDENT return dp [ throws ] [ sum ] ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for i in range ( 1 , face + 1 ) : NEW_LINE INDENT ans += NoofWays ( face , throws - 1 , sum - i ) ; NEW_LINE DEDENT dp [ throws ] [ sum ] = ans ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT faces = 6 ; throws = 3 ; sum = 12 ; NEW_LINE for i in range ( 55 ) : NEW_LINE INDENT for j in range ( 55 ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT print ( NoofWays ( faces , throws , sum ) ) ; NEW_LINE DEDENT
Longest sub | Function to return the length of the longest required sub - sequence ; Sort the array ; To store the resultant length ; If array contains only one element then it divides itself ; Every element divides itself ; Count for all numbers which are lesser than a [ i ] ; If a [ i ] % a [ j ] then update the maximum subsequence length , dp [ i ] = max ( dp [ i ] , 1 + dp [ j ] ) where j is in the range [ 0 , i - 1 ] ; If array contains only one element then i = j which doesn 't satisfy the condition ; Driver code
def find ( n , a ) : NEW_LINE INDENT a . sort ( ) ; NEW_LINE res = 1 ; NEW_LINE dp = [ 0 ] * n ; NEW_LINE dp [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT dp [ i ] = 1 ; NEW_LINE for j in range ( i ) : NEW_LINE INDENT if ( a [ i ] % a [ j ] == 0 ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , 1 + dp [ j ] ) ; NEW_LINE DEDENT DEDENT res = max ( res , dp [ i ] ) ; NEW_LINE DEDENT if ( res == 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return res ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 4 , 6 , 1 , 3 , 11 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( find ( n , a ) ) ; NEW_LINE DEDENT
Maximize the happiness of the groups on the Trip | Python3 implementation of the approach ; Function to return the maximized happiness ; Two arrays similar to 0 1 knapsack problem ; To store the happiness of the current group ; Current person is a child ; Woman ; Man ; Old person ; Group 's happiness is the sum of happiness  of the people in the group multiplied by  the number of people ; Solution using 0 1 knapsack ; Driver code ; Number of seats ; Groups
import numpy as np NEW_LINE def MaxHappiness ( A , N , v ) : NEW_LINE INDENT val = [ 0 ] * N ; wt = [ 0 ] * N ; c = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT string = v [ i ] ; NEW_LINE c = 0 ; NEW_LINE for j in range ( len ( string ) ) : NEW_LINE INDENT if ( string [ j ] == ' c ' ) : NEW_LINE INDENT c += 4 ; NEW_LINE DEDENT elif ( string [ j ] == ' w ' ) : NEW_LINE INDENT c += 3 ; NEW_LINE DEDENT elif ( string [ j ] == ' m ' ) : NEW_LINE INDENT c += 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT c += 1 ; NEW_LINE DEDENT DEDENT c *= len ( string ) ; NEW_LINE val [ i ] = c ; NEW_LINE wt [ i ] = len ( string ) ; NEW_LINE DEDENT k = np . zeros ( ( N + 1 , A + 1 ) ) NEW_LINE for i in range ( N + 1 ) : NEW_LINE INDENT for w in range ( A + 1 ) : NEW_LINE INDENT if ( i == 0 or w == 0 ) : NEW_LINE INDENT k [ i ] [ w ] = 0 ; NEW_LINE DEDENT elif ( wt [ i - 1 ] <= w ) : NEW_LINE INDENT k [ i ] [ w ] = max ( val [ i - 1 ] + k [ i - 1 ] [ w - wt [ i - 1 ] ] , k [ i - 1 ] [ w ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT k [ i ] [ w ] = k [ i - 1 ] [ w ] ; NEW_LINE DEDENT DEDENT DEDENT return k [ N ] [ A ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 5 ; NEW_LINE v = [ " mmo " , " oo " , " cmw " , " cc " , " c " ] ; NEW_LINE N = len ( v ) ; NEW_LINE print ( MaxHappiness ( A , N , v ) ) ; NEW_LINE DEDENT
0 | Python3 implementation of the approach ; To store states of DP ; To check if a state has been solved ; Function to compute the states ; Base case ; Check if a state has been solved ; Setting a state as solved ; Returning the solved state ; Function to pre - compute the states dp [ 0 ] [ 0 ] , dp [ 0 ] [ 1 ] , . . , dp [ 0 ] [ C_MAX ] ; Function to answer a query in O ( 1 ) ; Driver code ; Performing required pre - computation ; Perform queries
import numpy as np NEW_LINE import sys NEW_LINE C_MAX = 30 NEW_LINE max_arr_len = 10 NEW_LINE dp = np . zeros ( ( max_arr_len , C_MAX + 1 ) ) ; NEW_LINE v = np . zeros ( ( max_arr_len , C_MAX + 1 ) ) ; NEW_LINE INT_MIN = - ( sys . maxsize ) + 1 NEW_LINE def findMax ( i , r , w , n ) : NEW_LINE INDENT if ( r < 0 ) : NEW_LINE INDENT return INT_MIN ; NEW_LINE DEDENT if ( i == n ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( v [ i ] [ r ] ) : NEW_LINE INDENT return dp [ i ] [ r ] ; NEW_LINE DEDENT v [ i ] [ r ] = 1 ; NEW_LINE dp [ i ] [ r ] = max ( w [ i ] + findMax ( i + 1 , r - w [ i ] , w , n ) , findMax ( i + 1 , r , w , n ) ) ; NEW_LINE return dp [ i ] [ r ] ; NEW_LINE DEDENT def preCompute ( w , n ) : NEW_LINE INDENT for i in range ( C_MAX , - 1 , - 1 ) : NEW_LINE INDENT findMax ( 0 , i , w , n ) ; NEW_LINE DEDENT DEDENT def ansQuery ( w ) : NEW_LINE INDENT return dp [ 0 ] [ w ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT w = [ 3 , 8 , 9 ] ; NEW_LINE n = len ( w ) NEW_LINE preCompute ( w , n ) ; NEW_LINE queries = [ 11 , 10 , 4 ] ; NEW_LINE q = len ( queries ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( ansQuery ( queries [ i ] ) ) ; NEW_LINE DEDENT DEDENT