Maximum Tip Calculator Python
- Get link
- X
- Other Apps
PROGRAM TO CALCULATE THE MAXIMUM TIP
# Function that finds the maximum tips# from the given arrays as per the# given conditionsdef 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 CodeN = 5X = 3Y = 3A = [1, 2, 3, 4, 5]B = [5, 4, 3, 2, 1]# Stores the results of the# overlapping statedd = {}# Function Callprint(maximumTip(A, B, N, X, Y, dd))
OUTPUT:
21
- Get link
- X
- Other Apps
Comments
Post a Comment