diff --git a/CMakeLists.txt b/CMakeLists.txt index 431cd52..540975a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -132,7 +132,8 @@ set(APP_SOURCES "src/core/expression/FunctionPointer.cpp" "src/leetcode/arrays/two_sum/TwoSum.cpp" "src/leetcode/arrays/median_two_arrays/MedianTwoSortedArrays.cpp" - # "src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.cpp" + "src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.cpp" + "src/leetcode/arrays/longest_common_prefix/Solution.cpp" ) # Test files @@ -140,7 +141,8 @@ set(APP_TESTS "tests/DeleteMeTest.cpp" "tests/two_sum_ut.cpp" "tests/median_two_sorted_arrays_ut.cpp" - # "tests/container_with_most_water_ut.cpp" + "tests/container_with_most_water_ut.cpp" + "tests/longest_common_prefix_ut.cpp" ) # ---------------------------------------------------------------------------------------- diff --git a/src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.cpp b/src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.cpp new file mode 100644 index 0000000..9986c05 --- /dev/null +++ b/src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.cpp @@ -0,0 +1,55 @@ +#include "ContainerWithMostWater.h" + +// /** +// * @brief Complexity +// * Time: O(n^2) +// * Space: O(1) +// */ +// int ContainerWithMostWater::getMaxAmount(const std::vector& height) { +// std::size_t size = height.size(); +// int max{}; +// for (std::size_t i = 0; i < size - 1; ++i) { +// for (std::size_t j = i + 1; j < size; ++j) { +// int w = j - i; +// int h = std::min(height.at(i), height.at(j)); +// max = std::max(max, w * h); +// } +// } + +// return max; +// } + +/** + * @brief Two pointers solution + * Complexity + * Time: O(n) + * Space: O(1) + */ +#include +int ContainerWithMostWater::getMaxAmount(const std::vector& height) { + std::size_t size = height.size(); + if (size < 2) { + return -1; + } + + // size_t index = it - v.begin(); + auto lpos = height.begin(); + auto rpos = height.end() - 1; // end point to the last one e + int result{0}; + while (lpos < rpos) { + int h = std::min(*lpos, *rpos); + int w = static_cast(rpos - lpos); + int t = h * w; + result = std::max(result, t); + + // the height of the container is determined by the shorter position + // so it means we do not need to change the taller one. + if (*lpos < *rpos) { + lpos += 1; + } else { + rpos -= 1; + } + } + + return result; +} \ No newline at end of file diff --git a/src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.h b/src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.h new file mode 100644 index 0000000..753591d --- /dev/null +++ b/src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.h @@ -0,0 +1,49 @@ +//cppcheck-suppress-file [functionStatic] + +/* 11. Container With Most Water +Medium + +Hint: +You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). +Find two lines that together with the x-axis form a container, such that the container contains the most water. +Return the maximum amount of water a container can store. +Notice that you may not slant the container. + +Example 1: +Input: height = [1,8,6,2,5,4,8,3,7] +Output: 49 +Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49. + +Example 2: +Input: height = [1,1] +Output: 1 + +Constraints: +n == height.length +2 <= n <= 105 +0 <= height[i] <= 104 + +==== + ...--................................................................................. + .----........----.......................................---........................... + .-----.......-+#----------------------------------------##------------------.......... + .----........-+#+---------------------------------------##+-------------+#--.......... + .----........-+#+-------##------------------------------##+-------------+#--.......... + ..---........-+#+-------##--------------##+-------------##+-------------+#--.......... + .----........-+#+-------##--------------##+-----##------##+-------------+#--.......... + .----........-+#+-------##--------------##+-----##------##+-----##------+#--.......... + ..---........-+#+-------##------##------##+-----##------##+-----##------+#--.......... + .-------##----+#+-------##------##------##+-----##------##+-----##------+#-------..... + ........0......1.........................................................8............ + +h[1] & h[8] +*/ + +#pragma once + +#include + +class ContainerWithMostWater { + public: + int getMaxAmount(const std::vector& height); +}; \ No newline at end of file diff --git a/src/leetcode/arrays/longest_common_prefix/Solution.cpp b/src/leetcode/arrays/longest_common_prefix/Solution.cpp new file mode 100644 index 0000000..1e57e16 --- /dev/null +++ b/src/leetcode/arrays/longest_common_prefix/Solution.cpp @@ -0,0 +1,25 @@ +#include "Solution.h" +/** + * @brief Complexity + * - Time: O(n*m) + * - Space: O(1) + */ +std::string Solution::longestCommonPrefix( + const std::vector& strs) { + const std::string& str_benchmark = + strs.at(0); // use 1st string as a benchmark + + // Iterator charactor-by-charator over the first string + // to compare it against all other strings + for (size_t i = 0; i < str_benchmark.size(); ++i) { + char c = str_benchmark.at(i); + for (size_t j = 1; j < strs.size(); ++j) { + const std::string& str = strs.at(j); + if (i >= str.size() || c != str.at(i)) { + return str_benchmark.substr(0, i); + } + } + } + + return str_benchmark; +} diff --git a/src/leetcode/arrays/longest_common_prefix/Solution.h b/src/leetcode/arrays/longest_common_prefix/Solution.h new file mode 100644 index 0000000..3c66e1c --- /dev/null +++ b/src/leetcode/arrays/longest_common_prefix/Solution.h @@ -0,0 +1,32 @@ +//cppcheck-suppress-file [functionStatic,ctuOneDefinitionRuleViolation] + +// 14 - E +// Write a function to find the longest common prefix string amongst an array of strings. +// If there is no common prefix, return an empty string "". +// Example 1: + +// Input: strs = ["flower","flow","flight"] +// Output: "fl" +// Example 2: + +// Input: strs = ["dog","racecar","car"] +// Output: "" +// Explanation: There is no common prefix among the input strings. + +// Constraints: + +// 1 <= strs.length <= 200 +// 0 <= strs[i].length <= 200 +// strs[i] consists of only lowercase English letters if it is non-empty. + +// Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity ? + +#pragma once + +#include +#include + +class Solution { + public: + std::string longestCommonPrefix(const std::vector& strs); +}; diff --git a/tests/container_with_most_water_ut.cpp b/tests/container_with_most_water_ut.cpp new file mode 100644 index 0000000..296e6f8 --- /dev/null +++ b/tests/container_with_most_water_ut.cpp @@ -0,0 +1,16 @@ +#include +#include "../src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.h" + +TEST(ContainerWithMostWater, TC1) { + std::vector height{1, 8, 6, 2, 5, 4, 8, 3, 7}; + int expected = 49; + ContainerWithMostWater solution{}; + EXPECT_EQ(expected, solution.getMaxAmount(height)); +} + +TEST(ContainerWithMostWater, TC2) { + std::vector height{1, 1}; + int expected = 1; + ContainerWithMostWater solution{}; + EXPECT_EQ(expected, solution.getMaxAmount(height)); +} \ No newline at end of file diff --git a/tests/longest_common_prefix_ut.cpp b/tests/longest_common_prefix_ut.cpp new file mode 100644 index 0000000..6af8255 --- /dev/null +++ b/tests/longest_common_prefix_ut.cpp @@ -0,0 +1,31 @@ +#include +#include "../src/leetcode/arrays/longest_common_prefix/Solution.h" + +TEST(LongestCommonPrefix, TC1) { + std::vector strs{"flower", "flow", "flight"}; + std::string expected{"fl"}; + Solution s{}; + EXPECT_EQ(expected, s.longestCommonPrefix(strs)); +} + +TEST(LongestCommonPrefix, TC2) { + std::vector strs{"dog","racecar","car"}; + std::string expected{""}; + Solution s{}; + EXPECT_EQ(expected, s.longestCommonPrefix(strs)); +} + + +TEST(LongestCommonPrefix, TC3) { + std::vector strs{"a"}; + std::string expected{"a"}; + Solution s{}; + EXPECT_EQ(expected, s.longestCommonPrefix(strs)); +} + +TEST(LongestCommonPrefix, TC4) { + std::vector strs{"ab","a"}; + std::string expected{"a"}; + Solution s{}; + EXPECT_EQ(expected, s.longestCommonPrefix(strs)); +} \ No newline at end of file