text
stringlengths
1
636
code
stringlengths
8
1.89k
Check if i * 5 <= n
if ( i * 5 <= n ) : NEW_LINE INDENT dp [ i * 5 ] = min ( dp [ i * 5 ] , dp [ i ] + 1 ) NEW_LINE DEDENT
Check if i + 3 <= n
if ( i + 3 <= n ) : NEW_LINE INDENT dp [ i + 3 ] = min ( dp [ i + 3 ] , dp [ i ] + 1 ) NEW_LINE DEDENT
Return the result
return dp [ n ] NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given Input
n = 28 NEW_LINE
Function Call
m = findMinOperations ( n ) NEW_LINE
Print the result
if ( m != 9999 ) : NEW_LINE INDENT print ( m ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT
Function to find maximum sum of difference between maximums and minimums in the splitted subarrays
def getValue ( arr , N ) : NEW_LINE INDENT dp = [ 0 for i in range ( N ) ] NEW_LINE DEDENT
Traverse the array
for i in range ( 1 , N ) : NEW_LINE
Stores the maximum and minimum elements upto the i - th index
minn = arr [ i ] NEW_LINE maxx = arr [ i ] NEW_LINE j = i NEW_LINE
Traverse the range [ 0 , i ]
while ( j >= 0 ) : NEW_LINE
Update the minimum
minn = min ( arr [ j ] , minn ) NEW_LINE
Update the maximum
maxx = max ( arr [ j ] , maxx ) NEW_LINE
Update dp [ i ]
dp [ i ] = max ( dp [ i ] , maxx - minn + ( dp [ j - 1 ] if ( j >= 1 ) else 0 ) ) NEW_LINE j -= 1 NEW_LINE
Return the maximum sum of difference
return dp [ N - 1 ] NEW_LINE
Driver code
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
Function to count 0 's in a string
def count0 ( s ) : NEW_LINE
Stores count of 0 s
count = 0 NEW_LINE
Iterate over characters of string
for i in range ( len ( s ) ) : NEW_LINE
Recursive function to find the length of longest subset from an array of strings with at most A 0 ' s ▁ and ▁ B ▁ 1' s
def solve ( vec , A , B , idx ) : NEW_LINE
If idx is equal to N or A + B is equal to 0
if ( idx == len ( vec ) or A + B == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT
Stores the count of 0 's in arr[idx]
zero = count0 ( vec [ idx ] ) NEW_LINE
Stores the count of 1 's in arr[idx]
one = len ( vec [ idx ] ) - zero NEW_LINE
Stores the length of the subset if arr [ i ] is included
inc = 0 NEW_LINE
If zero is less than or equal to A and one is less than or equal to B
if ( zero <= A and one <= B ) : NEW_LINE INDENT inc = 1 + solve ( vec , A - zero , B - one , idx + 1 ) NEW_LINE DEDENT
Stores the length of the subset if arr [ i ] is excluded
exc = solve ( vec , A , B , idx + 1 ) NEW_LINE
Returns max of inc and exc
return max ( inc , exc ) NEW_LINE
Function to find the length of the longest subset from an array of strings with at most A 0 ' s ▁ and ▁ B ▁ 1' s
def MaxSubsetlength ( arr , A , B ) : NEW_LINE
Return
return solve ( arr , A , B , 0 ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ "1" , "0" , "10" ] NEW_LINE A = 1 NEW_LINE B = 1 NEW_LINE print ( MaxSubsetlength ( arr , A , B ) ) NEW_LINE DEDENT
Function to check if the string s consists of a single distinct character or not
def isUnique ( s ) : NEW_LINE INDENT return True if len ( set ( s ) ) == 1 else False NEW_LINE DEDENT
Function to calculate the maximum score possible by removing substrings
def maxScore ( s , a ) : NEW_LINE INDENT n = len ( s ) NEW_LINE DEDENT
If string is empty
if n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT
If length of string is 1
if n == 1 : NEW_LINE INDENT return a [ 0 ] NEW_LINE DEDENT
Store the maximum result
mx = - 1 NEW_LINE
Try to remove all substrings that satisfy the condition and check for resultant string after removal
for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE DEDENT
Store the substring { s [ i ] , . . , s [ j ] }
sub = s [ i : j + 1 ] NEW_LINE
Check if the substring contains only a single distinct character
if isUnique ( sub ) : NEW_LINE INDENT mx = max ( mx , a [ len ( sub ) - 1 ] + maxScore ( s [ : i ] + s [ j + 1 : ] , a ) ) NEW_LINE DEDENT
Return the maximum score
return mx NEW_LINE
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "011" NEW_LINE a = [ 1 , 3 , 1 ] NEW_LINE print ( maxScore ( s , a ) ) NEW_LINE DEDENT
Initialize a dictionary to store the precomputed results
dp = dict ( ) NEW_LINE
Function to calculate the maximum score possible by removing substrings
def maxScore ( s , a ) : NEW_LINE
If s is present in dp [ ] array
if s in dp : NEW_LINE INDENT return dp [ s ] NEW_LINE DEDENT
Base Cases :
n = len ( s ) NEW_LINE
If length of string is 0
if n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT
If length of string is 1
if n == 1 : NEW_LINE INDENT return a [ 0 ] NEW_LINE DEDENT
Put head pointer at start
head = 0 NEW_LINE
Initialize the max variable
mx = - 1 NEW_LINE
Generate the substrings using two pointers
while head < n : NEW_LINE INDENT tail = head NEW_LINE while tail < n : NEW_LINE DEDENT
If s [ head ] and s [ tail ] are different
if s [ tail ] != s [ head ] : NEW_LINE
Move head to tail and break
head = tail NEW_LINE break NEW_LINE
Store the substring
sub = s [ head : tail + 1 ] NEW_LINE
Update the maximum
mx = max ( mx , a [ len ( sub ) - 1 ] + maxScore ( s [ : head ] + s [ tail + 1 : ] , a ) ) NEW_LINE
Move the tail
tail += 1 NEW_LINE if tail == n : NEW_LINE break NEW_LINE
Store the score
dp [ s ] = mx NEW_LINE return mx NEW_LINE
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abb " NEW_LINE a = [ 1 , 3 , 1 ] NEW_LINE print ( maxScore ( s , a ) ) NEW_LINE DEDENT
Dimensions of the DP table
size = 100 NEW_LINE
Stores the dp states
ans = [ [ 0 for i in range ( size ) ] for j in range ( size ) ] NEW_LINE
Function to recursively count the number of unique outcomes possible by performing S flips on N coins
def numberOfUniqueOutcomes ( n , s ) : NEW_LINE
Base Case
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
If the count for the current state is not calculated , then calculate it recursively
elif ( ans [ n ] [ s ] == 0 ) : NEW_LINE INDENT ans [ n ] [ s ] = ( numberOfUniqueOutcomes ( n - 1 , s - 1 ) + numberOfUniqueOutcomes ( n - 1 , s - 2 ) ) NEW_LINE DEDENT
Otherwise return the already calculated value
return ans [ n ] [ s ] ; NEW_LINE
Driver Code
N = 5 NEW_LINE S = 8 NEW_LINE print ( numberOfUniqueOutcomes ( N , S ) ) NEW_LINE
Function to print the minimum number of character removals required to remove X as a subsequence from the string str
def printMinimumRemovals ( s , X ) : NEW_LINE
Length of the string str
N = len ( s ) NEW_LINE
Length of the string X
M = len ( X ) NEW_LINE
Stores the dp states
dp = [ [ 0 for x in range ( M ) ] for y in range ( N ) ] NEW_LINE
Fill first row of dp [ ] [ ]
for j in range ( M ) : NEW_LINE
If X [ j ] matches with str [ 0 ]
if ( s [ 0 ] == X [ j ] ) : NEW_LINE INDENT dp [ 0 ] [ j ] = 1 NEW_LINE DEDENT for i in range ( 1 , N ) : NEW_LINE for j in range ( M ) : NEW_LINE
If s [ i ] is equal to X [ j ]
if ( s [ i ] == X [ j ] ) : NEW_LINE
Update state after removing str [ i [
dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + 1 NEW_LINE
Update state after keeping str [ i ]
if ( j != 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = min ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - 1 ] ) NEW_LINE DEDENT
If str [ i ] is not equal to X [ j ]
else : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] NEW_LINE DEDENT
Print the minimum number of characters removals
print ( dp [ N - 1 ] [ M - 1 ] ) NEW_LINE
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE
Input
s = " btagd " NEW_LINE X = " bad " NEW_LINE
Function call to get minimum number of character removals
printMinimumRemovals ( s , X ) NEW_LINE
Dp table for memoization
dp = [ - 1 for i in range ( 100000 ) ] NEW_LINE
Function to count the number of ways to N - th station
def findWays ( x ) : NEW_LINE
Base Cases
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 current state is already evaluated
if ( dp [ x ] != - 1 ) : NEW_LINE INDENT return dp [ x ] NEW_LINE DEDENT
Count ways in which train 1 can be chosen
count = findWays ( x - 1 ) NEW_LINE
Count ways in which train 2 can be chosen
count += findWays ( x - 2 ) NEW_LINE
Count ways in which train 3 can be chosen
count += findWays ( x - 3 ) NEW_LINE
Store the current state
dp [ x ] = count NEW_LINE
Return the number of ways
return dp [ x ] NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given Input
n = 4 NEW_LINE
Function call to count the number of ways to reach the n - th station
print ( findWays ( n ) ) NEW_LINE
Python3 program for the above approach
from bisect import bisect_left , bisect_right NEW_LINE import sys NEW_LINE
Function to find the maximum possible sum of arectangle which is less than K
def maxSubarraySum ( sum , k , row ) : NEW_LINE INDENT curSum , curMax = 0 , - sys . maxsize - 1 NEW_LINE DEDENT
Stores the values ( cum_sum - K )
sumSet = { } NEW_LINE
Insert 0 into the set sumSet
sumSet [ 0 ] = 1 NEW_LINE
Traverse over the rows
for r in range ( row ) : NEW_LINE
Get cumulative sum from [ 0 to i ]
curSum += sum [ r ] NEW_LINE
Search for upperbound of ( cSum - K ) in the hashmap
arr = list ( sumSet . keys ( ) ) NEW_LINE it = bisect_left ( arr , curSum - k ) NEW_LINE
If upper_bound of ( cSum - K ) exists , then update max sum
if ( it != len ( arr ) ) : NEW_LINE INDENT curMax = max ( curMax , curSum - it ) NEW_LINE DEDENT
Insert cummulative value in the hashmap
sumSet [ curSum ] = 1 NEW_LINE
Return the maximum sum which is less than K
return curMax NEW_LINE
Function to find the maximum sum of rectangle such that its sum is no larger than K
def maxSumSubmatrix ( matrix , k ) : NEW_LINE