diff --git a/CPP/data_structures/graphs/graph/max_area_of_island.cpp b/CPP/data_structures/graphs/graph/max_area_of_island.cpp new file mode 100644 index 00000000..15d36f2d --- /dev/null +++ b/CPP/data_structures/graphs/graph/max_area_of_island.cpp @@ -0,0 +1,53 @@ +#include +#include +using namespace std; + +class Solution { +public: + int maxAreaOfIsland(vector>& matrix) { + int maxArea = 0; + for (int row = 0; row < matrix.size(); row++) { + for (int col = 0; col < matrix[0].size(); col++) { + if (matrix[row][col] == 1) { + maxArea = max(maxArea, explore(matrix, row, col)); + } + } + } + return maxArea; + } + +private: + int explore(vector>& matrix, int row, int col) { + if (matrix[row][col] == 0) return 0; + + matrix[row][col] = 0; + int area = 1; + + if (row - 1 >= 0) area += explore(matrix, row - 1, col); + if (row + 1 < matrix.size()) area += explore(matrix, row + 1, col); + if (col - 1 >= 0) area += explore(matrix, row, col - 1); + if (col + 1 < matrix[0].size()) area += explore(matrix, row, col + 1); + + return area; + } +}; + +int main() { + Solution solution; + + vector> matrix = { + {0,0,1,0,0,0,0,1,0,0,0,0,0}, + {0,0,0,0,0,0,0,1,1,1,0,0,0}, + {0,1,1,0,1,0,0,0,0,0,0,0,0}, + {0,1,0,0,1,1,0,0,1,0,1,0,0}, + {0,1,0,0,1,1,0,0,1,1,1,0,0}, + {0,0,0,0,0,0,0,0,0,0,1,0,0}, + {0,0,0,0,0,0,0,1,1,1,0,0,0}, + {0,0,0,0,0,0,0,1,1,0,0,0,0} + }; + + int result = solution.maxAreaOfIsland(matrix); + cout << "Max Area of Island: " << result << endl; + + return 0; +} diff --git a/CPP/dynamic_programming/longest_increasing_subsequence.cpp b/CPP/dynamic_programming/longest_increasing_subsequence.cpp new file mode 100644 index 00000000..2522c3e9 --- /dev/null +++ b/CPP/dynamic_programming/longest_increasing_subsequence.cpp @@ -0,0 +1,34 @@ +#include +#include +#include + +int lengthOfLIS(std::vector& nums) { + if (nums.empty()) return 0; + + std::vector dp(nums.size(), 1); + int maxLen = 1; + + for (int i = 1; i < nums.size(); i++) { + for (int j = 0; j < i; j++) { + if (nums[i] > nums[j]) { + dp[i] = std::max(dp[i], dp[j] + 1); + } + } + maxLen = std::max(maxLen, dp[i]); + } + + return maxLen; +} + +int main() { + std::vector nums = {10, 9, 2, 5, 3, 7, 101, 18}; + std::cout << lengthOfLIS(nums) << std::endl; + + nums = {0, 1, 0, 3, 2, 3}; + std::cout << lengthOfLIS(nums) << std::endl; + + nums = {7, 7, 7, 7, 7, 7, 7}; + std::cout << lengthOfLIS(nums) << std::endl; + + return 0; +} \ No newline at end of file