package com.fishercoder.solutions;

/**
 * 740. Delete and Earn
 *
 * Given an array nums of integers, you can perform operations on the array.
 * In each operation, you pick any nums[i] and delete it to earn nums[i] points. After, you must delete every element equal to nums[i] - 1 or nums[i] + 1.
 * You start with 0 points. Return the maximum number of points you can earn by applying such operations.

 Example 1:
 Input: nums = [3, 4, 2]
 Output: 6
 Explanation:
 Delete 4 to earn 4 points, consequently 3 is also deleted.
 Then, delete 2 to earn 2 points. 6 total points are earned.

 Example 2:
 Input: nums = [2, 2, 3, 3, 3, 4]
 Output: 9
 Explanation:
 Delete 3 to earn 3 points, deleting both 2's and the 4.
 Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
 9 total points are earned.

 Note:
 The length of nums is at most 20000.
 Each element nums[i] is an integer in the range [1, 10000].*/

public class _740 {
  public static class Solution1 {
    /**
     * Since the number is within range [1, 10000], we can build another array:
     * each number in the array denotes the total sum of this number that appears in this array
     * and
     * use the numbers themselves in the indices of another array
     *
     * credit: https://leetcode.com/problems/delete-and-earn/discuss/109895/JavaC++-Clean-Code-with-Explanation*/
    public int deleteAndEarn(int[] nums) {
      int n = 10001;
      int[] values = new int[n];
      for (int num : nums) {
        values[num] += num;
      }

      int take = 0;
      int skip = 0;
      for (int i = 0; i < n; i++) {
        int takeI = skip + values[i];
        int skipI = Math.max(skip, take);
        take = takeI;
        skip = skipI;
      }
      return Math.max(take, skip);
    }
  }
}

@@ -33,898 +33,8 @@
 s;%0A%0A
-/**%0A * 740. Delete and Earn%0A *%0A * Given an array nums of integers, you can perform operations on the array.%0A * In each operation, you pick any nums%5Bi%5D and delete it to earn nums%5Bi%5D points. After, you must delete every element equal to nums%5Bi%5D - 1 or nums%5Bi%5D + 1.%0A * You start with 0 points. Return the maximum number of points you can earn by applying such operations.%0A%0A Example 1:%0A Input: nums = %5B3, 4, 2%5D%0A Output: 6%0A Explanation:%0A Delete 4 to earn 4 points, consequently 3 is also deleted.%0A Then, delete 2 to earn 2 points. 6 total points are earned.%0A%0A Example 2:%0A Input: nums = %5B2, 2, 3, 3, 3, 4%5D%0A Output: 9%0A Explanation:%0A Delete 3 to earn 3 points, deleting both 2's and the 4.%0A Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.%0A 9 total points are earned.%0A%0A Note:%0A The length of nums is at most 20000.%0A Each element nums%5Bi%5D is an integer in the range %5B1, 10000%5D.*/%0A%0A
 publ
@@ -49,16 +49,18 @@
  _740 %7B%0A
+  
   public
@@ -93,12 +93,20 @@
     
+ 
+   
 /**%0A
+    
     
@@ -186,16 +186,20 @@
 y:%0A     
+ 
+   
 * each n
@@ -286,16 +286,20 @@
 ay%0A     
+    
 * and%0A  
@@ -301,16 +301,20 @@
 nd%0A     
+ 
+   
 * use th
@@ -371,18 +371,30 @@
 ay%0A     
-*%0A
+    * %3Cp%3E%0A    
      * c
@@ -500,11 +500,25 @@
 tion
-*/%0A
+%0A         */%0A    
     
@@ -558,24 +558,30 @@
 ms) %7B%0A      
+    
+  
 int n = 1000
@@ -583,16 +583,22 @@
  10001;%0A
+      
       in
@@ -622,16 +622,22 @@
 int%5Bn%5D;%0A
+      
       fo
@@ -665,16 +665,24 @@
         
+  
+      
 values%5Bn
@@ -703,17 +703,29 @@
     
-%7D%0A%0A
+      %7D%0A%0A    
       
+  
 int 
@@ -730,24 +730,30 @@
 t take = 0;%0A
+      
       int sk
@@ -756,24 +756,30 @@
 t skip = 0;%0A
+      
       for (i
@@ -802,32 +802,40 @@
  i++) %7B%0A        
+  
+      
 int takeI = skip
@@ -848,32 +848,40 @@
 ues%5Bi%5D;%0A        
+  
+      
 int skipI = Math
@@ -906,16 +906,24 @@
         
+  
+      
 take = t
@@ -936,16 +936,24 @@
         
+  
+      
 skip = s
@@ -964,18 +964,30 @@
 ;%0A      
+  
-%7D%0A
+    %7D%0A      
       re
@@ -1017,16 +1017,22 @@
 p);%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 740