TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
dynamic_programming Namespace Reference

Dynamic Programming algorithms. More...

Functions

template<typename T >
bool is_armstrong (const T &number)
 Checks if the given number is armstrong or not.
 
uint64_t LIS (const std::vector< uint64_t > &a, const uint32_t &n)
 Calculate the longest increasing subsequence for the specified numbers.
 
std::string lps (const std::string &a)
 Function that returns the longest palindromic subsequence of a string.
 
int maxCircularSum (std::vector< int > &arr)
 returns the maximum contiguous circular sum of an array
 
uint32_t trappedRainwater (const std::vector< uint32_t > &heights)
 Function to calculate the trapped rainwater.
 

Detailed Description

Dynamic Programming algorithms.

Dynamic programming algorithms.

Namespace for dynamic programming algorithms.

For std::vector.

for std::vector

Dynamic Programming algorithm.

for IO operations

Dynamic Programming Algorithms.

for assert for std::uint32_t for IO operations for std::string library for std::vector STL library

for assert for std::pow

Dynamic Programming algorithms

for assert for std::max for std::uint32_t for io operations

Dynamic Programming algorithms

for assert for std::max for std::uint64_t for IO operations

Dynamic Programming algorithms

for assert for std::string for std::vector

for assert for IO operations

Dynamic Programming algorithms

for assert for std::uint64_t for IO operations

Dynamic Programming algorithms

for std::assert for IO operations for unordered map

Dynamic Programming algorithms

For std::min and std::max For assert For std::size_t

Function Documentation

◆ is_armstrong()

template<typename T >
bool dynamic_programming::is_armstrong ( const T & number)

Checks if the given number is armstrong or not.

Parameters
numberthe number to check
Returns
false if the given number is NOT armstrong
true if the given number IS armstrong

Definition at line 39 of file armstrong_number_templated.cpp.

39 {
40 int count = 0, temp = number, result = 0, rem = 0;
41
42 // Count the number of digits of the given number.
43 // For example: 153 would be 3 digits.
44 while (temp != 0) {
45 temp /= 10;
46 count++;
47 }
48
49 // Calculation for checking of armstrongs number i.e.
50 // in an n-digit number sum of the digits is raised to a power of `n` is
51 // equal to the original number.
52 temp = number;
53 while (temp != 0) {
54 rem = temp % 10;
55 result += static_cast<T>(std::pow(rem, count));
56 temp /= 10;
57 }
58
59 if (result == number) {
60 return true;
61 } else {
62 return false;
63 }
64}

◆ LIS()

uint64_t dynamic_programming::LIS ( const std::vector< uint64_t > & a,
const uint32_t & n )

Calculate the longest increasing subsequence for the specified numbers.

Parameters
athe array used to calculate the longest increasing subsequence
nthe size used for the arrays
Returns
the length of the longest increasing subsequence in the a array of size n

Definition at line 40 of file longest_increasing_subsequence.cpp.

40 {
41 std::vector<int> lis(n);
42 for (int i = 0; i < n; ++i) {
43 lis[i] = 1;
44 }
45 for (int i = 0; i < n; ++i) {
46 for (int j = 0; j < i; ++j) {
47 if (a[i] > a[j] && lis[i] < lis[j] + 1) {
48 lis[i] = lis[j] + 1;
49 }
50 }
51 }
52 int res = 0;
53 for (int i = 0; i < n; ++i) {
54 res = std::max(res, lis[i]);
55 }
56 return res;
57}

◆ lps()

std::string dynamic_programming::lps ( const std::string & a)

Function that returns the longest palindromic subsequence of a string.

Parameters
astring whose longest palindromic subsequence is to be found
Returns
longest palindromic subsequence of the string

Definition at line 31 of file longest_palindromic_subsequence.cpp.

31 {
32 const auto b = std::string(a.rbegin(), a.rend());
33 const auto m = a.length();
34 using ind_type = std::string::size_type;
35 std::vector<std::vector<ind_type> > res(m + 1,
36 std::vector<ind_type>(m + 1));
37
38 // Finding the length of the longest
39 // palindromic subsequence and storing
40 // in a 2D array in bottoms-up manner
41 for (ind_type i = 0; i <= m; i++) {
42 for (ind_type j = 0; j <= m; j++) {
43 if (i == 0 || j == 0) {
44 res[i][j] = 0;
45 } else if (a[i - 1] == b[j - 1]) {
46 res[i][j] = res[i - 1][j - 1] + 1;
47 } else {
48 res[i][j] = std::max(res[i - 1][j], res[i][j - 1]);
49 }
50 }
51 }
52 // Length of longest palindromic subsequence
53 auto idx = res[m][m];
54 // Creating string of index+1 length
55 std::string ans(idx, '\0');
56 ind_type i = m, j = m;
57
58 // starting from right-most bottom-most corner
59 // and storing them one by one in ans
60 while (i > 0 && j > 0) {
61 // if current characters in a and b are same
62 // then it is a part of the ans
63 if (a[i - 1] == b[j - 1]) {
64 ans[idx - 1] = a[i - 1];
65 i--;
66 j--;
67 idx--;
68 }
69 // If they are not same, find the larger of the
70 // two and move in that direction
71 else if (res[i - 1][j] > res[i][j - 1]) {
72 i--;
73 } else {
74 j--;
75 }
76 }
77
78 return ans;
79}

◆ maxCircularSum()

int dynamic_programming::maxCircularSum ( std::vector< int > & arr)

returns the maximum contiguous circular sum of an array

Parameters
arris the array/vector
Returns
int which is the maximum sum

Definition at line 26 of file maximum_circular_subarray.cpp.

27{
28 // Edge Case
29 if (arr.size() == 1)
30 return arr[0];
31
32 // Sum variable which stores total sum of the array.
33 int sum = 0;
34 for (int i = 0; i < arr.size(); i++) {
35 sum += arr[i];
36 }
37
38 // Every variable stores first value of the array.
39 int current_max = arr[0], max_so_far = arr[0], current_min = arr[0], min_so_far = arr[0];
40
41 // Concept of Kadane's Algorithm
42 for (int i = 1; i < arr.size(); i++) {
43 // Kadane's Algorithm to find Maximum subarray sum.
44 current_max = std::max(current_max + arr[i], arr[i]);
45 max_so_far = std::max(max_so_far, current_max);
46
47 // Kadane's Algorithm to find Minimum subarray sum.
48 current_min = std::min(current_min + arr[i], arr[i]);
49 min_so_far = std::min(min_so_far, current_min);
50 }
51
52 if (min_so_far == sum)
53 return max_so_far;
54
55 // Return the maximum value
56 return std::max(max_so_far, sum - min_so_far);
57}

◆ trappedRainwater()

uint32_t dynamic_programming::trappedRainwater ( const std::vector< uint32_t > & heights)

Function to calculate the trapped rainwater.

Parameters
heightsArray representing the heights of walls
Returns
The amount of trapped rainwater

Definition at line 27 of file trapped_rainwater.cpp.

27 {
28 std::size_t n = heights.size();
29 if (n <= 2)
30 return 0; // No water can be trapped with less than 3 walls
31
32 std::vector<uint32_t> leftMax(n), rightMax(n);
33
34 // Calculate the maximum height of wall to the left of each wall
35 leftMax[0] = heights[0];
36 for (std::size_t i = 1; i < n; ++i) {
37 leftMax[i] = std::max(leftMax[i - 1], heights[i]);
38 }
39
40 // Calculate the maximum height of wall to the right of each wall
41 rightMax[n - 1] = heights[n - 1];
42 for (std::size_t i = n - 2; i < n; --i) {
43 rightMax[i] = std::max(rightMax[i + 1], heights[i]);
44 }
45
46 // Calculate the trapped rainwater between walls
47 uint32_t trappedWater = 0;
48 for (std::size_t i = 0; i < n; ++i) {
49 trappedWater +=
50 std::max(0u, std::min(leftMax[i], rightMax[i]) - heights[i]);
51 }
52
53 return trappedWater;
54}