File tree Expand file tree Collapse file tree
algorithms/dynamic_programming Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # Concept is almost same as 01 Knapsack Problem
2+
3+ def min_coin (coins , total ):
4+ cols = total + 1
5+ rows = len (coins )
6+
7+ t = [ [0 ] if col == 0 else float ('inf' ) for col in range (cols )] for i in range (rows )]
8+
9+ for i in range (rows ):
10+ for j in range (1 , cols ):
11+ if j < coins [i ]:
12+ t [i ][j ] = t [i - 1 ][j ]
13+ else :
14+ t [i ][j ] = min (t [i - 1 ][j ], 1 + t [i ][j - coins [i ])
15+
16+ return t [rows - 1 ][cols - 1 ]
Original file line number Diff line number Diff line change 1+ def lcs (s1 , s2 ):
2+ cols = len (s1 ) + 1
3+ rows = len (s2 ) + 1
4+
5+ t = [[0 for i in range (cols )] for i in range (rows )]
6+
7+ max_length = 0
8+
9+ for i in range (1 , rows ):
10+ for j in range (1 , cols ):
11+ if s2 [i - 1 ] == s1 [j - 1 ]:
12+ t [i ][j ] = 1 + t [i - 1 ][j - 1 ]
13+ else :
14+ t [i ][j ] = max (t [i - 1 ][j ], t [i ][j - 1 ])
15+
16+ max_length = max (max_length , t [i ][j ])
17+
18+ return max_length
Original file line number Diff line number Diff line change 1+ def lcs (s1 , s2 ):
2+ cols = len (s1 ) + 1
3+ rows = len (s2 ) + 1
4+
5+ t = [[0 for i in range (cols )] for i in range (rows )]
6+
7+ max_length = 0
8+
9+ for i in range (1 , rows ):
10+ for j in range (1 , cols ):
11+ if s [i - 1 ] == s [j - 1 ]:
12+ t [i ][j ] = t [i - 1 ][j - 1 ] + 1
13+ max_length = max (max_length , t [i ][j ])
14+
15+ return max_length
You can’t perform that action at this time.
0 commit comments