mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-04 02:59:43 +08:00
feat: add Strassen's Matrix Multiplication (#2413)
* Feat: Add Strassen's matrix multiplication * updating DIRECTORY.md * Fix cpp lint error * updating DIRECTORY.md * clang-format and clang-tidy fixes for02439b57* Fix windows error * Add namespaces * updating DIRECTORY.md * Proper documentation * Reduce the matrix size. * updating DIRECTORY.md * clang-format and clang-tidy fixes for0545555aCo-authored-by: toastedbreadandomelette <toastedbreadandomelette@gmail.com> Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com> Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
committed by
GitHub
parent
a6a9d8e75a
commit
5b238724b8
@@ -5,19 +5,19 @@
|
||||
* whether a subset with target sum exists or not.
|
||||
*
|
||||
* @details
|
||||
* In this problem, we use dynamic programming to find if we can pull out a
|
||||
* subset from an array whose sum is equal to a given target sum. The overall
|
||||
* time complexity of the problem is O(n * targetSum) where n is the size of
|
||||
* the array. For example, array = [1, -10, 2, 31, -6], targetSum = -14.
|
||||
* Output: true => We can pick subset [-10, 2, -6] with sum as
|
||||
* In this problem, we use dynamic programming to find if we can pull out a
|
||||
* subset from an array whose sum is equal to a given target sum. The overall
|
||||
* time complexity of the problem is O(n * targetSum) where n is the size of
|
||||
* the array. For example, array = [1, -10, 2, 31, -6], targetSum = -14.
|
||||
* Output: true => We can pick subset [-10, 2, -6] with sum as
|
||||
* (-10) + 2 + (-6) = -14.
|
||||
* @author [KillerAV](https://github.com/KillerAV)
|
||||
*/
|
||||
|
||||
#include <cassert> /// for std::assert
|
||||
#include <iostream> /// for IO operations
|
||||
#include <vector> /// for std::vector
|
||||
#include <unordered_map> /// for unordered map
|
||||
#include <unordered_map> /// for unordered map
|
||||
#include <vector> /// for std::vector
|
||||
|
||||
/**
|
||||
* @namespace dynamic_programming
|
||||
@@ -33,33 +33,31 @@ namespace dynamic_programming {
|
||||
namespace subset_sum {
|
||||
|
||||
/**
|
||||
* Recursive function using dynamic programming to find if the required sum
|
||||
* Recursive function using dynamic programming to find if the required sum
|
||||
* subset exists or not.
|
||||
* @param arr input array
|
||||
* @param targetSum the target sum of the subset
|
||||
* @param dp the map storing the results
|
||||
* @returns true/false based on if the target sum subset exists or not.
|
||||
*/
|
||||
bool subset_sum_recursion(
|
||||
const std::vector<int> &arr,
|
||||
int targetSum,
|
||||
std::vector<std::unordered_map<int, bool>> *dp,
|
||||
int index = 0) {
|
||||
|
||||
if(targetSum == 0) { // Found a valid subset with required sum.
|
||||
bool subset_sum_recursion(const std::vector<int> &arr, int targetSum,
|
||||
std::vector<std::unordered_map<int, bool>> *dp,
|
||||
int index = 0) {
|
||||
if (targetSum == 0) { // Found a valid subset with required sum.
|
||||
return true;
|
||||
}
|
||||
if(index == arr.size()) { // End of array
|
||||
if (index == arr.size()) { // End of array
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((*dp)[index].count(targetSum)) { // Answer already present in map
|
||||
if ((*dp)[index].count(targetSum)) { // Answer already present in map
|
||||
return (*dp)[index][targetSum];
|
||||
}
|
||||
|
||||
bool ans = subset_sum_recursion(arr, targetSum - arr[index], dp, index + 1)
|
||||
|| subset_sum_recursion(arr, targetSum, dp, index + 1);
|
||||
(*dp)[index][targetSum] = ans; // Save ans in dp map.
|
||||
|
||||
bool ans =
|
||||
subset_sum_recursion(arr, targetSum - arr[index], dp, index + 1) ||
|
||||
subset_sum_recursion(arr, targetSum, dp, index + 1);
|
||||
(*dp)[index][targetSum] = ans; // Save ans in dp map.
|
||||
return ans;
|
||||
}
|
||||
|
||||
@@ -84,10 +82,10 @@ bool subset_sum_problem(const std::vector<int> &arr, const int targetSum) {
|
||||
static void test() {
|
||||
// custom input vector
|
||||
std::vector<std::vector<int>> custom_input_arr(3);
|
||||
custom_input_arr[0] = std::vector<int> {1, -10, 2, 31, -6};
|
||||
custom_input_arr[1] = std::vector<int> {2, 3, 4};
|
||||
custom_input_arr[2] = std::vector<int> {0, 1, 0, 1, 0};
|
||||
|
||||
custom_input_arr[0] = std::vector<int>{1, -10, 2, 31, -6};
|
||||
custom_input_arr[1] = std::vector<int>{2, 3, 4};
|
||||
custom_input_arr[2] = std::vector<int>{0, 1, 0, 1, 0};
|
||||
|
||||
std::vector<int> custom_input_target_sum(3);
|
||||
custom_input_target_sum[0] = -14;
|
||||
custom_input_target_sum[1] = 10;
|
||||
|
||||
Reference in New Issue
Block a user