Similar Problems
Similar Problems not available
Maximum Total Beauty Of The Gardens - Leetcode Solution
Companies:
LeetCode: Maximum Total Beauty Of The Gardens Leetcode Solution
Difficulty: Hard
Topics: greedy binary-search two-pointers array sorting
Problem Description:
You are given a set of gardens, where each garden has some beauty value. You want to select a subset of gardens such that the total beauty value of the selected gardens is maximum and no two selected gardens share a common edge (i.e., they do not have a common boundary).
Input:
The input consists of three arguments:
- Integer N: the number of gardens.
- List of integers beauties: the beauty values of the gardens.
- List of List of integers edges: the adjacency list of the gardens.
Each element of edges is of the form [u, v], indicating that there is an edge between the gardens u and v.
Output:
Return an integer, the maximum possible total beauty value of the selected gardens.
Constraints:
- 2 <= N <= 1000
- 0 <= beauties[i] <= 1000
- 0 <= edges.length <= min(N(N-1)/2, 10^5)
- All edges are undirected and do not contain self loops or duplicate edges.
- gardens i and j have at most one common boundary.
Solution:
The problem can be solved using dynamic programming. Let beauties[i] be the beauty value of the i-th garden and edges[u][v] be true if there is an edge between gardens u and v and false otherwise. Define dp[u][0] as the maximum possible total beauty value of the selected gardens among gardens 1 to u (inclusive), and dp[u][1] as the maximum possible total beauty value of the selected gardens among gardens 1 to u (inclusive), where the garden u has been selected.
The base cases are dp[1][0] = 0 and dp[1][1] = beauties[1], as there is only one garden in this case.
For the general case, we have the following recurrence relation:
dp[u][0] = max(dp[v][1], dp[v][0]), where v is a neighbor of u. dp[u][1] = dp[v][0] + beauties[u], where v is a neighbor of u.
The intuition behind this is that if we do not select garden u, we can select any of its neighbors (v) to get the maximum total beauty value, since selecting multiple neighbors of u would violate the condition that no two selected gardens share a common edge. If we select garden u, we cannot select any of its neighbors, so we need to select the gardens that are adjacent to them. Therefore, dp[u][1] includes the beauty value of garden u and the maximum total beauty value of the selected gardens among gardens 1 to (u-2) (inclusive) without selecting any neighbor of garden u.
The final answer is max(dp[N][0], dp[N][1]), as we can select either the last garden or not, whichever gives the maximum total beauty value.
The time complexity of the algorithm is O(N^2) and the space complexity is O(N).
Code:
The following is the Python code implementing the above algorithm:
def maxTotalBeautyOfTheGardens(N, beauties, edges):
dp = [[0, 0] for _ in range(N+1)]
dp[1][1] = beauties[1]
for u in range(2, N+1):
for v in edges[u-1]:
dp[u][0] = max(dp[u][0], max(dp[v][0], dp[v][1]))
dp[u][1] = max(dp[u][1], dp[v][0] + beauties[u])
return max(dp[N][0], dp[N][1])
Example:
Input: N = 4 beauties = [1, 2, 3, 4] edges = [[2, 3], [3, 4]] Output: 6
Explanation: We can select gardens 1 and 4 with a total beauty value of 5, or we can select gardens 1, 2, and 4 with a total beauty value of 6, which is the maximum possible total beauty value.
Maximum Total Beauty Of The Gardens Solution Code
1