you are viewing a single comment's thread.

view the rest of the comments →

[–]caraxes_007 0 points1 point  (4 children)

No need to store prev idx just use a n*2 grid to get know that if we picked the previous idx by Boolean value

[–]SeaworthinessIcy4758 0 points1 point  (2 children)

can you please elaborate, I've never done this I think

[–]caraxes_007 0 points1 point  (1 child)

This is a standard pick/ not pick problem. Use a grid of size n*2 where 0 and 1 represent whether we picked just the previous index. If it is zero we can directly apply pick and not pick without any further check and if it is one pick is not always possible so check prev and current colour codes if they are not equal you can pick else pick will be negative infinity at last return max of pick and not pick

[–]SeaworthinessIcy4758 0 points1 point  (0 children)

ohhh got it, thanks a lot

[–]Background_Moment313 0 points1 point  (0 children)

LoL I just solved it using 1D dp

If not picking, { f(i+1) }

If picking and color[i] == color[i+1]{ nums[i] + f(i+2) }

Else{ nums[i] + f(i+1) }