Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
dynamic_programming Namespace Reference

Dynamic Programming algorithms. More...

Functions

uint64_t LIS (const std::vector< uint64_t > &a, const uint32_t &n)
 Calculate the longest increasing subsequence for the specified numbers.
 
int maxCircularSum (std::vector< int > &arr)
 returns the maximum contiguous circular sum of an array
 

Detailed Description

Dynamic Programming algorithms.

Dynamic programming algorithms.

for std::vector

Dynamic Programming algorithm.

Dynamic Programming Algorithms.

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

for assert for std::max for io operations

Dynamic Programming algorithms

for assert for std::max for IO operations

Dynamic Programming algorithms

for assert for IO operations

Dynamic Programming algorithms

for std::assert for IO operations for unordered map

Dynamic Programming algorithms

Function Documentation

◆ 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
39 {
40 std::vector<int> lis(n);
41 for (int i = 0; i < n; ++i) {
42 lis[i] = 1;
43 }
44 for (int i = 0; i < n; ++i) {
45 for (int j = 0; j < i; ++j) {
46 if (a[i] > a[j] && lis[i] < lis[j] + 1) {
47 lis[i] = lis[j] + 1;
48 }
49 }
50 }
51 int res = 0;
52 for (int i = 0; i < n; ++i) {
53 res = std::max(res, lis[i]);
54 }
55 return res;
56}
T max(T... args)
Here is the call graph for this function:

◆ 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
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}
T min(T... args)
T size(T... args)
Here is the call graph for this function: