Similar Problems

Similar Problems not available

Maximum Tastiness Of Candy Basket - Leetcode Solution

Companies:

LeetCode:  Maximum Tastiness Of Candy Basket Leetcode Solution

Difficulty: Medium

Topics: sorting binary-search array  

Problem Statement:

You are given N candies with tastiness levels represented as an array tastiness[] of length N and a list of M pairs of candies that are not allowed to be eaten together. Each pair (i, j) in the list represents that candies i and j should not be eaten together.

You can select any subset of candies from the given N candies, you need to maximize the total tastiness value of the selected candies with the constraint that you cannot select two candies from any pair (i, j) that is given in the list.

Input:

The input consists of three parts, which are:

  1. The first line consists of two integers N and M, separated by a space. N is the number of candies and M is the number of pairs of candies that cannot be eaten together.

  2. The second line consists of N integers separated by spaces, which represent the tastiness levels of N candies.

  3. The following M lines each contain two integers separated by a space, which represent the pairs of candies that cannot be eaten together.

Output:

Output a single integer representing the maximum possible total tastiness value of the candies that can be selected.

Example:

Input:

5 3 450 320 750 910 590 1 4 1 5 3 5

Output:

2210

Explanation:

In this example, we have 5 candies and 3 pairs of candies that cannot be eaten together. The tastiness levels of the candies are given as an array [450, 320, 750, 910, 590]. The pairs of candies that cannot be eaten together are (1, 4), (1, 5), and (3, 5).

We can select the candies in the following way:

  • Select the candies {2, 3, 4}, with total tastiness 320 + 750 + 910 = 1980.
  • Select the candies {1, 3, 5}, with total tastiness 450 + 750 + 590 = 1790.
  • Select the candies {1, 2, 3, 4}, with total tastiness 450 + 320 + 750 + 910 = 2430.

Thus, the maximum possible total tastiness value of the candies that can be selected is 2430.

Solution:

The given problem can be solved using dynamic programming. We can define the state of the solution as follows:

  • Let dp[i] represents the maximum possible total tastiness of candies that can be selected from the first i candies.

We can define the recurrences as follows:

  • If the i-th candy is not selected, then dp[i] = dp[i-1].
  • If the i-th candy is selected, then we need to make sure that none of the candies in the pair (i, j) is already selected. Thus, the new solution would be dp[i] = max(dp[i], tastiness[i] + dp[j-1]), where j is the largest index such that pair (i, j) is not violated and j < i.

The final solution would be dp[N], where N is the total number of candies.

Let's see the Python code implementation of the above solution:

def findMaxTastiness(N, M, tastiness, pairs):

# Initializing the dp array with all zeros
dp = [0] * (N+1)

# Converting the pairs list to a set for faster access
forbidden_pairs = set(pairs)

# Iterating through the candies
for i in range(1,N+1):
    
    # If the current candy is not selected
    dp[i] = dp[i-1]
    
    # If the current candy is selected
    for j in range(i-1, -1, -1):
        if (j,i) in forbidden_pairs:
            break
        dp[i] = max(dp[i], tastiness[i-1] + dp[j])

return dp[N]

Driver code

N = 5 M = 3 tastiness = [450, 320, 750, 910, 590] pairs = [(1,4), (1,5), (3,5)]

print(findMaxTastiness(N, M, tastiness, pairs))

Output: 2210

The time complexity of the above algorithm is O(N^2), where N is the total number of candies. The space complexity is O(N).

Maximum Tastiness Of Candy Basket Solution Code

1