codeparrot/xlcost-text-to-code huggingface dataset:
Number of ways to choose elements from the array such that their average is K | Python implementation of above approach ; Initialize dp array by - 1 ; Base cases Index can 't be less than 0 ; No element is picked hence average cannot be calculated ; If remainder is non zero , we cannot divide the sum by count i . e . the average will not be an integer ; If we find an average return 1 ; If we have already calculated this function simply return it instead of calculating it again ; If we don 't pick the current element  simple recur for index -1 ; If we pick the current element add it to our current sum and increment count by 1 ; Store the value for the current function ; Function to return the number of ways ; Push - 1 at the beginning to make it 1 - based indexing ; Call recursive function waysutil to calculate total ways ; Driver code
import numpy as np NEW_LINE MAX_INDEX = 51 NEW_LINE MAX_SUM = 2505 NEW_LINE dp = np . ones ( ( MAX_INDEX , MAX_SUM , MAX_INDEX ) ) * - 1 ; NEW_LINE def waysutil ( index , sum , count , arr , K ) : NEW_LINE INDENT if ( index < 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( index == 0 ) : NEW_LINE INDENT if ( count == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT remainder = sum % count ; NEW_LINE if ( remainder != 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT average = sum // count ; NEW_LINE if ( average == K ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT DEDENT if ( dp [ index ] [ sum ] [ count ] != - 1 ) : NEW_LINE INDENT return dp [ index ] [ sum ] [ count ] ; NEW_LINE DEDENT dontpick = waysutil ( index - 1 , sum , count , arr , K ) ; NEW_LINE pick = waysutil ( index - 1 , sum + arr [ index ] , count + 1 , arr , K ) ; NEW_LINE total = pick + dontpick ; NEW_LINE dp [ index ] [ sum ] [ count ] = total ; NEW_LINE return total ; NEW_LINE DEDENT def ways ( N , K , arr ) : NEW_LINE INDENT Arr = [ ] ; NEW_LINE Arr . append ( - 1 ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT Arr . append ( arr [ i ] ) ; NEW_LINE DEDENT answer = waysutil ( N , 0 , 0 , Arr , K ) ; NEW_LINE return answer ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE K = 5 ; NEW_LINE print ( ways ( N , K , arr ) ) ; NEW_LINE DEDENT
Subset with sum closest to zero | Python3 Code for above implementation ; Variable to store states of dp ; Function to return the number closer to integer s ; To find the sum closest to zero Since sum can be negative , we will add MAX to it to make it positive ; Base cases ; Checks if a state is already solved ; Recurrence relation ; Returning the value ; Function to calculate the closest sum value ; Calculate the Closest value for every subarray arr [ i - 1 : n ] ; Driver function ; Input array
import numpy as np NEW_LINE arrSize = 51 NEW_LINE maxSum = 201 NEW_LINE MAX = 100 NEW_LINE inf = 999999 NEW_LINE dp = np . zeros ( ( arrSize , maxSum ) ) ; NEW_LINE visit = np . zeros ( ( arrSize , maxSum ) ) ; NEW_LINE def RetClose ( a , b , s ) : NEW_LINE INDENT if ( abs ( a - s ) < abs ( b - s ) ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT else : NEW_LINE INDENT return b ; NEW_LINE DEDENT DEDENT def MinDiff ( i , sum , arr , n ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( visit [ i ] [ sum + MAX ] ) : NEW_LINE INDENT return dp [ i ] [ sum + MAX ] ; NEW_LINE DEDENT visit [ i ] [ sum + MAX ] = 1 ; NEW_LINE dp [ i ] [ sum + MAX ] = RetClose ( arr [ i ] + MinDiff ( i + 1 , sum + arr [ i ] , arr , n ) , MinDiff ( i + 1 , sum , arr , n ) , - 1 * sum ) ; NEW_LINE return dp [ i ] [ sum + MAX ] ; NEW_LINE DEDENT def FindClose ( arr , n ) : NEW_LINE INDENT ans = inf ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ans = RetClose ( arr [ i - 1 ] + MinDiff ( i , arr [ i - 1 ] , arr , n ) , ans , 0 ) ; NEW_LINE DEDENT print ( ans ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 25 , - 9 , - 10 , - 4 , - 7 , - 33 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE FindClose ( arr , n ) ; NEW_LINE DEDENT
Paths from entry to exit in matrix and maximum path sum | Recursive function to return the total paths from grid [ i ] [ j ] to grid [ n - 1 ] [ n - 1 ] ; Out of bounds ; If the current state hasn 't been solved before  ; Only valid move is right ; Only valid move is down ; Right and down , both are valid moves ; Recursive function to return the maximum sum along the path from grid [ i , j ] to grid [ n - 1 , n - 1 ] ; Out of bounds ; If the current state hasn 't been solved before  ; Only valid move is right ; Only valid move is down ; Right and down , both are valid moves ; Driver code ; Fill the dp [ n ] [ n ] array with - 1 ; When source and destination are same then there is only 1 path ; Print the count of paths from grid [ 0 , 0 ] to grid [ n - 1 ] [ n - 1 ] ; Fill the dp [ n ] [ n ] array again with - 1 ; When source and destination are same then the sum is grid [ n - 1 ] [ n - 1 ] ; Print the maximum sum among all the paths from grid [ 0 , 0 ] to grid [ n - 1 ] [ n - 1 ]
def totalPaths ( i , j , n , grid , dp ) : NEW_LINE INDENT if ( i < 0 or j < 0 or i >= n or j >= n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ i ] [ j ] == - 1 ) : NEW_LINE INDENT if ( grid [ i ] [ j ] == 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = totalPaths ( i , j + 1 , n , grid , dp ) NEW_LINE DEDENT elif ( grid [ i ] [ j ] == 2 ) : NEW_LINE INDENT dp [ i ] [ j ] = totalPaths ( i + 1 , j , n , grid , dp ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = totalPaths ( i , j + 1 , n , grid , dp ) + totalPaths ( i + 1 , j , n , grid , dp ) NEW_LINE DEDENT DEDENT return dp [ i ] [ j ] NEW_LINE DEDENT def maxSumPath ( i , j , n , grid , dp ) : NEW_LINE INDENT if ( i < 0 or j < 0 or i >= n or j >= n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ i ] [ j ] == - 1 ) : NEW_LINE INDENT if ( grid [ i ] [ j ] == 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = grid [ i ] [ j ] + maxSumPath ( i , j + 1 , n , grid , dp ) NEW_LINE DEDENT elif ( grid [ i ] [ j ] == 2 ) : NEW_LINE INDENT dp [ i ] [ j ] = grid [ i ] [ j ] + maxSumPath ( i + 1 , j , n , grid , dp ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = grid [ i ] [ j ] + max ( maxSumPath ( i , j + 1 , n , grid , dp ) , maxSumPath ( i + 1 , j , n , grid , dp ) ) NEW_LINE DEDENT DEDENT return dp [ i ] [ j ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT grid = [ [ 1 , 1 , 3 , 2 , 1 ] , [ 3 , 2 , 2 , 1 , 2 ] , [ 1 , 3 , 3 , 1 , 3 ] , [ 1 , 2 , 3 , 1 , 2 ] , [ 1 , 1 , 1 , 3 , 1 ] ] NEW_LINE n = len ( grid [ 0 ] ) NEW_LINE dp = [ [ - 1 ] * n ] * n NEW_LINE dp [ n - 1 ] [ n - 1 ] = 1 NEW_LINE print ( " Total ▁ paths : " , totalPaths ( 0 , 0 , n , grid , dp ) ) NEW_LINE dp = [ [ - 1 ] * n ] * n NEW_LINE dp [ n - 1 ] [ n - 1 ] = grid [ n - 1 ] [ n - 1 ] NEW_LINE print ( " Maximum ▁ sum : " , maxSumPath ( 0 , 0 , n , grid , dp ) ) NEW_LINE DEDENT
Queries for bitwise OR in the index range [ L , R ] of the given array | Python3 implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix count ; Function to answer query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; Condition for ith bit of answer to be set ; Driver code
import numpy as np NEW_LINE MAX = 100000 NEW_LINE bitscount = 32 NEW_LINE prefix_count = np . zeros ( ( bitscount , MAX ) ) ; NEW_LINE def findPrefixCount ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , bitscount ) : NEW_LINE INDENT prefix_count [ i ] [ 0 ] = ( ( arr [ 0 ] >> i ) & 1 ) ; NEW_LINE for j in range ( 1 , n ) : NEW_LINE INDENT prefix_count [ i ] [ j ] = ( ( arr [ j ] >> i ) & 1 ) ; NEW_LINE prefix_count [ i ] [ j ] += prefix_count [ i ] [ j - 1 ] ; NEW_LINE DEDENT DEDENT DEDENT def rangeOr ( l , r ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for i in range ( bitscount ) : NEW_LINE INDENT x = 0 ; NEW_LINE if ( l == 0 ) : NEW_LINE INDENT x = prefix_count [ i ] [ r ] ; NEW_LINE DEDENT else : NEW_LINE INDENT x = prefix_count [ i ] [ r ] - prefix_count [ i ] [ l - 1 ] ; NEW_LINE DEDENT if ( x != 0 ) : NEW_LINE INDENT ans = ( ans | ( 1 << i ) ) ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 5 , 3 , 5 , 2 , 3 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE findPrefixCount ( arr , n ) ; NEW_LINE queries = [ [ 1 , 3 ] , [ 4 , 5 ] ] ; NEW_LINE q = len ( queries ) ; NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( rangeOr ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) ; NEW_LINE DEDENT DEDENT
Distinct palindromic sub | Python3 implementation of the approach ; Function to return the count of distinct palindromic sub - strings of the given string s ; To store the positions of palindromic sub - strings ; Map to store the sub - strings ; Sub - strings of length 1 are palindromes ; Store continuous palindromic sub - strings ; Store palindromes of size 2 ; If str [ i ... ( i + 1 ) ] is not a palindromic then set dp [ i ] [ i + 1 ] = 0 ; Find palindromic sub - strings of length >= 3 ; End of palindromic substring ; If s [ start ] = = s [ end ] and dp [ start + 1 ] [ end - 1 ] is already palindrome then s [ start ... . end ] is also a palindrome ; Set dp [ start ] [ end ] = 1 ; Not a palindrome ; Return the count of distinct palindromes ; Driver code
import numpy as np ; NEW_LINE def palindromeSubStrs ( s ) : NEW_LINE INDENT dp = np . zeros ( ( len ( s ) , len ( s ) ) ) ; NEW_LINE m = { } ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT dp [ i ] [ i ] = 1 ; NEW_LINE m [ s [ i : i + 1 ] ] = 1 ; NEW_LINE DEDENT for i in range ( len ( s ) - 1 ) : NEW_LINE INDENT if ( s [ i ] == s [ i + 1 ] ) : NEW_LINE INDENT dp [ i ] [ i + 1 ] = 1 ; NEW_LINE m [ s [ i : i + 2 ] ] = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ i + 1 ] = 0 ; NEW_LINE DEDENT DEDENT for length in range ( 3 , len ( s ) + 1 ) : NEW_LINE INDENT for st in range ( len ( s ) - length + 1 ) : NEW_LINE INDENT end = st + length - 1 ; NEW_LINE if ( s [ st ] == s [ end ] and dp [ st + 1 ] [ end - 1 ] ) : NEW_LINE INDENT dp [ st ] [ end ] = 1 ; NEW_LINE m [ s [ st : end + 1 ] ] = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT dp [ st ] [ end ] = 0 ; NEW_LINE DEDENT DEDENT DEDENT return len ( m ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abaaa " ; NEW_LINE print ( palindromeSubStrs ( s ) ) ; NEW_LINE DEDENT
Maximum sum path in a matrix from top to bottom and back | Python 3 implementation of the approach ; Input matrix ; DP matrix ; Function to return the sum of the cells arr [ i1 ] [ j1 ] and arr [ i2 ] [ j2 ] ; Recursive function to return the required maximum cost path ; Column number of second path ; Base Case ; If already calculated , return from DP matrix ; Recurring for neighbouring cells of both paths together ; Saving result to the DP matrix for current state ; Driver code ; set initial value
import sys NEW_LINE n = 4 NEW_LINE m = 4 NEW_LINE arr = [ [ 1 , 0 , 3 , - 1 ] , [ 3 , 5 , 1 , - 2 ] , [ - 2 , 0 , 1 , 1 ] , [ 2 , 1 , - 1 , 1 ] ] NEW_LINE cache = [ [ [ - 1 for i in range ( 5 ) ] for j in range ( 5 ) ] for k in range ( 5 ) ] NEW_LINE def sum ( i1 , j1 , i2 , j2 ) : NEW_LINE INDENT if ( i1 == i2 and j1 == j2 ) : NEW_LINE INDENT return arr [ i1 ] [ j1 ] NEW_LINE DEDENT return arr [ i1 ] [ j1 ] + arr [ i2 ] [ j2 ] NEW_LINE DEDENT def maxSumPath ( i1 , j1 , i2 ) : NEW_LINE INDENT j2 = i1 + j1 - i2 NEW_LINE if ( i1 >= n or i2 >= n or j1 >= m or j2 >= m ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( cache [ i1 ] [ j1 ] [ i2 ] != - 1 ) : NEW_LINE INDENT return cache [ i1 ] [ j1 ] [ i2 ] NEW_LINE DEDENT ans = - sys . maxsize - 1 NEW_LINE ans = max ( ans , maxSumPath ( i1 + 1 , j1 , i2 + 1 ) + sum ( i1 , j1 , i2 , j2 ) ) NEW_LINE ans = max ( ans , maxSumPath ( i1 , j1 + 1 , i2 ) + sum ( i1 , j1 , i2 , j2 ) ) NEW_LINE ans = max ( ans , maxSumPath ( i1 , j1 + 1 , i2 + 1 ) + sum ( i1 , j1 , i2 , j2 ) ) NEW_LINE ans = max ( ans , maxSumPath ( i1 + 1 , j1 , i2 ) + sum ( i1 , j1 , i2 , j2 ) ) NEW_LINE cache [ i1 ] [ j1 ] [ i2 ] = ans NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( maxSumPath ( 0 , 0 , 0 ) ) NEW_LINE DEDENT
Bellman Ford Algorithm ( Simple Implementation ) | Python3 program for Bellman - Ford 's single source shortest path algorithm. ; The main function that finds shortest distances from src to all other vertices using Bellman - Ford algorithm . The function also detects negative weight cycle The row graph [ i ] represents i - th edge with three values u , v and w . ; Initialize distance of all vertices as infinite . ; initialize distance of source as 0 ; Relax all edges | V | - 1 times . A simple shortest path from src to any other vertex can have at - most | V | - 1 edges ; check for negative - weight cycles . The above step guarantees shortest distances if graph doesn 't contain  negative weight cycle. If we get a  shorter path, then there is a cycle. ; Driver Code ; Every edge has three values ( u , v , w ) where the edge is from vertex u to v . And weight of the edge is w .
from sys import maxsize NEW_LINE def BellmanFord ( graph , V , E , src ) : NEW_LINE INDENT dis = [ maxsize ] * V NEW_LINE dis [ src ] = 0 NEW_LINE for i in range ( V - 1 ) : NEW_LINE INDENT for j in range ( E ) : NEW_LINE INDENT if dis [ graph [ j ] [ 0 ] ] + graph [ j ] [ 2 ] < dis [ graph [ j ] [ 1 ] ] : NEW_LINE INDENT dis [ graph [ j ] [ 1 ] ] = dis [ graph [ j ] [ 0 ] ] + graph [ j ] [ 2 ] NEW_LINE DEDENT DEDENT DEDENT for i in range ( E ) : NEW_LINE INDENT x = graph [ i ] [ 0 ] NEW_LINE y = graph [ i ] [ 1 ] NEW_LINE weight = graph [ i ] [ 2 ] NEW_LINE if dis [ x ] != maxsize and dis [ x ] + weight < dis [ y ] : NEW_LINE INDENT print ( " Graph ▁ contains ▁ negative ▁ weight ▁ cycle " ) NEW_LINE DEDENT DEDENT print ( " Vertex ▁ Distance ▁ from ▁ Source " ) NEW_LINE for i in range ( V ) : NEW_LINE INDENT print ( " % d TABSYMBOL TABSYMBOL % d " % ( i , dis [ i ] ) ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT graph = [ [ 0 , 1 , - 1 ] , [ 0 , 2 , 4 ] , [ 1 , 2 , 3 ] , [ 1 , 3 , 2 ] , [ 1 , 4 , 2 ] , [ 3 , 2 , 5 ] , [ 3 , 1 , 1 ] , [ 4 , 3 , - 3 ] ] NEW_LINE BellmanFord ( graph , V , E , 0 ) NEW_LINE DEDENT
Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal | Python3 implementation of the approach ; Function to perform simple DFS ; Finding the number of times each bit is set in all the values of a subtree rooted at v ; Checking for each bit whether the numbers with that particular bit as set are either zero in both the resulting trees or greater than zero in both the resulting trees ; Driver code ; Number of nodes ; ArrayList to store the tree ; Array to store the value of nodes ; Array to store the number of times each bit is set in all the values in complete tree ; Finding the set bits in the value of node i ; append edges
m , x = [ 0 ] * 1000 , [ 0 ] * 22 NEW_LINE a = [ [ 0 for i in range ( 22 ) ] for j in range ( 1000 ) ] NEW_LINE ans = 0 NEW_LINE def dfs ( u , p ) : NEW_LINE INDENT global ans NEW_LINE for i in range ( 0 , len ( g [ u ] ) ) : NEW_LINE INDENT v = g [ u ] [ i ] NEW_LINE if v != p : NEW_LINE INDENT dfs ( v , u ) NEW_LINE for i in range ( 0 , 22 ) : NEW_LINE INDENT a [ u ] [ i ] += a [ v ] [ i ] NEW_LINE DEDENT DEDENT DEDENT pp = 0 NEW_LINE for i in range ( 0 , 22 ) : NEW_LINE INDENT if ( not ( ( a [ u ] [ i ] > 0 and x [ i ] - a [ u ] [ i ] > 0 ) or ( a [ u ] [ i ] == 0 and x [ i ] == 0 ) ) ) : NEW_LINE INDENT pp = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if pp == 0 : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE g = [ [ ] for i in range ( n + 1 ) ] NEW_LINE m [ 1 ] = 1 NEW_LINE m [ 2 ] = 3 NEW_LINE m [ 3 ] = 2 NEW_LINE m [ 4 ] = 3 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT y , k = m [ i ] , 0 NEW_LINE while y != 0 : NEW_LINE INDENT p = y % 2 NEW_LINE if p == 1 : NEW_LINE INDENT x [ k ] += 1 NEW_LINE a [ i ] [ k ] += 1 NEW_LINE DEDENT y = y // 2 NEW_LINE k += 1 NEW_LINE DEDENT DEDENT g [ 1 ] . append ( 2 ) NEW_LINE g [ 2 ] . append ( 1 ) NEW_LINE g [ 1 ] . append ( 3 ) NEW_LINE g [ 3 ] . append ( 1 ) NEW_LINE g [ 1 ] . append ( 4 ) NEW_LINE g [ 4 ] . append ( 1 ) NEW_LINE dfs ( 1 , 0 ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Find the maximum number of composite summands of a number | Python 3 implementation of the above approach ; Function to generate the dp array ; combination of three integers ; take the maximum number of summands ; Function to find the maximum number of summands ; If n is a smaller number , less than 16 , return dp [ n ] ; Else , find a minimal number t as explained in solution ; Driver code ; Generate dp array
global maxn NEW_LINE maxn = 16 NEW_LINE def precompute ( ) : NEW_LINE INDENT dp = [ - 1 for i in range ( maxn ) ] NEW_LINE dp [ 0 ] = 0 NEW_LINE v = [ 4 , 6 , 9 ] NEW_LINE for i in range ( 1 , maxn , 1 ) : NEW_LINE INDENT for k in range ( 3 ) : NEW_LINE INDENT j = v [ k ] NEW_LINE if ( i >= j and dp [ i - j ] != - 1 ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , dp [ i - j ] + 1 ) NEW_LINE DEDENT DEDENT DEDENT return dp NEW_LINE DEDENT def Maximum_Summands ( dp , n ) : NEW_LINE INDENT if ( n < maxn ) : NEW_LINE INDENT return dp [ n ] NEW_LINE DEDENT else : NEW_LINE INDENT t = int ( ( n - maxn ) / 4 ) + 1 NEW_LINE return t + dp [ n - 4 * t ] NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12 NEW_LINE dp = precompute ( ) NEW_LINE print ( Maximum_Summands ( dp , n ) ) NEW_LINE DEDENT
Minimum cost to reach end of array array when a maximum jump of K index is allowed | Python3 implementation of the approach ; for calculating the number of elements ; Allocating Memo table and initializing with INT_MAX ; Base case ; For every element relax every reachable element ie relax next k elements ; reaching next k element ; Relaxing the element ; return the last element in the array ; Driver Code
import sys NEW_LINE def minCostJumpsDP ( A , k ) : NEW_LINE INDENT size = len ( A ) NEW_LINE x = [ sys . maxsize ] * ( size ) NEW_LINE x [ 0 ] = 0 NEW_LINE for i in range ( size ) : NEW_LINE INDENT j = i + 1 NEW_LINE while j < i + k + 1 and j < size : NEW_LINE INDENT x [ j ] = min ( x [ j ] , x [ i ] + abs ( A [ i ] - A [ j ] ) ) NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return x [ size - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT input_ = [ 83 , 26 , 37 , 35 , 33 , 35 , 56 ] NEW_LINE print ( minCostJumpsDP ( input_ , 3 ) ) NEW_LINE DEDENT
DP on Trees | Set | Function to find the diameter of the tree using Dynamic Programming ; Store the first maximum and secondmax ; Traverse for all children of node ; Call DFS function again ; Find first max ; Secondmaximum ; elif dp1 [ i ] > secondmax : Find secondmaximum ; Base case for every node ; if firstmax != - 1 : Add ; Find dp [ 2 ] ; Return maximum of both ; Driver Code ; Constructed tree is 1 / \ 2 3 / \ 4 5 ; create undirected edges ; Find diameter by calling function
def dfs ( node , parent , dp1 , dp2 , adj ) : NEW_LINE INDENT firstmax , secondmax = - 1 , - 1 NEW_LINE for i in adj [ node ] : NEW_LINE INDENT if i == parent : NEW_LINE INDENT continue NEW_LINE DEDENT dfs ( i , node , dp1 , dp2 , adj ) NEW_LINE if firstmax == - 1 : NEW_LINE INDENT firstmax = dp1 [ i ] NEW_LINE DEDENT DEDENT elif dp1 [ i ] >= firstmax : NEW_LINE INDENT secondmax = firstmax NEW_LINE firstmax = dp1 [ i ] NEW_LINE secondmax = dp1 [ i ] NEW_LINE DEDENT dp1 [ node ] = 1 NEW_LINE INDENT dp1 [ node ] += firstmax NEW_LINE DEDENT if secondmax != - 1 : NEW_LINE INDENT dp2 [ node ] = 1 + firstmax + secondmax NEW_LINE DEDENT return max ( dp1 [ node ] , dp2 [ node ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , diameter = 5 , - 1 NEW_LINE adj = [ [ ] for i in range ( n + 1 ) ] NEW_LINE adj [ 1 ] . append ( 2 ) NEW_LINE adj [ 2 ] . append ( 1 ) NEW_LINE adj [ 1 ] . append ( 3 ) NEW_LINE adj [ 3 ] . append ( 1 ) NEW_LINE adj [ 2 ] . append ( 4 ) NEW_LINE adj [ 4 ] . append ( 2 ) NEW_LINE adj [ 2 ] . append ( 5 ) NEW_LINE adj [ 5 ] . append ( 2 ) NEW_LINE dp1 = [ 0 ] * ( n + 1 ) NEW_LINE dp2 = [ 0 ] * ( n + 1 ) NEW_LINE print ( " Diameter ▁ of ▁ the ▁ given ▁ tree ▁ is " , dfs ( 1 , 1 , dp1 , dp2 , adj ) ) NEW_LINE DEDENT
Find sub | Python implementation of the approach ; Function to return the sum of the sub - matrix ; Function that returns true if it is possible to find the sub - matrix with required sum ; 2 - D array to store the sum of all the sub - matrices ; Filling of dp [ ] [ ] array ; Checking for each possible sub - matrix of size k X k ; Sub - matrix with the given sum not found ; Driver code ; Function call
N = 4 NEW_LINE def getSum ( r1 , r2 , c1 , c2 , dp ) : NEW_LINE INDENT return dp [ r2 ] [ c2 ] - dp [ r2 ] [ c1 ] - dp [ r1 ] [ c2 ] + dp [ r1 ] [ c1 ] NEW_LINE DEDENT def sumFound ( K , S , grid ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( N + 1 ) ] for j in range ( N + 1 ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT dp [ i + 1 ] [ j + 1 ] = dp [ i + 1 ] [ j ] + dp [ i ] [ j + 1 ] - dp [ i ] [ j ] + grid [ i ] [ j ] NEW_LINE DEDENT DEDENT for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , N ) : NEW_LINE INDENT sum = getSum ( i , i + K , j , j + K , dp ) NEW_LINE if ( sum == S ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT grid = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] NEW_LINE K = 2 NEW_LINE S = 14 NEW_LINE if ( sumFound ( K , S , grid ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Stepping Numbers | Prints all stepping numbers reachable from num and in range [ n , m ] ; Queue will contain all the stepping Numbers ; Get the front element and pop from the queue ; If the Stepping Number is in the range [ n , m ] then display ; If Stepping Number is 0 or greater than m , no need to explore the neighbors ; Get the last digit of the currently visited Stepping Number ; There can be 2 cases either digit to be appended is lastDigit + 1 or lastDigit - 1 ; If lastDigit is 0 then only possible digit after 0 can be 1 for a Stepping Number ; If lastDigit is 9 then only possible digit after 9 can be 8 for a Stepping Number ; Prints all stepping numbers in range [ n , m ] using BFS . ; For every single digit Number ' i ' find all the Stepping Numbers starting with i ; Driver code ; Display Stepping Numbers in the range [ n , m ]
def bfs ( n , m , num ) : NEW_LINE INDENT q = [ ] NEW_LINE q . append ( num ) NEW_LINE while len ( q ) > 0 : NEW_LINE INDENT stepNum = q [ 0 ] NEW_LINE q . pop ( 0 ) ; NEW_LINE if ( stepNum <= m and stepNum >= n ) : NEW_LINE INDENT print ( stepNum , end = " ▁ " ) NEW_LINE DEDENT if ( num == 0 or stepNum > m ) : NEW_LINE INDENT continue NEW_LINE DEDENT lastDigit = stepNum % 10 NEW_LINE stepNumA = stepNum * 10 + ( lastDigit - 1 ) NEW_LINE stepNumB = stepNum * 10 + ( lastDigit + 1 ) NEW_LINE if ( lastDigit == 0 ) : NEW_LINE INDENT q . append ( stepNumB ) NEW_LINE DEDENT elif ( lastDigit == 9 ) : NEW_LINE INDENT q . append ( stepNumA ) NEW_LINE DEDENT else : NEW_LINE INDENT q . append ( stepNumA ) NEW_LINE q . append ( stepNumB ) NEW_LINE DEDENT DEDENT DEDENT def displaySteppingNumbers ( n , m ) : NEW_LINE INDENT for i in range ( 10 ) : NEW_LINE INDENT bfs ( n , m , i ) NEW_LINE DEDENT DEDENT n , m = 0 , 21 NEW_LINE displaySteppingNumbers ( n , m ) NEW_LINE