Rod-Cutting Problem: Maximization Objective, Divisible Input, Overlapping Subproblems, Dynamic Pricing or Resource Management
Rod-Cutting Problem: Maximization Objective, Divisible Input, Overlapping Subproblems, Dynamic Pricing or Resource Management
rod-cutting problem:
Maximization Objective, Divisible Input, Overlapping Subproblems,Dynamic
Pricing or Resource Management
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Example usage
int main() {
vector<int> prices = {1, 5, 8, 9, 10, 17, 17, 20}; // Prices for rods of length 1 to 8
int n = 8; // Total length of the rod
cout << "Maximum profit: " << rodCutting(prices, n) << endl;
return 0;
}
2. LCS
Use LCS to find the longest subsequence common to two strings, preserving
order but not requiring contiguity.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
// If the last characters of both strings match, reduce both lengths by 1 and add 1 to
the result
if (X[m - 1] == Y[n - 1])
return 1 + lcsRecursive(X, Y, m - 1, n - 1);
// Example usage
int main() {
string X = "ABCBDAB";
string Y = "BDCAB";
int m = X.size();
int n = Y.size();
// Example usage
int main() {
int m = 3, n = 3; // Grid dimensions
cout << "Number of unique paths: " << countPaths(m, n) << endl;
return 0;
}
// If there are only two characters and both are the same, it's a palindrome of length 2
if (s[i] == s[j] && i + 1 == j)
return 2;
// Example usage
int main() {
string s = "BBABCBCAB";
cout << "Length of Longest Palindromic Subsequence: " << lpsRecursive(s, 0, s.size()
- 1) << endl;
return 0;
}
5. Subset Sum
"Use this function to check if any subset of a set can sum up to a given target
value."
#include <iostream>
#include <vector>
using namespace std;
// Recursive function to determine if a subset with the given target sum exists
bool subsetSumRecursive(const vector<int>& arr, int n, int target) {
// Base Case: If target is 0, we can always achieve it with an empty subset
if (target == 0)
return true;
// Base Case: If no elements are left and target is not 0, it's not achievable
if (n == 0)
return false;
// Example usage
int main() {
vector<int> arr = {3, 34, 4, 12, 5, 2};
int target = 9;
return 0;
}
// Case 2: Include the current element if it's larger than the previous element in the
sequence
int include = 0;
if (prev_index == -1 || arr[current_index] > arr[prev_index])
include = 1 + lisRecursive(arr, current_index, current_index + 1, memo);
memo[prev_index + 1][current_index] = max(exclude, include);
return memo[prev_index + 1][current_index];
}
// Example usage
int main() {
vector<int> arr = {10, 22, 9, 33, 21, 50, 41, 60};
cout << "Length of LIS: " << lis(arr) << endl;
return 0;
}
7. 0-1 Knapsack
One-Liner for Identification of Usage: "Use this function to maximize the total
value of items that can be included in a knapsack of limited capacity without
breaking items."
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// If the weight of the nth item is more than the capacity W, it cannot be included
if (weights[n - 1] > W)
memo[n][W] = knapsackMemoized(values, weights, W, n - 1, memo);
else
// Otherwise, check the maximum value of two cases:
// 1. Including the nth item
// 2. Excluding the nth item
memo[n][W] = max(
values[n - 1] + knapsackMemoized(values, weights, W - weights[n - 1], n - 1,
memo),
knapsackMemoized(values, weights, W, n - 1, memo)
);
return memo[n][W];
}
// Example usage
int main() {
vector<int> values = {60, 100, 120};
vector<int> weights = {10, 20, 30};
int W = 50; // Knapsack capacity
int n = values.size();
vector<vector<int>> memo(n + 1, vector<int>(W + 1, -1)); // Initialize memo table
8. Fractional Knapsack
One-Liner for Identification of Usage: "Use this function to maximize the total
value of items that can be included in a knapsack, allowing fractional items.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
return totalValue;
}
// Example usage
int main() {
vector<int> values = {60, 100, 120};
vector<int> weights = {10, 20, 30};
int capacity = 50;
One-Liner for Identification of Usage: "Use this function to sort a vector of pairs by the
second element, breaking ties with the first element."
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Example usage
int main() {
vector<pair<int, int>> arr = {{3, 2}, {1, 3}, {1, 2}, {3, 3}, {2, 2}};
sort(arr.begin(), arr.end(), sortBySecondThenFirst);
return 0;
}
// Example usage
int main() {
map<string, int> myDict = {{"apple", 10}, {"banana", 5}, {"cherry", 15}};
vector<pair<string, int>> sortedDict(myDict.begin(), myDict.end());
// Sort by value
sort(sortedDict.begin(), sortedDict.end(), sortByValue);
return 0;
}
if (arr[mid] == x)
return mid;
else if (arr[mid] < x)
left = mid + 1;
else
right = mid - 1;
}
// Example usage
int main() {
vector<int> arr = {1, 3, 5, 7, 9};
int x = 5;
return 0;
}
12. Vectors:
#include <vector>
std::vector<int> vec; // Declares a vector of integers
std::vector<int> vec(5); // Declares a vector of size 5 with default values (0 for int)
std::vector<int> vec(5, 10); // Declares a vector of size 5 initialized with 10
std::vector<int> vec = {1, 2, 3}; // Declares and initializes a vector
12.
#include <stack>
std::stack<int> s;
#include <queue>
std::queue<int> q;
#include <list>
std::list<int> lst;
DATA STRUCTURES: