получая использование кучи после бесплатной ошибки в Leetcode, кажется, не понимаю причину root. Можете ли вы помочь мне здесь?
В основном все объявлено в стеке. Единственное, что я подозреваю, - это мелкая копия, которую я создаю в стеке для мультимножества temp, но вы не можете освободить что-либо в стеке прямо, не созданное в куче?
class Solution {
public:
void earn_points(multiset<int> points,int currpoint, int& max){
set<int> unique;
if(points.size() == 0){
cout <<"finalscore="<< currpoint << " "<<endl;
if(currpoint > max){
max = currpoint;
}
}
multiset<int> temp = points;
for(auto it=points.begin(); it != points.end(); ++it){
int num = *it;
if(unique.find(num) != unique.end()){
continue;
}
unique.insert(num);
int delete_num1 = num + 1;
int delete_num2 = num - 1;
points.erase(it);
if(points.find(delete_num1) != points.end())
points.erase(delete_num1);
if(points.find(delete_num2) != points.end())
points.erase(delete_num2);
cout << num <<" ";
for(auto i : points){
cout << i <<" ";
}
cout << endl;
earn_points(points,currpoint + num,max);
points = temp;
}
}
int deleteAndEarn(vector<int>& nums) {
multiset<int> points(nums.begin(),nums.end());
int max = INT32_MIN;
earn_points(points,0,max);
return max;
}
};