text
stringlengths
1
636
code
stringlengths
8
1.89k
Stores the number of rows and columns
row = len ( matrix ) NEW_LINE col = len ( matrix [ 0 ] ) NEW_LINE
Store the required result
ret = - sys . maxsize - 1 NEW_LINE
Set the left column
for i in range ( col ) : NEW_LINE INDENT sum = [ 0 ] * ( row ) NEW_LINE DEDENT
Set the right column for the left column set by outer loop
for j in range ( i , col ) : NEW_LINE
Calculate sum between the current left and right for every row
for r in range ( row ) : NEW_LINE INDENT sum [ r ] += matrix [ r ] [ j ] NEW_LINE DEDENT
Stores the sum of rectangle
curMax = maxSubarraySum ( sum , k , row ) NEW_LINE
Update the overall maximum sum
ret = max ( ret , curMax ) NEW_LINE
Print the result
print ( ret ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 1 , 0 , 1 ] , [ 0 , - 2 , 3 ] ] NEW_LINE K = 2 NEW_LINE DEDENT
Function Call
maxSumSubmatrix ( matrix , K ) NEW_LINE
Function to count binary strings of length N having substring "11"
def binaryStrings ( N ) : NEW_LINE
Initialize dp [ ] of size N + 1
dp = [ 0 ] * ( N + 1 ) NEW_LINE
Base Cases
dp [ 0 ] = 0 NEW_LINE dp [ 1 ] = 0 NEW_LINE
Stores the first N powers of 2
power = [ 0 ] * ( N + 1 ) NEW_LINE power [ 0 ] = 1 NEW_LINE
Generate
for i in range ( 1 , N + 1 ) : NEW_LINE INDENT power [ i ] = 2 * power [ i - 1 ] NEW_LINE DEDENT
Iterate over the range [ 2 , N ]
for i in range ( 2 , N + 1 ) : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + power [ i - 2 ] NEW_LINE DEDENT
Prtotal count of substrings
print ( dp [ N ] ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 12 NEW_LINE binaryStrings ( N ) NEW_LINE DEDENT
Python3 program for the above approach
import sys NEW_LINE
Function to find maximum continuous maximum sum in the array
def kadane ( v ) : NEW_LINE
Stores current and maximum sum
currSum = 0 NEW_LINE maxSum = - sys . maxsize - 1 NEW_LINE
Traverse the array v
for i in range ( len ( v ) ) : NEW_LINE
Add the value of the current element
currSum += v [ i ] NEW_LINE
Update the maximum sum
if ( currSum > maxSum ) : NEW_LINE INDENT maxSum = currSum NEW_LINE DEDENT if ( currSum < 0 ) : NEW_LINE INDENT currSum = 0 NEW_LINE DEDENT
Return the maximum sum
return maxSum NEW_LINE
Function to find the maximum submatrix sum
def maxSubmatrixSum ( A ) : NEW_LINE
Store the rows and columns of the matrix
r = len ( A ) NEW_LINE c = len ( A [ 0 ] ) NEW_LINE
Create an auxiliary matrix Traverse the matrix , prefix and initialize it will all 0 s
prefix = [ [ 0 for i in range ( c ) ] for j in range ( r ) ] NEW_LINE
Calculate prefix sum of all rows of matrix A [ ] [ ] and store in matrix prefix [ ]
for i in range ( r ) : NEW_LINE INDENT for j in range ( c ) : NEW_LINE DEDENT
Update the prefix [ ] [ ]
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
Store the maximum submatrix sum
maxSum = - sys . maxsize - 1 NEW_LINE
Iterate for starting column
for i in range ( c ) : NEW_LINE
Iterate for last column
for j in range ( i , c ) : NEW_LINE
To store current array elements
v = [ ] NEW_LINE
Traverse every row
for k in range ( r ) : NEW_LINE
Store the sum of the kth row
el = 0 NEW_LINE
Update the prefix sum
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
Push it in a vector
v . append ( el ) NEW_LINE
Update the maximum overall sum
maxSum = max ( maxSum , kadane ( v ) ) NEW_LINE
Print the answer
print ( maxSum ) NEW_LINE
Driver Code
matrix = [ [ 0 , - 2 , - 7 , 0 ] , [ 9 , 2 , - 6 , 2 ] , [ - 4 , 1 , - 4 , 1 ] , [ - 1 , 8 , 0 , - 2 ] ] NEW_LINE
Function Call
maxSubmatrixSum ( matrix ) NEW_LINE
Function to find the minimum cost of coloring the houses such that no two adjacent houses has the same color
def minCost ( costs , N ) : NEW_LINE
Corner Case
if ( N == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT
Auxiliary 2D dp array
dp = [ [ 0 for i in range ( 3 ) ] for j in range ( 3 ) ] NEW_LINE
Base Case
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
If current house is colored with red the take min cost of previous houses colored with ( blue and green )
dp [ i ] [ 0 ] = min ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 2 ] ) + costs [ i ] [ 0 ] NEW_LINE
If current house is colored with blue the take min cost of previous houses colored with ( red and green )
dp [ i ] [ 1 ] = min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 2 ] ) + costs [ i ] [ 1 ] NEW_LINE
If current house is colored with green the take min cost of previous houses colored with ( red and blue )
dp [ i ] [ 2 ] = min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + costs [ i ] [ 2 ] NEW_LINE
Print the min cost of the last painted house
print ( min ( dp [ N - 1 ] [ 0 ] , min ( dp [ N - 1 ] [ 1 ] , dp [ N - 1 ] [ 2 ] ) ) ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT costs = [ [ 14 , 2 , 11 ] , [ 11 , 14 , 5 ] , [ 14 , 3 , 10 ] ] NEW_LINE N = len ( costs ) NEW_LINE DEDENT
Function Call
minCost ( costs , N ) NEW_LINE
Initialize dp [ ] [ ]
dp = [ [ - 1 for i in range ( 2001 ) ] for j in range ( 2001 ) ] NEW_LINE
Function to find the minimum number of operations to make sum of A [ ] 0
def solve ( A , i , sum , N ) : NEW_LINE
Initialize answer
res = 2001 NEW_LINE
Base case
if ( sum < 0 or ( i == N and sum != 0 ) ) : NEW_LINE INDENT return 2001 NEW_LINE DEDENT
Otherwise , return 0
if ( sum == 0 or i >= N ) : NEW_LINE INDENT dp [ i ] [ sum ] = 0 NEW_LINE return 0 NEW_LINE DEDENT
Pre - computed subproblem
if ( dp [ i ] [ sum ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ sum ] NEW_LINE DEDENT
Recurrence relation for finding the minimum of the sum of subsets
res = min ( solve ( A , i + 1 , sum - A [ i ] , N ) + 1 , solve ( A , i + 1 , sum , N ) ) NEW_LINE
Return the result
dp [ i ] [ sum ] = res NEW_LINE return res NEW_LINE
Function to find the minimum number of elements required to be flipped to amke sum the array equal to 0
def minOp ( A , N ) : NEW_LINE INDENT sum = 0 NEW_LINE DEDENT
Find the sum of array
for it in A : NEW_LINE INDENT sum += it NEW_LINE DEDENT if ( sum % 2 == 0 ) : NEW_LINE
Initialise dp [ ] [ ] with - 1
dp = [ [ - 1 for i in range ( 2001 ) ] for j in range ( 2001 ) ] NEW_LINE ans = solve ( A , 0 , sum // 2 , N ) NEW_LINE
No solution exists
if ( ans < 0 or ans > N ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT
Otherwise
else : NEW_LINE INDENT print ( ans ) NEW_LINE DEDENT
If sum is odd , no subset is possible
else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT
Driver Code
A = [ 2 , 3 , 1 , 4 ] NEW_LINE N = len ( A ) NEW_LINE
Function Call
minOp ( A , N ) NEW_LINE
Stores the dp states
dp = [ [ [ 0 for i in range ( 1001 ) ] for i in range ( 101 ) ] for i in range ( 101 ) ] NEW_LINE
Function to find the count of subsequences having average K
def countAverage ( n , K , arr ) : NEW_LINE
Base condition
global dp NEW_LINE dp [ 0 ] [ 0 ] [ 0 ] = 1 NEW_LINE
Three loops for three states
for i in range ( n ) : NEW_LINE INDENT for k in range ( n ) : NEW_LINE INDENT for s in range ( 100 ) : NEW_LINE DEDENT DEDENT
Recurrence relation
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
Stores the sum of dp [ n ] [ j ] [ K * j ] all possible values of j with average K and sum K * j
cnt = 0 NEW_LINE
Iterate over the range [ 1 , N ]
for j in range ( 1 , n + 1 ) : NEW_LINE INDENT cnt += dp [ n ] [ j ] [ K * j ] NEW_LINE DEDENT
Return the final count
return cnt NEW_LINE
Driver Code
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
Function to count ways to remove pairs such that the remaining elements can be arranged in pairs vertically or horizontally
def numberofpairs ( v , k ) : NEW_LINE
Store the size of matrix
n = len ( v ) NEW_LINE
If N is odd , then no such pair exists
if ( n % 2 == 1 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT
Store the number of required pairs
ans = 0 NEW_LINE
Initialize an auxiliary matrix and fill it with 0 s
dp = [ [ 0 for i in range ( 2 ) ] for j in range ( k ) ] NEW_LINE for i in range ( k ) : NEW_LINE INDENT for j in range ( 2 ) : NEW_LINE INDENT dp [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT
Traverse the matrix v [ ] [ ]
for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE DEDENT
Check if i + j is odd or even
if ( ( i + j ) % 2 == 0 ) : NEW_LINE
Increment the value dp [ v [ i ] [ j ] - 1 ] [ 0 ] by 1
dp [ v [ i ] [ j ] - 1 ] [ 0 ] += 1 NEW_LINE else : NEW_LINE
Increment the value dp [ v [ i ] [ j ] - 1 ] [ 1 ] by 1
dp [ v [ i ] [ j ] - 1 ] [ 1 ] += 1 NEW_LINE
Iterate in range [ 0 , k - 1 ] using i
for i in range ( k ) : NEW_LINE
Iterate in range [ i + 1 , k - 1 ] using j
for j in range ( i + 1 , k ) : NEW_LINE
Update the ans
ans += dp [ i ] [ 0 ] * dp [ j ] [ 1 ] NEW_LINE ans += dp [ i ] [ 1 ] * dp [ j ] [ 0 ] NEW_LINE
Print the answer
print ( ans ) NEW_LINE
Driver code
mat = [ [ 1 , 2 ] , [ 3 , 4 ] ] NEW_LINE K = 4 NEW_LINE numberofpairs ( mat , K ) NEW_LINE
Python3 program for the above approach
import sys NEW_LINE
Store overlapping subproblems of the recurrence relation
dp = [ [ [ [ - 1 for i in range ( 50 ) ] for j in range ( 50 ) ] for k in range ( 50 ) ] for l in range ( 50 ) ] NEW_LINE
Function to find maximum sum of at most N with different index array elements such that at most X are from A [ ] , Y are from B [ ] and Z are from C [ ]
def FindMaxS ( X , Y , Z , n , A , B , C ) : NEW_LINE
Base Cases
if ( X < 0 or Y < 0 or Z < 0 ) : NEW_LINE INDENT return - sys . maxsize - 1 NEW_LINE DEDENT if ( n < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT
If the subproblem already computed
if ( dp [ n ] [ X ] [ Y ] [ Z ] != - 1 ) : NEW_LINE INDENT return dp [ n ] [ X ] [ Y ] [ Z ] NEW_LINE DEDENT
Selecting i - th element from A [ ]
ch = A [ n ] + FindMaxS ( X - 1 , Y , Z , n - 1 , A , B , C ) NEW_LINE
Selecting i - th element from B [ ]
ca = B [ n ] + FindMaxS ( X , Y - 1 , Z , n - 1 , A , B , C ) NEW_LINE
Selecting i - th element from C [ ]
co = C [ n ] + FindMaxS ( X , Y , Z - 1 , n - 1 , A , B , C ) NEW_LINE
i - th elements not selected from any of the arrays
no = FindMaxS ( X , Y , Z , n - 1 , A , B , C ) NEW_LINE