Skip to content

Commit 9e9a7e4

Browse files
committed
new question added
1 parent c26588b commit 9e9a7e4

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ nothing but fibonacci series.
178178
- In some algos involving DP you can start from n and in that case answer to n is dependent on n-1 and so on.
179179
- Most problems involving strings can be taken as S(i,j), either we compare last characters then if equal
180180
it gets converted to i-1,j-1 or we take min or max of i,j-1 and i-1,j
181+
- Somtimes if the problem is already a table, you need to reverse engineer it to find the min number of ways to do something. Example create a new table and start from the very bottom and construct the solution updwards. Answer in that case will be the cell 0,0
181182

182183
# Topic0: Programming Questions
183184

@@ -401,6 +402,7 @@ it gets converted to i-1,j-1 or we take min or max of i,j-1 and i-1,j
401402
- [Count the number of times string occured as the subsequence of the other string](/dynamic-programming/question25.c)
402403
- [Given an amount and some coints, find out in how many ways can this amount be formed](/dynamic-programming/question26.c)
403404
- [Given a 2xn board and tiles of size 2x1, count the number of ways to fill the board using 2x1 tiles](/dynamic-programming/question27.c)
405+
- [Given a cost matrix mxn having a cost at each cell. Find the min cost that it will take to reach cell (m-1,n-1) from top left corner cell (0,0) if the only allowed directions to move are right, down and diagnal down](/dynamic-programming/question28.c)
404406

405407
## Some important concepts to solve algos better
406408

dynamic-programming/question28.c

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
Given a cost matrix mxn having a cost at each cell. Find the min cost that it will take to
3+
reach cell (m-1,n-1) from top left corner cell (0,0) if the only allowed directions to move are right,
4+
down and diagnal down
5+
6+
METHOD:
7+
In this case we cannot use greedy as each time we will try to find the min from the node and traverse that
8+
path, but we may end up with a node having max as the only option or min as the max number which may not
9+
be the shortest path
10+
11+
Hence we use DP:
12+
Assume the matrix is something like this:
13+
14+
10 3 4
15+
5 6 7
16+
13 4 11
17+
18+
and we need to reach from 0,0 to n-1,m-1
19+
20+
Therefore at each node I have three choices, recursive equation may look like this:
21+
22+
C(0,0) = min{
23+
10 + C(0,1),
24+
10 + C(1,1),
25+
10 + C(1,0)
26+
}
27+
28+
Hence if we convert it into a tree, we can see repeating sub problems. The number of unique problems are clearly
29+
n*m and c(i,j)// i can take n values and j can take m values (entire row and columns)
30+
31+
Now we make a matrix of size m*n and reverse engineer the solution. If we start from 0,0, it is the bigger
32+
problem we need to solve, but if we start from n-1,m-1, it is the smallest problem. Hence we try to construct
33+
the bottom most row, as
34+
35+
28 15 11
36+
37+
as from cell in the middle it will take (4+11) 15 cost to reach 11 and similarly from cell 13 it will take(15+13)
38+
39+
Now for the next rows, we keep taking the min of the three ways we have been given in the question and 0,0
40+
will be the answer.
41+
42+
Time complexity: O(mn)
43+
Space complexity: O(mn)
44+
45+
Here we can even construct the path by just finding min starting from 0,0 in this matrix
46+
We can also save the space complexity in case path is not required by just maintaing the values of two rows
47+
at a time at any given time.
48+
*/
49+
#include <stdio.h>
50+
#include <stdlib.h>
51+
52+
int min(int a, int b, int c){
53+
return (a<b?(a<c?a:c):(b<c?b:c));
54+
}
55+
56+
int findCost(int rows, int columns,int arr[rows][columns]){
57+
58+
int cost[rows][columns];
59+
60+
int i,j;
61+
62+
cost[rows-1][columns-1] = arr[rows-1][columns-1];
63+
64+
for(i=rows-1;i>=0;i--){
65+
for(j=columns-1;j>=0;j--){
66+
if(i == rows-1){
67+
cost[i][j] = arr[i][j] + cost[i][j+1];
68+
}
69+
else if(j == columns-1){
70+
cost[i][j] = arr[i][j] + cost[i+1][j];
71+
}else{
72+
cost[i][j] = arr[i][j] + min(cost[i+1][j+1],cost[i][j+1],cost[i+1][j]);
73+
}
74+
}
75+
}
76+
77+
78+
//printing the array
79+
for(i=0;i<rows;i++){
80+
for(j=0;j<columns;j++){
81+
printf("%d ", cost[i][j]);
82+
}
83+
printf("\n");
84+
}
85+
86+
printf("the path is: \n");
87+
i=0,j=0;
88+
while(i<=rows-1 && j<=columns-1){
89+
printf("%d ", arr[i][j]);
90+
if(cost[i+1][j+1] < cost[i+1][j]){
91+
if(cost[i+1][j+1] < cost[i][j+1]){
92+
i = i+1;
93+
j = j+1;
94+
}else{
95+
i = i;
96+
j = j+1;
97+
}
98+
}else{
99+
if(cost[i+1][j] < cost[i][j+1]){
100+
i = i+1;
101+
j = j;
102+
}else{
103+
i = i;
104+
j = j+1;
105+
}
106+
}
107+
}
108+
109+
return cost[0][0];
110+
}
111+
112+
int main(){
113+
114+
int cost[3][3] = {
115+
{10,3,4},
116+
{5,6,17},
117+
{13,4,11},
118+
};
119+
120+
int rows = sizeof(cost)/sizeof(cost[0]);
121+
int columns = sizeof(cost[0])/sizeof(cost[0][0]);
122+
123+
int total = findCost(rows,columns,cost);
124+
printf("total cost of reaching is %d\n", total);
125+
126+
return 0;
127+
}
128+

nextquestions.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ GOOD QUESTIONS:
22
- question18.c (arrays)
33

44

5+
C concepts
6+
- dynamic memory allocation for 2d arrays
7+
- fgets,scanf, getc etc difference and when to use what
8+
59
TODO:
610
- question14.c (general),
711
- question17.c (arrays), to be done after trees and linkedlist is done

0 commit comments

Comments
 (0)