Skip to content

Commit 07695c7

Browse files
authored
Update Dynamic-Programming.md
1 parent c972696 commit 07695c7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Notes/Dynamic-Programming.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,26 @@ Now that the table is filled out, we can solve our original problem, where _i_ =
284284

285285
So our answer is: `dp[ 5 ][ 6 ]` = 60
286286

287+
```java
288+
for ( int i = 1; i <= N; i++ )
289+
{
290+
for ( int j = 0; j <= W; j++)
291+
{
292+
if ( weight[ i ] >= j )
293+
{
294+
if ( value[ i ] + dp[ i - 1 ][ j - weight[ i ] ] > dp[ i - 1 ][ j ] )
295+
{
296+
dp[ i ][ j ] = value[ i ] + dp[ i - 1 ][ j - weight[ i ] ];
297+
}
298+
else
299+
{
300+
dp[ i ][ j ] = dp[ i - 1 ][ j ];
301+
}
302+
}
303+
}
304+
}
305+
```
306+
287307
### Top-Down Solution
288308

289309
To solve the problem using the top-down approach, we will try to come up with some recursive function that will give us the answer.

0 commit comments

Comments
 (0)