Skip to content

Commit 4456c8a

Browse files
Merge branch 'master' into add/number_of_paths
2 parents 1391c19 + 6393130 commit 4456c8a

File tree

11 files changed

+656
-532
lines changed

11 files changed

+656
-532
lines changed

CMakeLists.txt

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,30 @@ if(USE_OPENMP)
2727
endif()
2828
endif()
2929

30-
add_subdirectory(math)
31-
add_subdirectory(others)
32-
add_subdirectory(search)
33-
add_subdirectory(ciphers)
34-
add_subdirectory(hashing)
35-
add_subdirectory(strings)
36-
add_subdirectory(sorting)
37-
add_subdirectory(geometry)
38-
add_subdirectory(graphics)
39-
add_subdirectory(probability)
4030
add_subdirectory(backtracking)
4131
add_subdirectory(bit_manipulation)
32+
add_subdirectory(ciphers)
33+
add_subdirectory(cpu_scheduling_algorithms)
34+
add_subdirectory(data_structures)
35+
add_subdirectory(divide_and_conquer)
4236
add_subdirectory(dynamic_programming)
37+
add_subdirectory(games)
38+
add_subdirectory(geometry)
39+
add_subdirectory(graph)
40+
add_subdirectory(graphics)
4341
add_subdirectory(greedy_algorithms)
44-
add_subdirectory(range_queries)
45-
add_subdirectory(operations_on_datastructures)
46-
add_subdirectory(data_structures)
42+
add_subdirectory(hashing)
4743
add_subdirectory(machine_learning)
44+
add_subdirectory(math)
4845
add_subdirectory(numerical_methods)
49-
add_subdirectory(graph)
50-
add_subdirectory(divide_and_conquer)
51-
add_subdirectory(games)
52-
add_subdirectory(cpu_scheduling_algorithms)
46+
add_subdirectory(operations_on_datastructures)
47+
add_subdirectory(others)
5348
add_subdirectory(physics)
49+
add_subdirectory(probability)
50+
add_subdirectory(range_queries)
51+
add_subdirectory(search)
52+
add_subdirectory(sorting)
53+
add_subdirectory(strings)
5454

5555
cmake_policy(SET CMP0054 NEW)
5656
cmake_policy(SET CMP0057 NEW)

ciphers/uint256_t.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class uint256_t {
7272
*/
7373
template <typename T, typename = typename std::enable_if<
7474
std::is_integral<T>::value, T>::type>
75-
explicit uint256_t(T low) : s(low), f(0) {}
75+
explicit uint256_t(T low) : f(0), s(low) {}
7676

7777
/**
7878
* @brief Parameterized constructor

data_structures/queue_using_array2.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
#include <iostream>
2-
using namespace std;
32

43
int queue[10];
54
int front = 0;
65
int rear = 0;
76

87
void Enque(int x) {
98
if (rear == 10) {
10-
cout << "\nOverflow";
9+
std::cout << "\nOverflow";
1110
} else {
1211
queue[rear++] = x;
1312
}
1413
}
1514

1615
void Deque() {
1716
if (front == rear) {
18-
cout << "\nUnderflow";
17+
std::cout << "\nUnderflow";
1918
}
2019

2120
else {
22-
cout << "\n" << queue[front++] << " deleted";
21+
std::cout << "\n" << queue[front++] << " deleted";
2322
for (int i = front; i < rear; i++) {
2423
queue[i - front] = queue[i];
2524
}
@@ -30,21 +29,21 @@ void Deque() {
3029

3130
void show() {
3231
for (int i = front; i < rear; i++) {
33-
cout << queue[i] << "\t";
32+
std::cout << queue[i] << "\t";
3433
}
3534
}
3635

3736
int main() {
3837
int ch, x;
3938
do {
40-
cout << "\n1. Enque";
41-
cout << "\n2. Deque";
42-
cout << "\n3. Print";
43-
cout << "\nEnter Your Choice : ";
44-
cin >> ch;
39+
std::cout << "\n1. Enque";
40+
std::cout << "\n2. Deque";
41+
std::cout << "\n3. Print";
42+
std::cout << "\nEnter Your Choice : ";
43+
std::cin >> ch;
4544
if (ch == 1) {
46-
cout << "\nInsert : ";
47-
cin >> x;
45+
std::cout << "\nInsert : ";
46+
std::cin >> x;
4847
Enque(x);
4948
} else if (ch == 2) {
5049
Deque();

data_structures/stack_using_array.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Stack {
2525
*
2626
* @param size Maximum size of the stack
2727
*/
28-
Stack(int size) : stackSize(size), stackIndex(-1), stack(new T[size]) {}
28+
Stack(int size) : stack(new T[size]), stackSize(size), stackIndex(-1) {}
2929

3030
/**
3131
* @brief Checks if the stack is full

data_structures/tree_234.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ class Node {
3939
* @param item the first value we insert to the node
4040
*/
4141
explicit Node(int64_t item)
42-
: count(1),
43-
items({{item, 0, 0}}),
44-
children({{nullptr, nullptr, nullptr, nullptr}}) {}
42+
: items({{item, 0, 0}}),
43+
children({{nullptr, nullptr, nullptr, nullptr}}),
44+
count(1) {}
4545

4646
/**
4747
* @brief Get the item count that current saved in the node

data_structures/trie_tree.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
#include <array>
1111
#include <cassert>
12+
#include <cstdint>
1213
#include <iostream>
1314
#include <memory>
1415
#include <string>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* @file
3+
* @brief Implementation of the [Trapped Rainwater
4+
* Problem](https://www.geeksforgeeks.org/trapping-rain-water/)
5+
* @details
6+
* This implementation calculates the total trapped rainwater using a
7+
* two-pointer approach. It maintains two pointers (`left` and `right`) and
8+
* tracks the maximum height seen so far from both ends (`leftMax` and
9+
* `rightMax`). At each step, the algorithm decides which side to process based
10+
* on which boundary is smaller, ensuring O(n) time and O(1) space complexity.
11+
* @author [kanavgoyal898](https://github.com/kanavgoyal898)
12+
*/
13+
14+
#include <algorithm> /// For std::min and std::max
15+
#include <cassert> /// For assert
16+
#include <cstddef> /// For std::size_t
17+
#include <cstdint> /// For std::uint32_t
18+
#include <vector> /// For std::vector
19+
20+
/*
21+
* @namespace
22+
* @brief Dynamic Programming Algorithms
23+
*/
24+
namespace dynamic_programming {
25+
/**
26+
* @brief Function to calculate the trapped rainwater
27+
* @param heights Array representing the heights of walls
28+
* @return The amount of trapped rainwater
29+
*/
30+
uint32_t trappedRainwater(const std::vector<uint32_t>& heights) {
31+
std::size_t n = heights.size();
32+
if (n <= 2)
33+
return 0; // Not enough walls to trap water
34+
35+
std::size_t left = 0, right = n - 1;
36+
uint32_t leftMax = 0, rightMax = 0, trappedWater = 0;
37+
38+
// Traverse from both ends towards the center
39+
while (left < right) {
40+
if (heights[left] < heights[right]) {
41+
// Water trapped depends on the tallest wall to the left
42+
if (heights[left] >= leftMax)
43+
leftMax = heights[left]; // Update left max
44+
else
45+
trappedWater +=
46+
leftMax - heights[left]; // Water trapped at current left
47+
++left;
48+
} else {
49+
// Water trapped depends on the tallest wall to the right
50+
if (heights[right] >= rightMax)
51+
rightMax = heights[right]; // Update right max
52+
else
53+
trappedWater +=
54+
rightMax -
55+
heights[right]; // Water trapped at current right
56+
--right;
57+
}
58+
}
59+
60+
return trappedWater;
61+
}
62+
63+
} // namespace dynamic_programming
64+
65+
/**
66+
* @brief Self-test implementations
67+
* @returns void
68+
*/
69+
static void test() {
70+
std::vector<uint32_t> test_basic = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
71+
assert(dynamic_programming::trappedRainwater(test_basic) == 6);
72+
73+
std::vector<uint32_t> test_peak_under_water = {3, 0, 2, 0, 4};
74+
assert(dynamic_programming::trappedRainwater(test_peak_under_water) == 7);
75+
76+
std::vector<uint32_t> test_bucket = {5, 1, 5};
77+
assert(dynamic_programming::trappedRainwater(test_bucket) == 4);
78+
79+
std::vector<uint32_t> test_skewed_bucket = {4, 1, 5};
80+
assert(dynamic_programming::trappedRainwater(test_skewed_bucket) == 3);
81+
82+
std::vector<uint32_t> test_empty = {};
83+
assert(dynamic_programming::trappedRainwater(test_empty) == 0);
84+
85+
std::vector<uint32_t> test_flat = {0, 0, 0, 0, 0};
86+
assert(dynamic_programming::trappedRainwater(test_flat) == 0);
87+
88+
std::vector<uint32_t> test_no_trapped_water = {1, 1, 2, 4, 0, 0, 0};
89+
assert(dynamic_programming::trappedRainwater(test_no_trapped_water) == 0);
90+
91+
std::vector<uint32_t> test_single_elevation = {5};
92+
assert(dynamic_programming::trappedRainwater(test_single_elevation) == 0);
93+
94+
std::vector<uint32_t> test_two_point_elevation = {5, 1};
95+
assert(dynamic_programming::trappedRainwater(test_two_point_elevation) ==
96+
0);
97+
98+
std::vector<uint32_t> test_large_elevation_map_difference = {5, 1, 6, 1,
99+
7, 1, 8};
100+
assert(dynamic_programming::trappedRainwater(
101+
test_large_elevation_map_difference) == 15);
102+
}
103+
104+
/**
105+
* @brief Main function
106+
* @returns 0 on exit
107+
*/
108+
int main() {
109+
test(); // run self-test implementations
110+
return 0;
111+
}

machine_learning/a_star_search.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
#include <algorithm> /// for `std::reverse` function
2323
#include <array> /// for `std::array`, representing `EightPuzzle` board
2424
#include <cassert> /// for `assert`
25+
#include <cstdint> /// for `std::uint32_t`
2526
#include <functional> /// for `std::function` STL
2627
#include <iostream> /// for IO operations
2728
#include <map> /// for `std::map` STL
2829
#include <memory> /// for `std::shared_ptr`
2930
#include <set> /// for `std::set` STL
3031
#include <vector> /// for `std::vector` STL
32+
3133
/**
3234
* @namespace machine_learning
3335
* @brief Machine learning algorithms

0 commit comments

Comments
 (0)