Skip to content

Commit

Permalink
Added solutions for:
Browse files Browse the repository at this point in the history
  • Loading branch information
asesh committed Oct 31, 2024
1 parent 786ed9f commit 8f33a59
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 21 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 = "429"
endingLineNumber = "429"
startingLineNumber = "433"
endingLineNumber = "433"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -30,8 +30,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "395"
endingLineNumber = "395"
startingLineNumber = "399"
endingLineNumber = "399"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -46,8 +46,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "320"
endingLineNumber = "320"
startingLineNumber = "324"
endingLineNumber = "324"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -62,8 +62,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "288"
endingLineNumber = "288"
startingLineNumber = "292"
endingLineNumber = "292"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -110,8 +110,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "250"
endingLineNumber = "250"
startingLineNumber = "254"
endingLineNumber = "254"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -126,8 +126,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "213"
endingLineNumber = "213"
startingLineNumber = "217"
endingLineNumber = "217"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -142,8 +142,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "172"
endingLineNumber = "172"
startingLineNumber = "176"
endingLineNumber = "176"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -174,8 +174,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "161"
endingLineNumber = "161"
startingLineNumber = "165"
endingLineNumber = "165"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -190,8 +190,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "147"
endingLineNumber = "147"
startingLineNumber = "151"
endingLineNumber = "151"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -254,8 +254,8 @@
filePath = "algorithm/number.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "1195"
endingLineNumber = "1195"
startingLineNumber = "1200"
endingLineNumber = "1200"
landmarkName = "invoke_product_of_array_except_itself()"
landmarkType = "9">
</BreakpointContent>
Expand Down
59 changes: 59 additions & 0 deletions algorithm/binary_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ void invoke_binary_tree() {
std::cout<<std::endl;
}

template<typename T>
int get_binary_tree_height(CBinaryTree<T>* node) {
if(!node) {
return 0;
}

int left_height = get_binary_tree_height(node->m_left_node) + 1;
int right_height = get_binary_tree_height(node->m_right_node) + 1;
return std::max(left_height, right_height);
}

template<typename T>
void right_side_view(CBinaryTree<T>* node, int level, std::vector<int>& output) {
if(!node) {
Expand All @@ -92,3 +103,51 @@ void invoke_right_side_view() {
std::cout<<node_value<<"->";
});
}

/*
3
/ \
9 20
/ \
15 7
RC: O(nh), SC: O(n+h)
*/
void level_order_traversal(CBinaryTree<int32_t>* node, int level, int actual_level, std::unordered_map<int, std::vector<int>>& value_map) {
if(!node) {
return;
}

if(level == 1) {
value_map[actual_level].push_back(node->m_data);
} else if(level > 1) {
level_order_traversal(node->m_left_node, level - 1, actual_level, value_map);
level_order_traversal(node->m_right_node, level - 1, actual_level, value_map);
}
}
void invoke_level_order_traversal() {
std::vector<std::vector<int>> output;
std::unordered_map<int, std::vector<int>> value_map;
CBinaryTree<int32_t> binary_tree(3);
binary_tree.m_left_node = new CBinaryTree<int32_t>(9);
binary_tree.m_right_node = new CBinaryTree<int32_t>(20);
binary_tree.m_right_node->m_left_node = new CBinaryTree<int32_t>(15);
binary_tree.m_right_node->m_right_node = new CBinaryTree<int32_t>(7);

int tree_height = get_binary_tree_height(&binary_tree);
for(int level = 1; level <= tree_height; ++level) {
level_order_traversal(&binary_tree, level, level, value_map);
}

for(int level = 1; level <= tree_height; ++level) {
output.push_back(value_map[level]);
}

std::cout<<"The level-order traversal: ";
std::for_each(output.begin(), output.end(), [&](std::vector<int>& nodes) {
for(auto& node: nodes) {
std::cout<<node<<", ";
}
std::cout<<"; ";
});
}
5 changes: 5 additions & 0 deletions algorithm/binary_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,9 @@ void invoke_binary_search_tree();
*/
void invoke_right_side_view();

/*
102. Binary Tree Level Order Traversal: https://leetcode.com/problems/binary-tree-level-order-traversal
*/
void invoke_level_order_traversal();

#endif /* BINARY_TREE_HPP */
6 changes: 5 additions & 1 deletion algorithm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
#include "graph.hpp"

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

// invoke_level_order_traversal();

// invoke_right_side_view();

// invoke_longest_palindromic_substring();

Expand Down
43 changes: 43 additions & 0 deletions algorithm/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,11 @@ nums1: [1,3], nums2: [2] => Output: 2.0
nums1: [1,2], nums2: [3,4] => Output: 2.50000
nums1: [1,3,4,5], nums: [2,3,6,7] => Output: 3.50000
Process:
Total elements: 8 -> even
mid_index = ((8/2)-1) => [3+1]/2 = 7/2 = 3.5
nums1: [4,5,6,7], nums2: [0,1,2,3]
*/
double median_of_two_sorted_arrays(std::vector<int>& nums1, std::vector<int>& nums2) {
double output = 0.0;
Expand Down Expand Up @@ -1546,3 +1551,41 @@ void invoke_two_sum() {
auto output = two_sum(input, 22);
std::cout<<"The indices of two sums are: "<<output[0]<<", "<<output[1]<<std::endl;
}

/*
Input: Output:
[1,2,3] [7,4,1]
[4,5,6] => [8,5,2]
[7,8,9] [9,6,3]
Process:
Reverse the input => [7, 8, 9]
[4, 5, 6]
[1, 2, 3]
R0C1
[7, 8, 9]R0C2
R1C0 [4, 5, 6]R1C2
R2C0 [1, 2, 3]
R2C1
Swap the symmetrical parts
*/
void rotate_image(std::vector<std::vector<int>>& input) {
std::reverse(input.begin(), input.end());
for(int row = 0; row < input.size(); ++row) {
for(int column = row + 1; column < input[row].size(); ++column) {
std::swap(input[row][column], input[column][row]);
}
}
}
void invoke_rotate_image() {
std::vector<std::vector<int>> input = {
{1,2,3},
{4,5,6},
{7,8,9},
};
rotate_image(input);
std::cout<<"The rotated matrix is: "<<std::endl;
std::for_each(input.begin(), input.end(), [](std::vector<int> row) {
std::cout<<row[0]<<", "<<row[1]<<", "<<row[2]<<std::endl;
});
}
5 changes: 5 additions & 0 deletions algorithm/number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,9 @@ void invoke_two_sum_ii();
*/
void invoke_two_sum();

/*
48. Rotate Image: https://leetcode.com/problems/rotate-image
*/
void invoke_rotate_image();

#endif /* NUMBER_HPP */

0 comments on commit 8f33a59

Please sign in to comment.