Advertisement

Maximum Total Damage With Spell Casting - LeetCode 3186 Solution

Maximum Total Damage With Spell Casting - Complete Solution Guide

Maximum Total Damage With Spell Casting is LeetCode problem 3186, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

A magician has various spells. You are given an array power , where each element represents the damage of a spell. Multiple spells can have the same damage value. It is a known fact that if a magician decides to cast a spell with a damage of power[i] , they cannot cast any spell with a damage of power[i] - 2 , power[i] - 1 , power[i] + 1 , or power[i] + 2 . Each spell can be cast only once . Return the maximum possible total damage that a magician can cast. Example 1: Input: power = [1,1,3,4] Ou

Detailed Explanation

The problem asks us to find the maximum total damage a magician can cast, given an array 'power' representing the damage of each spell. There's a constraint: if a spell with damage 'power[i]' is cast, spells with damages 'power[i] - 2', 'power[i] - 1', 'power[i] + 1', and 'power[i] + 2' cannot be cast. Each spell can only be cast once. In essence, we need to select a subset of spells maximizing the sum of their damage, subject to the given adjacency constraint.

Solution Approach

The solution uses dynamic programming to determine the maximum damage. It first counts the occurrences of each spell damage and then sorts the unique spell damage values. A DP array 'dp' is created, where dp[i] represents the maximum damage achievable considering spells up to the i-th unique damage value. For each unique damage value, we have two choices: either include it in the solution or skip it. If we include it, we add its total damage (damage * count) to the maximum damage achievable using compatible spells (those with damage less than current damage - 2). If we skip it, the maximum damage remains the same as the previous damage. The maximum of these two choices is stored in dp[i].

Step-by-Step Algorithm

  1. Step 1: Count the occurrences of each spell damage value using a HashMap/Counter.
  2. Step 2: Extract the unique spell damage values from the HashMap/Counter and sort them in ascending order.
  3. Step 3: Initialize a dynamic programming array 'dp' of size equal to the number of unique damage values.
  4. Step 4: Iterate through the sorted unique damage values. For each damage value 'p', determine the index 'j' of the largest damage value that is strictly less than 'p - 2'. This represents the last 'compatible' power.
  5. Step 5: Calculate the potential damage if 'p' is included: (p * count[p]) + dp[j - 1] (if j > 0, otherwise 0).
  6. Step 6: Calculate the potential damage if 'p' is excluded: dp[i - 1] (if i > 0, otherwise 0).
  7. Step 7: Store the maximum of the two potential damages in dp[i].
  8. Step 8: After iterating through all unique damage values, the last element of 'dp' contains the maximum total damage.

Key Insights

  • Insight 1: The problem can be modeled as finding the maximum weight independent set on a line graph where nodes are spell damages and edges connect incompatible damages.
  • Insight 2: Dynamic programming is a suitable technique because the problem exhibits overlapping subproblems (calculating the maximum damage for sub-arrays of spells).
  • Insight 3: Sorting the spell damages simplifies the dynamic programming process as we can iterate through them in order and consider whether or not to include each spell in our optimal solution.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Two Pointers, Binary Search, Dynamic Programming, Sorting, Counting.

Companies

Asked at: Citadel, PhonePe.