Get Maximum in Generated Array | dynamic programming | leetcode 1646

Pubblicato il: 01 settembre 2021
sul canale di: Joey'sTech
256
10

Hey there, I am Joey and you are watching a video from my dynamic programming tutorial.
I am going to explain to you a dynamic programming problem from the website of Leetcode - get maximum in generated array.

You are given an integer n.
The problem talks about an array 'nums' of length n+1 that is produced using the below formula -

All you have to do is find out the maximum value from this array nums.

The problem is about constructing the array using dynamic programming that means you should use the subproblems to calculate the value of the next subproblem.

So, before we begin solving this DP problem I want you to subscribe to my channel and hit the bell icon as that way you will get notified when I release new videos like these.

Like I said we will first create the array and then it becomes very easy to find the maximum value.

Let’s try to find out the pattern first so that we can use the existing subproblems to determine the value of the new subproblems.

You see we have 0 in the first cell and 1 in the second cell. And for the next cell the index will be 2*1 which will turn to 1 as per the question because when the formula is nums[2*i] then it will change to nums[i].

For the next cell, the index will be 2*1 + 1 which will lead to nums[1] + nums[2] as the formula for odd index is nums[2*i + 1] = nums[i] + nums[i+1]

So, for an even index the subproblem’s index from which we are going to take the value can be determined by dividing it by 2.

For the odd index, the subproblem’s index can be determined if I subtract 1 from the odd index and divide it by 2 and for the other subproblem, which is this , add 1 to the result .

So if the index is 3, I subtract 1 from 3 for which we get 2. Divide it by 2 and we get 1 as the index of our first subproblem and now the other component will be 1+1 so we get this component the index of which will be 2. Hence it will be 1+1 which gives us 2.

I think we have figured out our pattern so let’s quickly generate the array.


In questa pagina del sito puoi guardare il video online Get Maximum in Generated Array | dynamic programming | leetcode 1646 della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Joey'sTech 01 settembre 2021, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 256 volte e gli è piaciuto 10 spettatori. Buona visione!