# Dynamic Programming and the hardest problem on HackerRank

The hardest problem on HackerRank, sorted by Max Score and level “Expert” is Separate The Chocolate. It’s worth 250 points and the level “Expert” is the highest one. How to solve it?

## Problem Statement

Tom and Derpina have a rectangular shaped chocolate bar with chocolates labeled T, D and U. They want to split the bar into exactly two pieces such that:

- Tom's piece can not contain any chocolate labeled D and similarly, Derpina's piece can not contain any chocolate labeled T and U can be used by either of the two.
- All chocolates in each piece must be connected (two chocolates are connected if they share an edge), i.e. the chocolates should form one connected component
- The absolute difference between the number of chocolates in pieces should be at most K
- After dividing it into exactly two pieces, in any piece, there should not be 4 adjacent chocolates that form a square, i.e. there should not be a fragment like this:

XX

XXInput Format

The first line of the input contains 3 integers M, N and K separated by a single space.

M lines follow, each of which contains N characters. Each character is 'T','D' or 'U'.Constraints

0≤ M, N ≤8

0≤ K ≤ M * NOutput FormatA single line containing the number of ways to divide the chocolate bar.

## My approach

I didn't have any ideas how to solve it with DP. I've coded a solution which checks every possible way of chocolate division. I felt it wasn't the right solution because the complexity of that is exponential. The graph of possible permutations is checked wisely - at each "branch" I check the required conditions (connectivity, if K difference can be met, squares presence) and cancel the computation of subtrees with invalid state. The result? Quite nice: 16 out of 19 tests passed, 210.53 out of 250 points scored. I looked at the leaderboard, and noticed despite the winners many scored just like me. I've optimized some cases, however that wasn't enough. DP was indeed needed. I've tried to figure it out but finally gave up.

## The solution

The whole editorial provided by HackerRank is very short:

This problem is a complicated dynamic programming problem (DP). The idea is simple but the implementation is difficult.We can iterate the grids one by one. For each grid suppose the left and the upper one has been given to Tom or Derpina. (Color black or white.)To decide the square [i][j], we need all the square’s state in square[i][0..j – 1] and square[i – 1][j..n -1] , all the (n + 1) squares in total can decide this square’s state. We can use these as a part of state and we also must keep whether a component is dead. (If it’s dead then add one more square of the same color is invalid.)

I can't wrap my head around the DP function though. It is strange for me that the vertical line will be the DP state - because the connectivity must be met for the whole matrix... anyone got an explanation?

## Lessons learned

- If you can solve a part of a very hard problem - it still can be very profitable in terms of HackerRank score. Compare 210.53 points to 50 points from ["Two Robots" medium problem](/2016/11/dynamic-programming-in-5-easy-steps---examples---two-robots).
- Looking at other's solutions, I've noticed this piece of code: