79237469

Date: 2024-11-29 14:49:13
Score: 0.5
Natty:
Report link

class Solution { public: long long dp[105][105][105]; int fun(int ind, vector&nums, int op1,int op2, int k){

    if(ind<0){
        return 0;
    }
    if(op1<=0 and op2<=0){
        int sum =0;
        while(ind>=0){
            sum+=nums[ind];
            ind--;
        }
        return sum;
    }
    if(dp[op1][op2][ind]!=-1){
        return dp[op1][op2][ind];
    }
    
    int x=INT_MAX,y=INT_MAX,z=INT_MAX,a=INT_MAX;
    int curr=nums[ind];
    if(op2>0 and nums[ind]>=k){
        x = nums[ind]-k + fun(ind-1,nums,op1,op2-1,k);
    }
    if(op1>0){
        int temp = (curr+1)/2;
        y = temp + fun(ind-1,nums, op1-1,op2,k);
    }
    if(op2>0 and curr>=k and op1>0){
        int temp1 = (curr-k+1)/2;
        int temp2 = (curr+1)/2 >=k ?((curr+1)/2)-k: INT_MAX;
        z = min(temp1,temp2) + fun(ind-1,nums,op1-1,op2-1,k);
    }
    a = nums[ind]+fun(ind - 1, nums, op1, op2, k);
    return dp[op1][op2][ind]=min({x,y,z,a});
}

int minArraySum(vector<int>& nums, int k, int op1, int op2) {
    memset(dp,-1,sizeof(dp));
    return fun(nums.size()-1,nums,op1,op2,k);
}

};

This solution worked for me

Thanks to @unmitigated

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Whitelisted phrase (-1): worked for me
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @unmitigated
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: snehith oddula