Skip to content

Commit

Permalink
Added more string and number algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
asesh committed Nov 17, 2023
1 parent 8f0cb29 commit da89299
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 19 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "350"
endingLineNumber = "350"
startingLineNumber = "367"
endingLineNumber = "367"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -30,8 +30,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "316"
endingLineNumber = "316"
startingLineNumber = "333"
endingLineNumber = "333"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -46,8 +46,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "241"
endingLineNumber = "241"
startingLineNumber = "258"
endingLineNumber = "258"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -62,8 +62,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "209"
endingLineNumber = "209"
startingLineNumber = "226"
endingLineNumber = "226"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -174,8 +174,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "167"
endingLineNumber = "167"
startingLineNumber = "184"
endingLineNumber = "184"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -190,8 +190,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "130"
endingLineNumber = "130"
startingLineNumber = "147"
endingLineNumber = "147"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -206,8 +206,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "89"
endingLineNumber = "89"
startingLineNumber = "106"
endingLineNumber = "106"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -238,8 +238,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "78"
endingLineNumber = "78"
startingLineNumber = "95"
endingLineNumber = "95"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -254,8 +254,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "64"
endingLineNumber = "64"
startingLineNumber = "81"
endingLineNumber = "81"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -413,5 +413,69 @@
</Locations>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "D8E30408-CC82-457D-9793-0797E9E15CE3"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "algorithm/string_algorithm.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "301"
endingLineNumber = "301"
landmarkName = "levenshtein_distance(first_word, second_word)"
landmarkType = "9">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "50072210-3356-4F74-B3CF-261297D5D146"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "algorithm/string_algorithm.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "284"
endingLineNumber = "284"
landmarkName = "levenshtein_distance(first_word, second_word)"
landmarkType = "9">
<Locations>
<Location
uuid = "50072210-3356-4F74-B3CF-261297D5D146 - 9294fc3ee4599373"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "levenshtein_distance(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt;&gt; const&amp;, std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt;&gt; const&amp;)"
moduleName = "algorithm"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/aseshshrestha/Code/Xcode%20Projects/algorithm/algorithm/string_algorithm.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "281"
endingLineNumber = "281"
offsetFromSymbolStart = "152">
</Location>
<Location
uuid = "50072210-3356-4F74-B3CF-261297D5D146 - 9294fc3ee459931e"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "levenshtein_distance(std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt;&gt; const&amp;, std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt;&gt; const&amp;)"
moduleName = "algorithm"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/aseshshrestha/Code/Xcode%20Projects/algorithm/algorithm/string_algorithm.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "284"
endingLineNumber = "284"
offsetFromSymbolStart = "152">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
19 changes: 18 additions & 1 deletion algorithm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,24 @@

int main(int argc, const char* argv[]) {

invoke_calculate_minimum_coins();
// std::vector<int> denoms = {1, 2, 5};
// test_recursion(denoms, 5);

// std::vector<std::vector<int>> visted(5, std::vector<int>(10, false));

invoke_river_size();

// invoke_levenshtein_distance();

// invoke_river_sizes();

// invoke_number_of_ways_to_traverse_graph();

// invoke_calculate_minimum_number_of_steps();

// invoke_calculate_number_of_steps();

// invoke_calculate_minimum_coins();

// std::cout<<(-1%10)<<std::endl;

Expand Down
158 changes: 158 additions & 0 deletions algorithm/number_algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,161 @@ void invoke_calculate_minimum_coins() {
int minimum_coins = calculate_minimum_coins(11, {1, 2, 5}, 0, computed_targets);
std::cout<<"Min. coins needed: "<<minimum_coins<<std::endl;
}

int calculate_four_number_sum(int target, const std::vector<int>& array, int sum) {
if(sum > target) {
return INT_MAX;
}

int calculated_sum = 0;
for(const auto& current_number: array) {

}

return INT_MAX;
}

void invoke_calculate_four_number_sum() {
std::vector<int> array = {7, 6, 4, -1, 1, 2};
calculate_four_number_sum(16, array, 0);
}

// Using top-down DP
void calculate_total_number_of_steps(int number_of_steps, int& total_number_of_steps, std::unordered_map<int, int>& computed_values) {
static std::vector<int> steps = {1, 2};

if(number_of_steps < 0) {
return;
}
if(computed_values.contains(number_of_steps)) {
// std::cout<<"Returning cached number of steps: "<<number_of_steps<<std::endl;
total_number_of_steps += computed_values.at(number_of_steps);
return;
}

if(number_of_steps == 0) {
// std::cout<<"Number of steps is 0 when : "<<number_of_steps<<std::endl;
total_number_of_steps++;
return;
}

for(const auto& step: steps) {
calculate_total_number_of_steps(number_of_steps - step, total_number_of_steps, computed_values);
}

// std::cout<<"Storing Key: "<<number_of_steps<<", value: "<<total_number_of_steps<<std::endl;
computed_values[number_of_steps] = total_number_of_steps;
}

void invoke_calculate_total_number_of_steps() {
int total_number_of_steps = 0;
std::unordered_map<int, int> computed_values;
calculate_total_number_of_steps(4, total_number_of_steps, computed_values);
std::cout<<std::endl<<"Number of steps that can be taken: "<<total_number_of_steps<<std::endl;
}

// https://leetcode.com/problems/climbing-stairs/description/
// Using bottom-up DP
int get_total_number_of_steps(int total_steps) {
int previous = 1, next = 1;
int sum = 0;
total_steps -= 2;
while(total_steps >= 0) {
--total_steps;

sum = previous + next;
previous = next;
next = sum;
}

return sum;
}

int calculate_minimum_number_of_steps(int destination_steps, int total_steps = 0) {
static std::vector<int> steps = {1, 2};
if(destination_steps < 0) {
return INT_MAX;
}

if(destination_steps == 0) {
return total_steps;
}

++total_steps;

int min_steps = INT_MAX;
for(const auto& step: steps) {
int total_possible_steps = calculate_minimum_number_of_steps(destination_steps - step, total_steps);
min_steps = std::min(min_steps, total_possible_steps);
}

return min_steps;
}

void invoke_calculate_minimum_number_of_steps() {
std::cout<<"Minimum number of steps: "<<calculate_minimum_number_of_steps(25)<<std::endl;
}

// https://www.algoexpert.io/questions/number-of-ways-to-traverse-graph
int number_of_ways_to_traverse_graph(int total_width, int total_height) {
std::vector<int> computed_cells(total_width + 1, 1); // = {1, 1, 1, 1, 0};
computed_cells[total_width] = 0;

for(int current_row = total_height - 2; current_row >= 0; --current_row) {
std::vector<int> new_row(total_width + 1, 0);
for(int current_column = total_width - 1; current_column >= 0; --current_column) {
new_row[current_column] = new_row[current_column + 1] + computed_cells[current_column];
}

computed_cells = new_row;
}

return computed_cells[0];
}

void invoke_number_of_ways_to_traverse_graph() {
std::cout<<"Number of ways to traverse graph: "<<number_of_ways_to_traverse_graph(4, 3);
}

void river_size_dfs(const std::vector<std::vector<int>>& matrix, std::vector<std::vector<int>>& visited, int& river_size, int row, int column) {
auto column_size = matrix[0].size();
auto row_size = matrix.size();
if(row >= 0 && row < row_size && column >= 0 && column < column_size && matrix[row][column] == 1 &&
!visited[row][column]) {
++river_size;
visited[row][column] = true;
river_size_dfs(matrix, visited, river_size, row, column - 1); // Move left
river_size_dfs(matrix, visited, river_size, row - 1, column); // Move to top
river_size_dfs(matrix, visited, river_size, row, column + 1); // Move to right
river_size_dfs(matrix, visited, river_size, row + 1, column); // Move to bottom
}
}

// Time: O(wh), Space: O(wh)
void invoke_river_size() {
std::vector<int> output;

std::vector<std::vector<int>> matrix = {
{1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0}
};

// Space: O(wh)
std::vector<std::vector<int>> visited(matrix.size(), std::vector<int>(matrix[0].size(), false));

// Time: O(wh)
for(int row = 0; row < matrix.size(); ++row) {
for(int column = 0; column < matrix[0].size(); ++column) {

if(matrix[row][column] == 1 && !visited[row][column]) {
int river_size = 0;
river_size_dfs(matrix, visited, river_size, row, column);
output.push_back(river_size);
}
}
}

std::cout<<"The river sizes are: ";
for(const auto& size: output) {
std::cout<<size<<", ";
}
}
12 changes: 12 additions & 0 deletions algorithm/number_algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ void rat_in_a_maze();
void calculate_minimum_coins(int target, const std::vector<int>& denoms, int coins_needed, int& min_coins, std::unordered_map<int, int>& computed_targets);
void invoke_calculate_minimum_coins();

int calculate_four_number_sum(int target, const std::vector<int>& array, int sum);
void invoke_calculate_four_number_sum();

void calculate_total_number_of_steps(int number_of_steps, int& total_number_of_steps, std::unordered_map<int, int>& computed_values);
void invoke_calculate_total_number_of_steps();

void invoke_calculate_minimum_number_of_steps();

void invoke_number_of_ways_to_traverse_graph();

void invoke_river_size();

#endif /* number_algorithm_hpp */
Loading

0 comments on commit da89299

Please sign in to comment.