Class Solution

java.lang.Object
g0701_0800.s0740_delete_and_earn.Solution

public class Solution extends Object
740 - Delete and Earn.<p>Medium</p> <p>You are given an integer array <code>nums</code>. You want to maximize the number of points you get by performing the following operation any number of times:</p> <ul> <li>Pick any <code>nums[i]</code> and delete it to earn <code>nums[i]</code> points. Afterwards, you must delete <strong>every</strong> element equal to <code>nums[i] - 1</code> and <strong>every</strong> element equal to <code>nums[i] + 1</code>.</li> </ul> <p>Return <em>the <strong>maximum number of points</strong> you can earn by applying the above operation some number of times</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [3,4,2]</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> You can perform the following operations:</p> <ul> <li> <p>Delete 4 to earn 4 points. Consequently, 3 is also deleted. nums = [2].</p> </li> <li> <p>Delete 2 to earn 2 points. nums = [].</p> </li> </ul> <p>You earn a total of 6 points.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [2,2,3,3,3,4]</p> <p><strong>Output:</strong> 9</p> <p><strong>Explanation:</strong> You can perform the following operations:</p> <ul> <li> <p>Delete a 3 to earn 3 points. All 2&rsquo;s and 4&rsquo;s are also deleted. nums = [3,3].</p> </li> <li> <p>Delete a 3 again to earn 3 points. nums = [3].</p> </li> <li> <p>Delete a 3 once more to earn 3 points. nums = [].</p> </li> </ul> <p>You earn a total of 9 points.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li> <li><code>1 <= nums[i] <= 10<sup>4</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • deleteAndEarn

      public int deleteAndEarn(int[] nums)