feat: Add ncr mod p code (#1325)

* feat: Add ncr mod p code (#1323)

* Update math/ncr_modulo_p.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Added all functions inside a class + added more asserts

* updating DIRECTORY.md

* clang-format and clang-tidy fixes for f6df24a5

* Replace int64_t to uint64_t + add namespace + detailed documentation

* clang-format and clang-tidy fixes for e09a0579

* Add extra namespace + add const& in function arguments

* clang-format and clang-tidy fixes for 8111f881

* Update ncr_modulo_p.cpp

* clang-format and clang-tidy fixes for 2ad2f721

* Update math/ncr_modulo_p.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update math/ncr_modulo_p.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update math/ncr_modulo_p.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* clang-format and clang-tidy fixes for 5b69ba5c

* updating DIRECTORY.md

* clang-format and clang-tidy fixes for a8401d4b

Co-authored-by: David Leal <halfpacho@gmail.com>
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Kaustubh Damania
2020-11-22 23:05:01 +05:30
committed by GitHub
parent c8ce6f404c
commit 67e26cfbae
19 changed files with 1068 additions and 854 deletions

View File

@@ -75,10 +75,11 @@ int maxKnapsackValue(const int capacity, const std::array<int, n> &weight,
int profit2 = maxValue[i - 1][j];
maxValue[i][j] = std::max(profit1, profit2);
} else
} else {
// as weight of current item is greater than allowed weight, so
// maxProfit will be profit obtained by excluding current item.
maxValue[i][j] = maxValue[i - 1][j];
}
}
}

View File

@@ -1,7 +1,8 @@
/**
* @file
* @brief [Minimum coins](https://leetcode.com/problems/coin-change/) change problem is a problem used to find the minimum number of
* coins required to completely reach a target amount.
* @brief [Minimum coins](https://leetcode.com/problems/coin-change/) change
* problem is a problem used to find the minimum number of coins required to
* completely reach a target amount.
*
* @details
* This problem can be solved using 2 methods:
@@ -17,10 +18,10 @@
* @author [Divyansh Kushwaha](https://github.com/webdesignbydivyansh)
*/
#include <iostream> // for io operations
#include <vector> // for std::vector
#include <cassert> // for assert
#include <climits> // for INT_MAX
#include <cassert> // for assert
#include <climits> // for INT_MAX
#include <iostream> // for io operations
#include <vector> // for std::vector
/**
* @namespace dynamic_programming
@@ -29,36 +30,39 @@
namespace dynamic_programming {
/**
* @namespace mincoins_topdown
* @brief Functions for [minimum coin exchange](https://leetcode.com/problems/coin-change/) problem
* @brief Functions for [minimum coin
* exchange](https://leetcode.com/problems/coin-change/) problem
*/
namespace mincoins_topdown {
/**
* @brief This implementation is for finding minimum number of coins .
* @param T template-type to use any kind of value
* @param n amount to be reached
* @param coins vector of coins
* @param t deontes the number of coins
* @param dp initilised to 0
* @returns minimum number of coins
*/
template<typename T>
int64_t mincoins(const T &n, const std::vector<T> &coins, const int16_t &t, std::vector<T> dp){
if(n==0){
return 0;
}
if(dp[n]!=0){
return dp[n];
}
int ans=INT_MAX; //variable to store min coins
for(int i=0;i<t;i++){
if(n-coins[i]>=0){ //if after subtracting the current denomination is it greater than 0 or not
int sub=mincoins(n-coins[i],coins,t,dp);
ans=std::min(ans,sub+1);
}
}
dp[n]=ans;
return dp[n]; //returns minimum number of coins
/**
* @brief This implementation is for finding minimum number of coins .
* @param T template-type to use any kind of value
* @param n amount to be reached
* @param coins vector of coins
* @param t deontes the number of coins
* @param dp initilised to 0
* @returns minimum number of coins
*/
template <typename T>
int64_t mincoins(const T &n, const std::vector<T> &coins, const int16_t &t,
std::vector<T> dp) {
if (n == 0) {
return 0;
}
if (dp[n] != 0) {
return dp[n];
}
int ans = INT_MAX; // variable to store min coins
for (int i = 0; i < t; i++) {
if (n - coins[i] >= 0) { // if after subtracting the current
// denomination is it greater than 0 or not
int sub = mincoins(n - coins[i], coins, t, dp);
ans = std::min(ans, sub + 1);
}
}
dp[n] = ans;
return dp[n]; // returns minimum number of coins
}
} // namespace mincoins_topdown
} // namespace dynamic_programming
@@ -70,12 +74,13 @@ namespace mincoins_topdown {
static void test() {
// example 1: number of coins=3 and minimum coins required=3(7,7,1)
const int64_t n1 = 15;
const int8_t t1=3, a1=0;
const int8_t t1 = 3, a1 = 0;
std::cout << "\nTest 1...";
std::vector<int64_t> arr1 {1,7,10};
std::vector<int64_t> dp1 (n1+1);
fill(dp1.begin(),dp1.end(),a1);
assert(dynamic_programming::mincoins_topdown::mincoins(n1, arr1, t1, dp1)==3);
std::vector<int64_t> arr1{1, 7, 10};
std::vector<int64_t> dp1(n1 + 1);
fill(dp1.begin(), dp1.end(), a1);
assert(dynamic_programming::mincoins_topdown::mincoins(n1, arr1, t1, dp1) ==
3);
std::cout << "Passed\n";
}
@@ -84,6 +89,6 @@ static void test() {
* @returns 0 on exit
*/
int main() {
test(); // execute the test
test(); // execute the test
return 0;
}

View File

@@ -1,26 +1,26 @@
/**
* @file
* @brief Implements [Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning-ii/) algorithm, giving you the minimum number of partitions you need to make
* @brief Implements [Palindrome
* Partitioning](https://leetcode.com/problems/palindrome-partitioning-ii/)
* algorithm, giving you the minimum number of partitions you need to make
*
* @details
* palindrome partitioning uses dynamic programming and goes to all the possible partitions to find the minimum
* you are given a string and you need to give minimum number of partitions needed to divide it into a number of palindromes
* [Palindrome Partitioning] (https://www.geeksforgeeks.org/palindrome-partitioning-dp-17/)
* overall time complexity O(n^2)
* For example: example 1:-
* String : "nitik"
* Output : 2 => "n | iti | k"
* For example: example 2:-
* String : "ababbbabbababa"
* Output : 3 => "aba | b | bbabb | ababa"
* palindrome partitioning uses dynamic programming and goes to all the possible
* partitions to find the minimum you are given a string and you need to give
* minimum number of partitions needed to divide it into a number of palindromes
* [Palindrome Partitioning]
* (https://www.geeksforgeeks.org/palindrome-partitioning-dp-17/) overall time
* complexity O(n^2) For example: example 1:- String : "nitik" Output : 2 => "n
* | iti | k" For example: example 2:- String : "ababbbabbababa" Output : 3 =>
* "aba | b | bbabb | ababa"
* @author [Sujay Kaushik] (https://github.com/sujaykaushik008)
*/
#include <iostream> // for io operations
#include <vector> // for std::vector
#include <algorithm> // for std::min
#include <cassert> // for std::assert
#include <climits> // for INT_MAX
#include <iostream> // for io operations
#include <vector> // for std::vector
/**
* @namespace dynamic_programming
@@ -28,67 +28,73 @@
*/
namespace dynamic_programming {
/**
* @namespace palindrome_partitioning
* @brief Functions for [Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning-ii/) algorithm
*/
namespace palindrome_partitioning {
/**
* @namespace palindrome_partitioning
* @brief Functions for [Palindrome
* Partitioning](https://leetcode.com/problems/palindrome-partitioning-ii/)
* algorithm
*/
namespace palindrome_partitioning {
/**
* Function implementing palindrome partitioning algorithm using lookup table method.
* @param str input string
* @returns minimum number of partitions
*/
int pal_part(const std::string &str) {
/**
* Function implementing palindrome partitioning algorithm using lookup table
* method.
* @param str input string
* @returns minimum number of partitions
*/
int pal_part(const std::string &str) {
int n = str.size();
int n = str.size();
// creating lookup table for minimum number of cuts
std::vector<std::vector<int> > cuts(n, std::vector<int>(n, 0));
// creating lookup table for minimum number of cuts
std::vector<std::vector<int> > cuts(n, std::vector<int>(n, 0));
// creating lookup table for palindrome checking
std::vector<std::vector<bool> > is_palindrome(n,
std::vector<bool>(n, false));
// creating lookup table for palindrome checking
std::vector<std::vector<bool> > is_palindrome(n, std::vector<bool>(n, false));
// initialization
for (int i = 0; i < n; i++) {
is_palindrome[i][i] = true;
cuts[i][i] = 0;
}
// initialization
for (int i = 0; i < n; i++) {
is_palindrome[i][i] = true;
cuts[i][i] = 0;
for (int len = 2; len <= n; len++) {
for (int start_index = 0; start_index < n - len + 1; start_index++) {
int end_index = start_index + len - 1;
if (len == 2) {
is_palindrome[start_index][end_index] =
(str[start_index] == str[end_index]);
} else {
is_palindrome[start_index][end_index] =
(str[start_index] == str[end_index]) &&
is_palindrome[start_index + 1][end_index - 1];
}
for (int len = 2; len <= n; len++) {
for (int start_index = 0; start_index < n - len + 1; start_index++) {
int end_index = start_index + len - 1;
if (len == 2) {
is_palindrome[start_index][end_index] = (str[start_index] == str[end_index]);
} else {
is_palindrome[start_index][end_index] =
(str[start_index] == str[end_index]) && is_palindrome[start_index + 1][end_index - 1];
}
if (is_palindrome[start_index][end_index]) {
cuts[start_index][end_index] = 0;
} else {
cuts[start_index][end_index] = INT_MAX;
for (int partition = start_index; partition <= end_index - 1; partition++) {
cuts[start_index][end_index] = std::min(cuts[start_index][end_index],
cuts[start_index][partition] +
cuts[partition + 1][end_index] + 1);
}
}
if (is_palindrome[start_index][end_index]) {
cuts[start_index][end_index] = 0;
} else {
cuts[start_index][end_index] = INT_MAX;
for (int partition = start_index; partition <= end_index - 1;
partition++) {
cuts[start_index][end_index] =
std::min(cuts[start_index][end_index],
cuts[start_index][partition] +
cuts[partition + 1][end_index] + 1);
}
}
return cuts[0][n - 1];
}
} // namespace palindrome_partitioning
}
return cuts[0][n - 1];
}
} // namespace palindrome_partitioning
} // namespace dynamic_programming
/**
* @brief Test Function
* @return void
*/
*/
static void test() {
// custom input vector
std::vector<std::string> custom_input{"nitik", "ababbbabbababa", "abdc"};
@@ -97,7 +103,9 @@ static void test() {
std::vector<int> calculated_output(3);
for (int i = 0; i < 3; i++) {
calculated_output[i] = dynamic_programming::palindrome_partitioning::pal_part(custom_input[i]);
calculated_output[i] =
dynamic_programming::palindrome_partitioning::pal_part(
custom_input[i]);
}
// expected output vector