您的位置:首页 > 其它

LeetCode | 740. Delete and Earn

2017-12-11 15:10 344 查看
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]
.

简单来说就是不断的取一个数,将数组中比这个数大一和小一的数全部删除,看最后得到的数的和,毫无疑问,这题使用DP来解决,不过我看网上的标准答案有BUG,题目并没有说数组中的数是连续的,而网上的标准答案默认了数组中的数是连续的,和容易就能举出不连续数组的反例,

通过这道题我收获了几个技巧,其一是以后在解决这样的题目的时候,对于同一个数多次出现的题,可以设定一个数组,num
用来记录n出现的次数,或者是n出现次数的和,

以后在解dp的题目的时候可能dp
,当n大于某个数的时候dp才有效,因此可以先把dp[1-n]先自己求出,当做特殊情况处理,然后在用公式解决n之后的值的dp

Sort(a.begin(),a.end())这个是对vector使用sort的技巧

 

class Solution {

    #include <algorithm>

public:

    int deleteAndEarn(vector<int>&nums) {

      sort(nums.begin(), nums.end());

     

      vector <int> numIndex; int j = 0;

      vector <int> numCount;

      int dp[20000];

      if (nums.size() == 0) return 0;

 

      for (int i = 0; i < nums.size(); i++)

      {

            if (nums[i] != nums[j]) {

                  numIndex.push_back(j);

                  numCount.push_back(nums[j] *(i - j));

                  j = i;

            }

      }

      numIndex.push_back(j);

      numCount.push_back(nums[j] * (nums.size() -j));

 

      dp[0] = numCount[0];

      if (numCount.size() == 1) return dp[0];

 

      if (abs(nums[numIndex[1]] -nums[numIndex[0]]) == 1)

      {

            dp[1] = max(numCount[0],numCount[1]);

      }

      else

      {

            dp[1] =numCount[0]+numCount[1];

      }

      if (numCount.size() == 2) return dp[1];

      if (abs(nums[numIndex[2]] -nums[numIndex[1]]) == 1)

      {

            if (abs(nums[numIndex[1]] -nums[numIndex[0]]) == 1)

            {

                  dp[2] = max(numCount[0] +numCount[2],numCount[1]);

            }

            else

            {

                  dp[2] = max(numCount[0] + numCount[1],numCount[0]+ numCount[2]);

            }

      }

      else

      {

            if (abs(nums[numIndex[1]] -nums[numIndex[0]]) == 1)

            {

                  dp[2] = max(numCount[0] +numCount[2], numCount[1] + numCount[2]);

            }

            else

            {

                  dp[2] = numCount[0] +numCount[1] + numCount[2];

            }

      }

 

      if (numCount.size() == 3) return dp[2];

 

      int maxNum = max(max(dp[0], dp[1]),dp[2]);

 

      for (int i = 3; i < numIndex.size();i++)

      {

            if (abs(nums[numIndex[i]] -nums[numIndex[i - 1]]) == 1)

            {

                  dp[i] = max(dp[i - 3] +numCount[i], dp[i - 2] + numCount[i]);

            }

            else

            {

                  if (abs(nums[numIndex[i - 1]]- nums[numIndex[i - 2]]) == 1)

                  {

                       dp[i] = max(dp[i - 2] +numCount[i], dp[i - 1] + numCount[i]);

                  }

                  else

                  {

                       dp[i] = dp[i - 1] +numCount[i];

                  }

            }

           

            if (dp[i] > maxNum) maxNum =dp[i];

      }

      return maxNum;

    }

    int max(int a,int b)

    {

        return a<b?b:a;

    }

};

4000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: