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 3082a46

Browse files
Added DP problems
1 parent 685b485 commit 3082a46

File tree

8 files changed

+237
-0
lines changed

8 files changed

+237
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Given a rod of length n inches and an array of prices that contains prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces. For example, if length of the rod is 8 and the values of different pieces are given as following, then the maximum obtainable value is 22 (by cutting in two pieces of lengths 2 and 6)
2+
3+
length | 1 2 3 4 5 6 7 8
4+
--------------------------------------------
5+
price | 1 5 8 9 10 17 17 20
6+
7+
1) Optimal Substructure:
8+
We can get the best price by making a cut at different positions and comparing the values obtained after a cut. We can recursively call the same function for a piece obtained after a cut.
9+
10+
Let cutRoad(n) be the required (best possible price) value for a rod of lenght n. cutRod(n) can be written as following.
11+
12+
cutRod(n) = max(price[i] + cutRod(n-i-1)) for all i in {0, 1 .. n-1}
13+
14+
2) Overlapping Subproblems
15+
Like other typical Dynamic Programming(DP) problems, recomputations of same subproblems can be avoided by constructing a temporary array val[] in bottom up manner.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<stdio.h>
2+
#include<limits.h>
3+
4+
int max(int a, int b)
5+
{
6+
return (a > b)? a : b;
7+
}
8+
9+
int DPcutRod(int price[], int n)
10+
{
11+
int val[n+1];
12+
val[0] = 0;
13+
int i, j;
14+
for (i = 1; i<=n; i++)
15+
{
16+
int max_val = INT_MIN;
17+
for (j = 0; j < i; j++)
18+
max_val = max(max_val, price[j] + val[i-j-1]);
19+
val[i] = max_val;
20+
}
21+
22+
return val[n];
23+
}
24+
25+
int main()
26+
{
27+
int n;
28+
printf("Enter rod length\n");
29+
scanf("%d",&n);
30+
int arr[n],i;
31+
printf("Enter prices for lengths 1 to n\n");
32+
for(i=0;i<n;i++)
33+
{
34+
scanf("%d",&arr[i]);
35+
}
36+
printf("Maximum Obtainable Value is %d\n", DPcutRod(arr, n));
37+
getchar();
38+
return 0;
39+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Given a cost matrix cost[][] and a position (m, n) in cost[][], write a function that returns cost of minimum cost path to reach (m, n) from (0, 0). Each cell of the matrix represents a cost to traverse through that cell. Total cost of a path to reach (m, n) is sum of all the costs on that path (including both source and destination). You can only traverse down, right and diagonally lower cells from a given cell, i.e., from a given cell (i, j), cells (i+1, j), (i, j+1) and (i+1, j+1) can be traversed.
2+
3+
1) Optimal Substructure
4+
The path to reach (m, n) must be through one of the 3 cells: (m-1, n-1) or (m-1, n) or (m, n-1). So minimum cost to reach (m, n) can be written as "minimum of the 3 cells plus cost[m][n]".
5+
6+
minCost(m, n) = min (minCost(m-1, n-1), minCost(m-1, n), minCost(m, n-1)) + cost[m][n]
7+
8+
2) Overlapping Subproblems
9+
Like other typical Dynamic Programming(DP) problems, recomputations of same subproblems can be
10+
avoided by constructing a temporary array tc[][] in bottom up manner.
11+
12+
13+
Example:
14+
3 3
15+
1 2 3
16+
4 8 2
17+
1 5 3
18+
19+
Ouptut:
20+
8
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
int main(){
7+
std::ios::sync_with_stdio(false);
8+
/*
9+
We can easily observe that the optimum solution of the minimum
10+
cost at any row i is only dependent on the cost at row i-1 and
11+
the current row elements. Thus if we only store these two arrays
12+
of length c, We have reduced space from n*m to 2*m.
13+
*/
14+
15+
int i, j, r, c;
16+
printf("Enter no of rows and no of cols\n");
17+
cin>>r>>c; //take input of the rows and columns from user.
18+
int cost[c], dp[c];
19+
int x;
20+
/*
21+
Since for the first row the minimum cost is the only cost possible
22+
required to get there, we directly calculate it.
23+
*/
24+
printf("Enter a R X C array\n");
25+
26+
for(i=0; i<c; i++){
27+
cin>>cost[i];
28+
if(i>0) cost[i] = cost[i] + cost[i-1];
29+
}
30+
/*
31+
For the subsequent rows, the optimised cost for each cell in the
32+
previous row has been stored in the array cost[] and the optimised
33+
cost for the present row is stored in the array dp[].
34+
*/
35+
for(i=1; i<r; i++){
36+
for(j=0; j<c; j++){
37+
cin>>x;
38+
if(j==0)
39+
dp[j] = cost[j] + x;
40+
else{
41+
dp[j] = x + min(dp[j-1], min(cost[j-1], cost[j]));
42+
}
43+
}
44+
/*
45+
After dp[] has been found entirely, we copy its elements to
46+
cost[] and continue the iteration.
47+
*/
48+
for(j=0; (j<c && i!=c-1); j++){
49+
cost[j] = dp[j];
50+
}
51+
}
52+
printf("The min cost path from 1,1 to r,c is\n");
53+
cout<<dp[c-1]<<"\n";
54+
return 0;
55+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Given a set of non-negative integers, and a value sum, determine if there is a subset of the given set with sum equal to given sum.
2+
3+
Examples: set[] = {3, 34, 4, 12, 5, 2}, sum = 9
4+
Output: True //There is a subset (4, 5) with sum 9.
5+
6+
Let isSubSetSum(int set[], int n, int sum) be the function to find whether there is a subset of set[] with sum equal to sum. n is the number of elements in set[].
7+
8+
The isSubsetSum problem can be divided into two subproblems
9+
...a) Include the last element, recur for n = n-1, sum = sum – set[n-1]
10+
...b) Exclude the last element, recur for n = n-1.
11+
If any of the above the above subproblems return true, then return true.
12+
13+
Following is the recursive formula for isSubsetSum() problem.
14+
15+
isSubsetSum(set, n, sum) = isSubsetSum(set, n-1, sum) ||
16+
isSubsetSum(set, n-1, sum-set[n-1])
17+
Base Cases:
18+
isSubsetSum(set, n, sum) = false, if sum > 0 and n == 0
19+
isSubsetSum(set, n, sum) = true, if sum == 0
20+
21+
We can solve the problem in Pseudo-polynomial time using Dynamic programming. We create a boolean 2D table subset[][] and fill it in bottom up manner. The value of subset[i][j] will be true if there is a subset of set[0..j-1] with sum equal to i., otherwise false. Finally, we return subset[sum][n]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <stdio.h>
2+
3+
bool DP(int set[], int n, int sum)
4+
{
5+
bool subset[n+1][sum+1];
6+
for (int i = 0; i <= n; i++)
7+
subset[i][0] = true;
8+
for (int i = 1; i <= sum; i++)
9+
subset[0][i] = false;
10+
for (int i = 1; i <= n; i++)
11+
{
12+
for (int j = 1; j <= sum; j++)
13+
{
14+
if(j<set[i-1])
15+
subset[i][j] = subset[i-1][j];
16+
if (j >= set[i-1])
17+
subset[i][j] = subset[i-1][j] ||
18+
subset[i - 1][j-set[i-1]];
19+
}
20+
}
21+
return subset[n][sum];
22+
}
23+
24+
int main()
25+
{
26+
int n;
27+
int sum;
28+
printf("Enter no of elements of array and desired sum\n");
29+
scanf("%d%d",&n,&sum);
30+
int i,a[n];
31+
printf("Enter the array\n");
32+
for(i=0;i<n;i++)
33+
scanf("%d",&a[i]);
34+
if (DP(a, n+1, sum) == true)
35+
printf("Found a subset with given sum\n");
36+
else
37+
printf("No subset with given sum\n");
38+
return 0;
39+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... shows the first 11 ugly numbers. By convention, 1 is included.
2+
3+
Given a number n, the task is to find n’th Ugly number.
4+
5+
Input : n = 7
6+
Output : 8
7+
8+
Input : n = 10
9+
Output : 12
10+
11+
Input : n = 15
12+
Output : 24
13+
14+
Input : n = 150
15+
Output : 5832
16+
17+
DP method:
18+
Here is a time efficient solution with O(n) extra space. The ugly-number sequence is 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...
19+
because every number can only be divided by 2, 3, 5, one way to look at the sequence is to split the sequence to three groups as below:
20+
(1) ×ばつ2, ×ばつ2, ×ばつ2, ×ばつ2, ×ばつ2, ...
21+
(2) ×ばつ3, ×ばつ3, ×ばつ3, ×ばつ3, ×ばつ3, ...
22+
(3) ×ばつ5, ×ばつ5, ×ばつ5, ×ばつ5, ×ばつ5, ...
23+
24+
We can find that every subsequence is the ugly-sequence itself (1, 2, 3, 4, 5, ...) multiply 2, 3, 5. Then we use similar merge method as merge sort, to get every ugly number from the three subsequence. Every step we choose the smallest one, and move one step after.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int main()
4+
{
5+
printf("Enter no of test cases\n");
6+
int t; cin >> t;
7+
int i2, i3, i5;
8+
i2 = i3 = i5 = 0;
9+
int ugly[500];
10+
ugly[0] = 1;
11+
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++;
16+
}
17+
int n;
18+
while(t--) {
19+
printf("Enter 'n' for the nth ugly number\n");
20+
cin >> n;
21+
cout << ugly[n-1] << endl;
22+
}
23+
return 0;
24+
}

0 commit comments

Comments
(0)

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