Leetcode 3599 | Fixed Audio | Partition array to minimize xor | Leetcode weekly contest 456 | DP

preview_player
Показать описание
Below is the link of solution. Ask your doubts in comments below. For more clarity, refer the solution code. If you understand the solution, Like the video and Subscribe to my channel to get more updates

Комментарии
Автор

tried same but its giving tle

class Solution {

public:
int dp[251][251][251];
int f(int i, int j, vector<int>& nums, int k, int count, vector<int>&p){
if(count>k)
return INT_MAX;
if(j==nums.size()-1){
if(count!=k)
return INT_MAX;
int x=p[j];
if(i>0)
x=x^p[i-1];
return x;
}
if(dp[i][j][count]!=-1)
return dp[i][j][count];
int a=f(i, j+1, nums, k, count, p);
int x=p[j];
if(i>0)
x=x^p[i-1];
int b=max(x, f(j+1, j+1, nums, k, count+1, p));
return dp[i][j][count]=min(a, b);
}
int minXor(vector<int>& nums, int k) {
memset(dp, -1, sizeof(dp));
vector<int>p(nums.size());
p[0]=nums[0];
for(int i=1;i<nums.size();i++)
p[i]=p[i-1]^nums[i];
return f(0, 0, nums, k, 1, p);
}
};

shubhamdahiya
welcome to shbcf.ru