Partition Array for Maximum Sum | Why DP | Recursion | Memo | Bottom Up | Leetcode 1043

preview_player
Показать описание
This is the 85th Video of our Playlist "Dynamic Programming : Popular Interview Problems".
In this video we will try to solve a very good and classic DP problem based on array partition - Partition Array for Maximum Sum | Leetcode 1043

I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.

Problem Name : Partition Array for Maximum Sum | Leetcode 1043
Company Tags : will update soon

Approach-1 Summary : The approach uses a recursive dynamic programming strategy with memoization to avoid redundant calculations. The function solve recursively explores different partitioning options, keeping track of the maximum sum encountered. The memoization array t is used to store and retrieve previously computed results, reducing the time complexity. The main function initializes necessary variables, sets up memoization, and calls the recursive solve function to obtain the maximum sum after partitioning.

Approach-2 Summary : The approach uses dynamic programming to iteratively compute the maximum sum for each partition. The array t is used to store intermediate results, where t[i] represents the maximum sum for the partition arr[0 to i]. The code iterates through each position in the array, considering partitions of size at most k. It calculates the current maximum within the partition and updates the maximum sum for the current position based on the best result from previous partitions. The final result is the maximum sum for the entire array, stored in t[n]. This approach avoids recursion and efficiently computes the maximum sum after partitioning.

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

✨ Timelines✨
00:00 - Introduction

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #2024 #newyear
Рекомендации по теме
Комментарии
Автор

//TOP DOWN TO BOTTOM UP
int solve (vector<int>& arr, int k, int idx){
if (idx == arr.size()){
return 0;
}
if (dp[idx] != -1)
return dp[idx];

int maxE = INT_MIN, sum = INT_MIN, n = arr.size();
for(int i = idx; i < min(n, idx+k); i++){
maxE = max(maxE, arr[i]);
sum = max(sum, maxE*(i-idx+1) + solve(arr, k, i+1));
}
return dp[idx] = sum;
}

//USING SAME CODE COPY PASTE!
int arr, int k) {
int n = arr.size();
//base case n+1 is 0 in dp!
vector<int>dp(n+1, 0);

for(int idx = n-1; idx >= 0; idx--){
int maxE = -1;
for(int i = idx; i < min(n, idx+k); i++){
maxE = max(maxE, arr[i]);
dp[idx] = max(dp[idx], maxE*(i-idx+1) + dp[i+1]);
}
}
return dp[0];
}

theOmKumar
Автор

mera ho gaya ye!!! i'm watching you dp playlist from last 1.5 weeks, and this was my 1st dp problem that i have solved myself, thank you so much for everything!!! Your way of teaching is so good!!! thnk youuu

dvghf
Автор

I was able to solve with bottom up just by following your playlist. I derived from the recursion memo but couldn’t think of your bottom up. That was unique 👌🏻

aws_handles
Автор

// Bottom-Up approach😊
int solveTab(vector<int>arr, int k){
vector<int>dp(arr.size()+1, 0);
for(int
int max_val=0;
int res=0;
for(int j=index;j<arr.size() && (j-index+1)<=k;j++){
max_val= max(max_val, arr[j]);
res = max(res,
}
dp[index]=res;
}
return dp[0];
}


nayan_CSE
Автор

Amazing explanation as always. You have improved my DP ❤️

DevOpskagyaan
Автор

You are the best mik. Thank you for working so hard and helping us also

souravjoshi
Автор

Thank youuu for the amazing explaination.

anonymous
Автор

Observations:
The best part I learnt today is when I need the maximum sum I calculated the max in the subarray again but got to know it take every time o(n) time which increase the tc so instead we can maintain a maxi and do it in o(1) operations
Done✅🙇‍♂

jagadeeshp
Автор

was able to solve by recursion + memo thanks to you.

theOmKumar
Автор

Thanks a lot. Was waiting for you 👌🏻👌🏻

aws_handles
Автор

it was a great explantion bro, ,thanks

CodeMode
Автор

bhaiya recursion k under jane par confuse ho jata hun itana sarar recusrion calls
story kaise banau

MakeuPerfect
Автор

Space Optimized In Bottom Up O( k )

int arr, int k)
{
vector<int> dp(k);

for(int i=arr.size()-1;i>=0;i--)
{
int maxi=0;
int sum=0;

for(int j=i;j<i+k && j<arr.size();j++)
{
maxi=max(maxi, arr[j]);
int so;
if(j+1>=arr.size())
{
so=0;
}
else
{
so=dp[(j+1)%k];
}
sum=max(sum, so+(j-i+1)*maxi);
}
dp[i%k]=sum;
}
return dp[0];
}



//Bottom Up
class Solution {
public:

int arr, int k)
{
vector<int> dp(arr.size());

for(int i=arr.size()-1;i>=0;i--)
{
int maxi=0;
int sum=0;

for(int j=i;j<i+k && j<arr.size();j++)
{
maxi=max(maxi, arr[j]);
int so;
if(j+1>=arr.size())
{
so=0;
}
else
{
so=dp[j+1];
}
sum=max(sum, so+(j-i+1)*maxi);
}
dp[i]=sum;
}
return dp[0];
}
};

sooyashjaju
Автор

can you explained how we got 9 at middle ones because there is one subarray possible which is 15 there should be 15

rahuljha
Автор

Bhai 2035 wala question banao please....

mahakaalgaming
Автор

Bhaiya ajj(4th feb) ka GFG POTD pls karwa dena pls pls

newglobal
Автор

HW for same code in java:-
public int maxSumAfterPartitioning(int[] arr, int k) {
int n = arr.length;
int[] dp = new int[n+1];
for (int index = n - 1; index >= 0; index--) {
int curr_max = -1;
for(int j = index; j<arr.length && j-index+1 <= k; j++){
curr_max = Math.max(curr_max, arr[j]);
dp[index] = Math.max(dp[index], (j-index+1)*curr_max + dp[j+1] );
}
}
return dp[0];
}

mohdtalib
Автор

in bottom up why did u multiple currmax*j +t[i-j] we can also do currmax+t[i-j] also ryt?

aishwaryap.s.v.s
Автор

i know i was dp but there is no way I could come up with approach

bhuppidhamii
Автор

bhiya at most k na lekr poora k size subarray hi le lete tab pakka hi maxSum aayega na. Agar aise karenge toh kam recusrion mein kaam bhi hojayyega.

molyoxide
join shbcf.ru