Maximum Tip Calculator Python

PROGRAM TO CALCULATE THE MAXIMUM TIP



# Function that finds the maximum tips
# from the given arrays as per the
# given conditions
def maximumTip(arr1, arr2, n, x, y, dd):
 
    # Create key of N, X, Y
    key = str(n) + '_' + str(x) + '_' + str(y)
 
    # Return if the current state is
    # already calculated
    if key in dd:
        return dd[key]
 
    # Base Condition
    if n == 0:
        return 0
 
    # Store and return
    if x != 0 and y != 0:
        dd[key] = max(
            arr1[n-1] + maximumTip(arr1, arr2, n-1, x-1, y, dd),
            arr2[n-1] + maximumTip(arr1, arr2, n-1, x, y-1, dd)
        )
 
        # Return the current state result
        return dd[key]
 
    # If y is zero, only x value
    # can be used
    if y == 0:
        dd[key] = arr1[n-1] + maximumTip(arr1, arr2, n-1, x-1, y, dd)
 
        # Return the current state result
        return dd[key]
 
    # If x is zero, only y value
    # can be used
    else:
 
        dd[key] = arr2[n-1] + maximumTip(arr1, arr2, n-1, x, y-1, dd)
 
        # Return the current state result
        return dd[key]
 
 
# Drive Code
N = 5
X = 3
Y = 3
A = [1, 2, 3, 4, 5]
B = [5, 4, 3, 2, 1]
 
# Stores the results of the
# overlapping state
dd = {}
 
# Function Call
print(maximumTip(A, B, N, X, Y, dd))


OUTPUT:
21

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java