Showing posts with label dp. Show all posts
Showing posts with label dp. Show all posts

Wednesday, December 13, 2017

DP problem solving

The problem I will be solving is Sums in a Triangle .The problem basically asking us to find the maximum sum in a single path from the given triangle and path can be 1st rows number+ next rows number from the same column or current column+1 and then next row and so on.
At a glance the solution that comes in mind is that we'll take the number from first row and then from next row we'll take the number from same column or current column+1 depending on which one is maximum. But this is a greedy approach and if you see the 2nd test case, you'll see that this approach doesn't give us the correct answer. the second test case:
1
1 2
4 1 2
2 3 1 1
Here if we take 1 then if we follow greedy approach and take 2 from 2nd row because that is the maximum between 1 and 2, then from 3rd row we can't take 4. Because the constraints says the path can be the below row and same column or current column+1(that means one place to the right) so, the answer we'll get in this approach is 6 which is wrong.

But if we use bottom up approach we'll get correct answer. In bottom up approach we'll divide the triangle into smaller triangles. And as the name suggest we'll start from the bottom(last row). So, if we draw the the triangle in  row and column:



If we start from row 4 column 1 and row 4 column 2, between these two if we add 3 with 4 we'll get the maximum number, so the answer for row 3 column 1 is 7. And if we follow  same method we'll get answer 4 for row 3 column 2. Calculating all the rows we'll get a triangle like below:

We can see at the 1st row we get the result. In the beginning I've said in bottom up approach we'll divide it in smaller triangle and that's what we did. We took row 4  column 1, row 4 column 2 and row 3 column 1 as a smaller triangle and that gave us the answer for row 3 column 1. And that's how we got the result for other rows also.

Now let's get to the coding part. as we are dealing with rows and columns ,it is obvious that we have to take a 2d array and and take input.
Now let's implement the bottom up approach. What we are doing is we are starting from the last row and checking maximum value of two column of that row till the column number is one less than the row number:
If you notice I'm running the outer loop till 2 , though I've take input from 1. This is because, we are storing the result in a[i-1][j] by adding it with the maximum of a[i][j] and a[i][j+1], if we run loop till 1 when i=1, we'll have  a[0][j] where we don't have any value.  And then in the inner loop we're just checking which one is maximum and adding it with the previous rows number, exactly what we did in the table earlier. And at the end we are printing the number from first row, because that's where we get the result duh! (remember the table ;) ).
The whole code looks like this:

And the reason it is a dp problem  is because we could divide it in sub problem and at the end got result by solving those sub problems.

Thursday, October 19, 2017

Density Independent Pixel


In Android material design first thing that we have to think about is if the app looks as expected on all devices irrespective of the screen size. That's where density independent pixel (or DP) comes into action. Density independent pixel allows us to create assets that appears in an expected way across all devices no matter what is the resolution or density of a target device. Basically, it gives your app the consistent look that you expected.

For example, if you give a the size of a button 20px that may look great in the device that you are testing your app on, but if you run it on a different device it may not look the way you intended it to look. That's because when you set the size on pixel,it is fixed therefor in a small device if  it looks good then in a bigger device it will look really small.

So, I just said that if we give px(pixel) unit, it is fixed but what happens when we give dp (density independent pixel) unit. At runtime the system handles any scaling of the dp units. One dp is equivalent to 1px in a 160dpi(dots per inch) screen, this is the baseline  density.

The conversion equation of px and dp is:

                                                px/dp = dpi/160dpi

If we have a a screen with 213dpi screen then we'll get 1dp equal to 1.3px.

                                               px = dp*(dpi/160dpi) = dp*(213/160 ) = 1.3dp
So, this is what I know about dp. If there's any mistakes feel free to point it out but be kind :) .

Introduction To Binary Search Tree

A tree data structure is a way to hold data that looks like a tree when it's visualized. For example: All data points in a tre...