Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8f0483d

Browse files
Dpcommit 2
1 parent 3082a46 commit 8f0483d

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

‎Competitive Coding/Dynamic Programming/cutRod/cutRod.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
#include<stdio.h>
22
#include<limits.h>
33

4+
// A utility function to get the maximum of two integers
45
int max(int a, int b)
56
{
67
return (a > b)? a : b;
78
}
89

10+
11+
/* Returns the best obtainable price for a rod of length n and
12+
price[] as prices of different pieces */
913
int DPcutRod(int price[], int n)
1014
{
1115
int val[n+1];
@@ -14,11 +18,12 @@ int DPcutRod(int price[], int n)
1418
for (i = 1; i<=n; i++)
1519
{
1620
int max_val = INT_MIN;
21+
// Recursively cut the rod in different pieces and compare different
22+
// configurations
1723
for (j = 0; j < i; j++)
1824
max_val = max(max_val, price[j] + val[i-j-1]);
1925
val[i] = max_val;
2026
}
21-
2227
return val[n];
2328
}
2429

‎Competitive Coding/Dynamic Programming/subsetSum/subsetSum.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
#include <stdio.h>
22

3+
// Returns true if there is a subset of a[] with sun equal to given sum
34
bool DP(int set[], int n, int sum)
45
{
6+
// The value of subset[i][j] will be true if there is a
7+
// subset of set[0..j-1] with sum equal to i
58
bool subset[n+1][sum+1];
9+
// If sum is 0, then answer is true
610
for (int i = 0; i <= n; i++)
711
subset[i][0] = true;
12+
// If sum is not 0 and set is empty, then answer is false
813
for (int i = 1; i <= sum; i++)
914
subset[0][i] = false;
15+
// Fill the subset table in botton up manner
1016
for (int i = 1; i <= n; i++)
1117
{
1218
for (int j = 1; j <= sum; j++)

‎Competitive Coding/Dynamic Programming/uglyNumbers/uglyNumbers.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ int main()
66
int t; cin >> t;
77
int i2, i3, i5;
88
i2 = i3 = i5 = 0;
9-
int ugly[500];
9+
int ugly[500];// To store ugly numbers
1010
ugly[0] = 1;
1111
for (int i = 1; i < 500; ++i) {
12-
ugly[i] = min(ugly[i2]*2, min(ugly[i3]*3, ugly[i5]*5));
13-
ugly[i] != ugly[i2]*2 ? : i2++;
14-
ugly[i] != ugly[i3]*3? : i3++;
15-
ugly[i] != ugly[i5]*5 ? : i5++;
12+
ugly[i] = min(ugly[i2]*2, min(ugly[i3]*3, ugly[i5]*5));//get minimum of possible solutions
13+
ugly[i] != ugly[i2]*2 ? : i2++;// next_mulitple_of_2 = ugly[i2]*2;
14+
ugly[i] != ugly[i3]*3? : i3++;// next_mulitple_of_3 = ugly[i3]*3
15+
ugly[i] != ugly[i5]*5 ? : i5++;// next_mulitple_of_5 = ugly[i5]*5;
1616
}
1717
int n;
1818
while(t--) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /