codeparrot/xlcost-text-to-code huggingface dataset:
Find minimum time to finish all jobs with given constraints | Utility function to get maximum element in job [ 0. . n - 1 ] ; Returns true if it is possible to finish jobs [ ] within given time 'time ; cnt is count of current assignees required for jobs ; time assigned to current assignee ; If time assigned to current assignee exceeds max , increment count of assignees . ; Else add time of job to current time and move to next job . ; Returns true if count is smaller than k ; Returns minimum time required to finish given array of jobs k -- > number of assignees T -- > Time required by every assignee to finish 1 unit m -- > Number of jobs ; Set start and end for binary search end provides an upper limit on time ; Find the job that takes maximum time ; Do binary search for minimum feasible time ; If it is possible to finish jobs in mid time ; ans = min ( ans , mid ) Update answer ; Driver program
def getMax ( arr , n ) : NEW_LINE INDENT result = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if arr [ i ] > result : NEW_LINE INDENT result = arr [ i ] NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT ' NEW_LINE def isPossible ( time , K , job , n ) : NEW_LINE INDENT cnt = 1 NEW_LINE curr_time = 0 NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT if curr_time + job [ i ] > time : NEW_LINE INDENT curr_time = 0 NEW_LINE cnt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT curr_time += job [ i ] NEW_LINE i += 1 NEW_LINE DEDENT DEDENT return cnt <= K NEW_LINE DEDENT def findMinTime ( K , T , job , n ) : NEW_LINE INDENT end = 0 NEW_LINE start = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT end += job [ i ] NEW_LINE DEDENT job_max = getMax ( job , n ) NEW_LINE while start <= end : NEW_LINE INDENT mid = int ( ( start + end ) / 2 ) NEW_LINE if mid >= job_max and isPossible ( mid , K , job , n ) : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT DEDENT return ans * T NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT job = [ 10 , 7 , 8 , 12 , 6 , 8 ] NEW_LINE n = len ( job ) NEW_LINE k = 4 NEW_LINE T = 5 NEW_LINE print ( findMinTime ( k , T , job , n ) ) NEW_LINE DEDENT
Longest subarray with all even or all odd elements | Function to calculate longest substring with odd or even elements ; Initializing dp [ ] ; Initializing dp [ 0 ] with 1 ; ans will store the final answer ; Traversing the array from index 1 to N - 1 ; Checking both current and previous element is even or odd ; Updating dp [ i ] with dp [ i - 1 ] + 1 ; Storing max element to ans ; Returning the final answer ; Driver Code ; Input ; Function call
def LongestOddEvenSubarray ( A , N ) : NEW_LINE INDENT dp = [ 0 for i in range ( N ) ] NEW_LINE dp [ 0 ] = 1 NEW_LINE ans = 1 NEW_LINE for i in range ( 1 , N , 1 ) : NEW_LINE INDENT if ( ( A [ i ] % 2 == 0 and A [ i - 1 ] % 2 == 0 ) or ( A [ i ] % 2 != 0 and A [ i - 1 ] % 2 != 0 ) ) : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT ans = max ( ans , dp [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 2 , 5 , 7 , 2 , 4 , 6 , 8 , 3 ] NEW_LINE N = len ( A ) NEW_LINE print ( LongestOddEvenSubarray ( A , N ) ) NEW_LINE DEDENT
Count of subarrays with maximum value as K | Function to count the subarrays with maximum not greater than K ; If arr [ i ] > k then arr [ i ] cannot be a part of any subarray . ; Count the number of elements where arr [ i ] is not greater than k . ; Summation of all possible subarrays in the variable ans . ; Function to count the subarrays with maximum value is equal to K ; Stores count of subarrays with max <= k - 1. ; Stores count of subarrays with max >= k + 1. ; Stores count of subarrays with max = k . ; Driver Code ; Given Input ; Function Call
def totalSubarrays ( arr , n , k ) : NEW_LINE INDENT ans = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( arr [ i ] > k ) : NEW_LINE INDENT i += 1 NEW_LINE continue NEW_LINE DEDENT count = 0 NEW_LINE while ( i < n and arr [ i ] <= k ) : NEW_LINE INDENT i += 1 NEW_LINE count += 1 NEW_LINE DEDENT ans += ( ( count * ( count + 1 ) ) // 2 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def countSubarrays ( arr , n , k ) : NEW_LINE INDENT count1 = totalSubarrays ( arr , n , k - 1 ) NEW_LINE count2 = totalSubarrays ( arr , n , k ) NEW_LINE ans = count2 - count1 NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE k = 3 NEW_LINE arr = [ 2 , 1 , 3 , 4 ] NEW_LINE print ( countSubarrays ( arr , n , k ) ) NEW_LINE DEDENT
Maximum subsequence sum such that no three are consecutive in O ( 1 ) space | Function to calculate the maximum subsequence sum such that no three elements are consecutive ; when N is 1 , answer would be the only element present ; when N is 2 , answer would be sum of elements ; variable to store sum up to i - 3 ; variable to store sum up to i - 2 ; variable to store sum up to i - 1 ; variable to store the final sum of the subsequence ; find the maximum subsequence sum up to index i ; update first , second and third ; return ans ; ; Driver code ; Input ; Function call
def maxSumWO3Consec ( A , N ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT return A [ 0 ] NEW_LINE DEDENT if ( N == 2 ) : NEW_LINE INDENT return A [ 0 ] + A [ 1 ] NEW_LINE DEDENT third = A [ 0 ] NEW_LINE second = third + A [ 1 ] NEW_LINE first = max ( second , A [ 1 ] + A [ 2 ] ) NEW_LINE sum = max ( max ( third , second ) , first ) NEW_LINE for i in range ( 3 , N , 1 ) : NEW_LINE INDENT sum = max ( max ( first , second + A [ i ] ) , third + A [ i ] + A [ i - 1 ] ) NEW_LINE third = second NEW_LINE second = first NEW_LINE first = sum NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 3000 , 2000 , 1000 , 3 , 10 ] NEW_LINE N = len ( A ) NEW_LINE print ( maxSumWO3Consec ( A , N ) ) NEW_LINE DEDENT
Minimum number of given operations required to reduce a number to 2 | Function to find the minimum number of operations required to reduce n to 2 ; Initialize a dp array ; Handle the base case ; Iterate in the range [ 2 , n ] ; Check if i * 5 <= n ; Check if i + 3 <= n ; Return the result ; Driver code ; Given Input ; Function Call ; Print the result
def findMinOperations ( n ) : NEW_LINE INDENT dp = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT dp [ i ] = 999999 NEW_LINE DEDENT dp [ 2 ] = 0 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( i * 5 <= n ) : NEW_LINE INDENT dp [ i * 5 ] = min ( dp [ i * 5 ] , dp [ i ] + 1 ) NEW_LINE DEDENT if ( i + 3 <= n ) : NEW_LINE INDENT dp [ i + 3 ] = min ( dp [ i + 3 ] , dp [ i ] + 1 ) NEW_LINE DEDENT DEDENT return dp [ n ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 28 NEW_LINE m = findMinOperations ( n ) NEW_LINE if ( m != 9999 ) : NEW_LINE INDENT print ( m ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT
Split array into subarrays such that sum of difference between their maximums and minimums is maximum | Function to find maximum sum of difference between maximums and minimums in the splitted subarrays ; Traverse the array ; Stores the maximum and minimum elements upto the i - th index ; Traverse the range [ 0 , i ] ; Update the minimum ; Update the maximum ; Update dp [ i ] ; Return the maximum sum of difference ; Driver code
def getValue ( arr , N ) : NEW_LINE INDENT dp = [ 0 for i in range ( N ) ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT minn = arr [ i ] NEW_LINE maxx = arr [ i ] NEW_LINE j = i NEW_LINE while ( j >= 0 ) : NEW_LINE INDENT minn = min ( arr [ j ] , minn ) NEW_LINE maxx = max ( arr [ j ] , maxx ) NEW_LINE dp [ i ] = max ( dp [ i ] , maxx - minn + ( dp [ j - 1 ] if ( j >= 1 ) else 0 ) ) NEW_LINE j -= 1 NEW_LINE DEDENT DEDENT return dp [ N - 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 8 , 1 , 7 , 9 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE print ( getValue ( arr , N ) ) NEW_LINE DEDENT
Maximum score possible by removing substrings made up of single distinct character | Function to check if the string s consists of a single distinct character or not ; Function to calculate the maximum score possible by removing substrings ; If string is empty ; If length of string is 1 ; Store the maximum result ; Try to remove all substrings that satisfy the condition and check for resultant string after removal ; Store the substring { s [ i ] , . . , s [ j ] } ; Check if the substring contains only a single distinct character ; Return the maximum score ; Driver Code
def isUnique ( s ) : NEW_LINE INDENT return True if len ( set ( s ) ) == 1 else False NEW_LINE DEDENT def maxScore ( s , a ) : NEW_LINE INDENT n = len ( s ) NEW_LINE if n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT if n == 1 : NEW_LINE INDENT return a [ 0 ] NEW_LINE DEDENT mx = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT sub = s [ i : j + 1 ] NEW_LINE if isUnique ( sub ) : NEW_LINE INDENT mx = max ( mx , a [ len ( sub ) - 1 ] + maxScore ( s [ : i ] + s [ j + 1 : ] , a ) ) NEW_LINE DEDENT DEDENT DEDENT return mx NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "011" NEW_LINE a = [ 1 , 3 , 1 ] NEW_LINE print ( maxScore ( s , a ) ) NEW_LINE DEDENT
Maximum score possible by removing substrings made up of single distinct character | Initialize a dictionary to store the precomputed results ; Function to calculate the maximum score possible by removing substrings ; If s is present in dp [ ] array ; Base Cases : ; If length of string is 0 ; If length of string is 1 ; Put head pointer at start ; Initialize the max variable ; Generate the substrings using two pointers ; If s [ head ] and s [ tail ] are different ; Move head to tail and break ; Store the substring ; Update the maximum ; Move the tail ; Store the score ; Driver Code
dp = dict ( ) NEW_LINE def maxScore ( s , a ) : NEW_LINE INDENT if s in dp : NEW_LINE INDENT return dp [ s ] NEW_LINE DEDENT n = len ( s ) NEW_LINE if n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT if n == 1 : NEW_LINE INDENT return a [ 0 ] NEW_LINE DEDENT head = 0 NEW_LINE mx = - 1 NEW_LINE while head < n : NEW_LINE INDENT tail = head NEW_LINE while tail < n : NEW_LINE INDENT if s [ tail ] != s [ head ] : NEW_LINE INDENT head = tail NEW_LINE break NEW_LINE DEDENT sub = s [ head : tail + 1 ] NEW_LINE mx = max ( mx , a [ len ( sub ) - 1 ] + maxScore ( s [ : head ] + s [ tail + 1 : ] , a ) ) NEW_LINE tail += 1 NEW_LINE DEDENT if tail == n : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT dp [ s ] = mx NEW_LINE return mx NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abb " NEW_LINE a = [ 1 , 3 , 1 ] NEW_LINE print ( maxScore ( s , a ) ) NEW_LINE DEDENT
Count all unique outcomes possible by performing S flips on N coins | Dimensions of the DP table ; Stores the dp states ; Function to recursively count the number of unique outcomes possible by performing S flips on N coins ; Base Case ; If the count for the current state is not calculated , then calculate it recursively ; Otherwise return the already calculated value ; Driver Code
size = 100 NEW_LINE ans = [ [ 0 for i in range ( size ) ] for j in range ( size ) ] NEW_LINE def numberOfUniqueOutcomes ( n , s ) : NEW_LINE INDENT if ( s < n ) : NEW_LINE INDENT ans [ n ] [ s ] = 0 ; NEW_LINE DEDENT elif ( n == 1 or n == s ) : NEW_LINE INDENT ans [ n ] [ s ] = 1 ; NEW_LINE DEDENT elif ( ans [ n ] [ s ] == 0 ) : NEW_LINE INDENT ans [ n ] [ s ] = ( numberOfUniqueOutcomes ( n - 1 , s - 1 ) + numberOfUniqueOutcomes ( n - 1 , s - 2 ) ) NEW_LINE DEDENT return ans [ n ] [ s ] ; NEW_LINE DEDENT N = 5 NEW_LINE S = 8 NEW_LINE print ( numberOfUniqueOutcomes ( N , S ) ) NEW_LINE
Minimize removals to remove another string as a subsequence of a given string | Function to print the minimum number of character removals required to remove X as a subsequence from the string str ; Length of the string str ; Length of the string X ; Stores the dp states ; Fill first row of dp [ ] [ ] ; If X [ j ] matches with str [ 0 ] ; If s [ i ] is equal to X [ j ] ; Update state after removing str [ i [ ; Update state after keeping str [ i ] ; If str [ i ] is not equal to X [ j ] ; Print the minimum number of characters removals ; Driver Code ; Input ; Function call to get minimum number of character removals
def printMinimumRemovals ( s , X ) : NEW_LINE INDENT N = len ( s ) NEW_LINE M = len ( X ) NEW_LINE dp = [ [ 0 for x in range ( M ) ] for y in range ( N ) ] NEW_LINE for j in range ( M ) : NEW_LINE INDENT if ( s [ 0 ] == X [ j ] ) : NEW_LINE INDENT dp [ 0 ] [ j ] = 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( s [ i ] == X [ j ] ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + 1 NEW_LINE if ( j != 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = min ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT print ( dp [ N - 1 ] [ M - 1 ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " btagd " NEW_LINE X = " bad " NEW_LINE printMinimumRemovals ( s , X ) NEW_LINE DEDENT
Railway Station | TCS CodeVita 2020 | Dp table for memoization ; Function to count the number of ways to N - th station ; Base Cases ; If current state is already evaluated ; Count ways in which train 1 can be chosen ; Count ways in which train 2 can be chosen ; Count ways in which train 3 can be chosen ; Store the current state ; Return the number of ways ; Driver Code ; Given Input ; Function call to count the number of ways to reach the n - th station
dp = [ - 1 for i in range ( 100000 ) ] NEW_LINE def findWays ( x ) : NEW_LINE INDENT if ( x < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( x == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( x == 1 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT if ( x == 2 ) : NEW_LINE INDENT return 4 NEW_LINE DEDENT if ( dp [ x ] != - 1 ) : NEW_LINE INDENT return dp [ x ] NEW_LINE DEDENT count = findWays ( x - 1 ) NEW_LINE count += findWays ( x - 2 ) NEW_LINE count += findWays ( x - 3 ) NEW_LINE dp [ x ] = count NEW_LINE return dp [ x ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE print ( findWays ( n ) ) NEW_LINE DEDENT
Maximum sum not exceeding K possible for any rectangle of a Matrix | Python3 program for the above approach ; Function to find the maximum possible sum of arectangle which is less than K ; Stores the values ( cum_sum - K ) ; Insert 0 into the set sumSet ; Traverse over the rows ; Get cumulative sum from [ 0 to i ] ; Search for upperbound of ( cSum - K ) in the hashmap ; If upper_bound of ( cSum - K ) exists , then update max sum ; Insert cummulative value in the hashmap ; Return the maximum sum which is less than K ; Function to find the maximum sum of rectangle such that its sum is no larger than K ; Stores the number of rows and columns ; Store the required result ; Set the left column ; Set the right column for the left column set by outer loop ; Calculate sum between the current left and right for every row ; Stores the sum of rectangle ; Update the overall maximum sum ; Print the result ; Driver Code ; Function Call
from bisect import bisect_left , bisect_right NEW_LINE import sys NEW_LINE def maxSubarraySum ( sum , k , row ) : NEW_LINE INDENT curSum , curMax = 0 , - sys . maxsize - 1 NEW_LINE sumSet = { } NEW_LINE sumSet [ 0 ] = 1 NEW_LINE for r in range ( row ) : NEW_LINE INDENT curSum += sum [ r ] NEW_LINE arr = list ( sumSet . keys ( ) ) NEW_LINE it = bisect_left ( arr , curSum - k ) NEW_LINE if ( it != len ( arr ) ) : NEW_LINE INDENT curMax = max ( curMax , curSum - it ) NEW_LINE DEDENT sumSet [ curSum ] = 1 NEW_LINE DEDENT return curMax NEW_LINE DEDENT def maxSumSubmatrix ( matrix , k ) : NEW_LINE INDENT row = len ( matrix ) NEW_LINE col = len ( matrix [ 0 ] ) NEW_LINE ret = - sys . maxsize - 1 NEW_LINE for i in range ( col ) : NEW_LINE INDENT sum = [ 0 ] * ( row ) NEW_LINE for j in range ( i , col ) : NEW_LINE INDENT for r in range ( row ) : NEW_LINE INDENT sum [ r ] += matrix [ r ] [ j ] NEW_LINE DEDENT curMax = maxSubarraySum ( sum , k , row ) NEW_LINE ret = max ( ret , curMax ) NEW_LINE DEDENT DEDENT print ( ret ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 1 , 0 , 1 ] , [ 0 , - 2 , 3 ] ] NEW_LINE K = 2 NEW_LINE maxSumSubmatrix ( matrix , K ) NEW_LINE DEDENT
Count N | Function to count binary strings of length N having substring "11" ; Initialize dp [ ] of size N + 1 ; Base Cases ; Stores the first N powers of 2 ; Generate ; Iterate over the range [ 2 , N ] ; Prtotal count of substrings ; Driver Code
def binaryStrings ( N ) : NEW_LINE INDENT dp = [ 0 ] * ( N + 1 ) NEW_LINE dp [ 0 ] = 0 NEW_LINE dp [ 1 ] = 0 NEW_LINE power = [ 0 ] * ( N + 1 ) NEW_LINE power [ 0 ] = 1 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT power [ i ] = 2 * power [ i - 1 ] NEW_LINE DEDENT for i in range ( 2 , N + 1 ) : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + power [ i - 2 ] NEW_LINE DEDENT print ( dp [ N ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 12 NEW_LINE binaryStrings ( N ) NEW_LINE DEDENT
Maximum sum submatrix | Python3 program for the above approach ; Function to find maximum continuous maximum sum in the array ; Stores current and maximum sum ; Traverse the array v ; Add the value of the current element ; Update the maximum sum ; Return the maximum sum ; Function to find the maximum submatrix sum ; Store the rows and columns of the matrix ; Create an auxiliary matrix Traverse the matrix , prefix and initialize it will all 0 s ; Calculate prefix sum of all rows of matrix A [ ] [ ] and store in matrix prefix [ ] ; Update the prefix [ ] [ ] ; Store the maximum submatrix sum ; Iterate for starting column ; Iterate for last column ; To store current array elements ; Traverse every row ; Store the sum of the kth row ; Update the prefix sum ; Push it in a vector ; Update the maximum overall sum ; Print the answer ; Driver Code ; Function Call
import sys NEW_LINE def kadane ( v ) : NEW_LINE INDENT currSum = 0 NEW_LINE maxSum = - sys . maxsize - 1 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT currSum += v [ i ] NEW_LINE if ( currSum > maxSum ) : NEW_LINE INDENT maxSum = currSum NEW_LINE DEDENT if ( currSum < 0 ) : NEW_LINE INDENT currSum = 0 NEW_LINE DEDENT DEDENT return maxSum NEW_LINE DEDENT def maxSubmatrixSum ( A ) : NEW_LINE INDENT r = len ( A ) NEW_LINE c = len ( A [ 0 ] ) NEW_LINE prefix = [ [ 0 for i in range ( c ) ] for j in range ( r ) ] NEW_LINE for i in range ( r ) : NEW_LINE INDENT for j in range ( c ) : NEW_LINE INDENT if ( j == 0 ) : NEW_LINE INDENT prefix [ i ] [ j ] = A [ i ] [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT prefix [ i ] [ j ] = A [ i ] [ j ] + prefix [ i ] [ j - 1 ] NEW_LINE DEDENT DEDENT DEDENT maxSum = - sys . maxsize - 1 NEW_LINE for i in range ( c ) : NEW_LINE INDENT for j in range ( i , c ) : NEW_LINE INDENT v = [ ] NEW_LINE for k in range ( r ) : NEW_LINE INDENT el = 0 NEW_LINE if ( i == 0 ) : NEW_LINE INDENT el = prefix [ k ] [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT el = prefix [ k ] [ j ] - prefix [ k ] [ i - 1 ] NEW_LINE DEDENT v . append ( el ) NEW_LINE DEDENT maxSum = max ( maxSum , kadane ( v ) ) NEW_LINE DEDENT DEDENT print ( maxSum ) NEW_LINE DEDENT matrix = [ [ 0 , - 2 , - 7 , 0 ] , [ 9 , 2 , - 6 , 2 ] , [ - 4 , 1 , - 4 , 1 ] , [ - 1 , 8 , 0 , - 2 ] ] NEW_LINE maxSubmatrixSum ( matrix ) NEW_LINE
Minimize cost of painting N houses such that adjacent houses have different colors | Function to find the minimum cost of coloring the houses such that no two adjacent houses has the same color ; Corner Case ; Auxiliary 2D dp array ; Base Case ; If current house is colored with red the take min cost of previous houses colored with ( blue and green ) ; If current house is colored with blue the take min cost of previous houses colored with ( red and green ) ; If current house is colored with green the take min cost of previous houses colored with ( red and blue ) ; Print the min cost of the last painted house ; Driver Code ; Function Call
def minCost ( costs , N ) : NEW_LINE INDENT if ( N == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT dp = [ [ 0 for i in range ( 3 ) ] for j in range ( 3 ) ] NEW_LINE dp [ 0 ] [ 0 ] = costs [ 0 ] [ 0 ] NEW_LINE dp [ 0 ] [ 1 ] = costs [ 0 ] [ 1 ] NEW_LINE dp [ 0 ] [ 2 ] = costs [ 0 ] [ 2 ] NEW_LINE for i in range ( 1 , N , 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = min ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 2 ] ) + costs [ i ] [ 0 ] NEW_LINE dp [ i ] [ 1 ] = min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 2 ] ) + costs [ i ] [ 1 ] NEW_LINE dp [ i ] [ 2 ] = min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + costs [ i ] [ 2 ] NEW_LINE DEDENT print ( min ( dp [ N - 1 ] [ 0 ] , min ( dp [ N - 1 ] [ 1 ] , dp [ N - 1 ] [ 2 ] ) ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT costs = [ [ 14 , 2 , 11 ] , [ 11 , 14 , 5 ] , [ 14 , 3 , 10 ] ] NEW_LINE N = len ( costs ) NEW_LINE minCost ( costs , N ) NEW_LINE DEDENT
Minimize count of flips required to make sum of the given array equal to 0 | Initialize dp [ ] [ ] ; Function to find the minimum number of operations to make sum of A [ ] 0 ; Initialize answer ; Base case ; Otherwise , return 0 ; Pre - computed subproblem ; Recurrence relation for finding the minimum of the sum of subsets ; Return the result ; Function to find the minimum number of elements required to be flipped to amke sum the array equal to 0 ; Find the sum of array ; Initialise dp [ ] [ ] with - 1 ; No solution exists ; Otherwise ; If sum is odd , no subset is possible ; Driver Code ; Function Call
dp = [ [ - 1 for i in range ( 2001 ) ] for j in range ( 2001 ) ] NEW_LINE def solve ( A , i , sum , N ) : NEW_LINE INDENT res = 2001 NEW_LINE if ( sum < 0 or ( i == N and sum != 0 ) ) : NEW_LINE INDENT return 2001 NEW_LINE DEDENT if ( sum == 0 or i >= N ) : NEW_LINE INDENT dp [ i ] [ sum ] = 0 NEW_LINE return 0 NEW_LINE DEDENT if ( dp [ i ] [ sum ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ sum ] NEW_LINE DEDENT res = min ( solve ( A , i + 1 , sum - A [ i ] , N ) + 1 , solve ( A , i + 1 , sum , N ) ) NEW_LINE dp [ i ] [ sum ] = res NEW_LINE return res NEW_LINE DEDENT def minOp ( A , N ) : NEW_LINE INDENT sum = 0 NEW_LINE for it in A : NEW_LINE INDENT sum += it NEW_LINE DEDENT if ( sum % 2 == 0 ) : NEW_LINE INDENT dp = [ [ - 1 for i in range ( 2001 ) ] for j in range ( 2001 ) ] NEW_LINE ans = solve ( A , 0 , sum // 2 , N ) NEW_LINE if ( ans < 0 or ans > N ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ans ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT A = [ 2 , 3 , 1 , 4 ] NEW_LINE N = len ( A ) NEW_LINE minOp ( A , N ) NEW_LINE
Count subsequences having average of its elements equal to K | Stores the dp states ; Function to find the count of subsequences having average K ; Base condition ; Three loops for three states ; Recurrence relation ; Stores the sum of dp [ n ] [ j ] [ K * j ] all possible values of j with average K and sum K * j ; Iterate over the range [ 1 , N ] ; Return the final count ; Driver Code
dp = [ [ [ 0 for i in range ( 1001 ) ] for i in range ( 101 ) ] for i in range ( 101 ) ] NEW_LINE def countAverage ( n , K , arr ) : NEW_LINE INDENT global dp NEW_LINE dp [ 0 ] [ 0 ] [ 0 ] = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for k in range ( n ) : NEW_LINE INDENT for s in range ( 100 ) : NEW_LINE INDENT dp [ i + 1 ] [ k + 1 ] [ s + arr [ i ] ] += dp [ i ] [ k ] [ s ] NEW_LINE dp [ i + 1 ] [ k ] [ s ] += dp [ i ] [ k ] [ s ] NEW_LINE DEDENT DEDENT DEDENT cnt = 0 NEW_LINE for j in range ( 1 , n + 1 ) : NEW_LINE INDENT cnt += dp [ n ] [ j ] [ K * j ] NEW_LINE DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 9 , 7 , 8 , 9 ] NEW_LINE K = 8 NEW_LINE N = len ( arr ) NEW_LINE print ( countAverage ( N , K , arr ) ) NEW_LINE DEDENT