From 6ff656711a8c3c80d4ef84c90bc5c6a69bd48caa Mon Sep 17 00:00:00 2001 From: Phong Nguyen Date: Fri, 30 Jan 2026 13:58:14 +0700 Subject: [PATCH 1/3] add LC0011 --- CMakeLists.txt | 4 +- .../ContainerWithMostWater.cpp | 55 +++++++++++++++++++ .../ContainerWithMostWater.h | 49 +++++++++++++++++ tests/container_with_most_water_ut.cpp | 16 ++++++ 4 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.cpp create mode 100644 src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.h create mode 100644 tests/container_with_most_water_ut.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 431cd52..102dd43 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -132,7 +132,7 @@ 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" ) # Test files @@ -140,7 +140,7 @@ 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" ) # ---------------------------------------------------------------------------------------- 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..3152ca1 --- /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& heights) { +// std::size_t size = heights.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(heights.at(i), heights.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& heights) { + std::size_t size = heights.size(); + if (size < 2) { + return -1; + } + + // size_t index = it - v.begin(); + auto lpos = heights.begin(); + auto rpos = heights.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..06fc0ef --- /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& heights); +}; \ No newline at end of file diff --git a/tests/container_with_most_water_ut.cpp b/tests/container_with_most_water_ut.cpp new file mode 100644 index 0000000..b8cc241 --- /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 heights{1, 8, 6, 2, 5, 4, 8, 3, 7}; + int expected = 49; + ContainerWithMostWater solution{}; + EXPECT_EQ(expected, solution.getMaxAmount(heights)); +} + +TEST(ContainerWithMostWater, TC2) { + std::vector heights{1, 1}; + int expected = 1; + ContainerWithMostWater solution{}; + EXPECT_EQ(expected, solution.getMaxAmount(heights)); +} \ No newline at end of file From c03af31628e257b3e7febff6da05f47a01c0f068 Mon Sep 17 00:00:00 2001 From: Phong Nguyen Date: Sun, 1 Feb 2026 15:14:13 +0700 Subject: [PATCH 2/3] Fix typo --- .../ContainerWithMostWater.cpp | 14 +++++++------- .../ContainerWithMostWater.h | 2 +- tests/container_with_most_water_ut.cpp | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.cpp b/src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.cpp index 3152ca1..9986c05 100644 --- a/src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.cpp +++ b/src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.cpp @@ -5,13 +5,13 @@ // * Time: O(n^2) // * Space: O(1) // */ -// int ContainerWithMostWater::getMaxAmount(const std::vector& heights) { -// std::size_t size = heights.size(); +// 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(heights.at(i), heights.at(j)); +// int h = std::min(height.at(i), height.at(j)); // max = std::max(max, w * h); // } // } @@ -26,15 +26,15 @@ * Space: O(1) */ #include -int ContainerWithMostWater::getMaxAmount(const std::vector& heights) { - std::size_t size = heights.size(); +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 = heights.begin(); - auto rpos = heights.end() - 1; // end point to the last one e + 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); diff --git a/src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.h b/src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.h index 06fc0ef..753591d 100644 --- a/src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.h +++ b/src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.h @@ -45,5 +45,5 @@ h[1] & h[8] class ContainerWithMostWater { public: - int getMaxAmount(const std::vector& heights); + int getMaxAmount(const std::vector& height); }; \ No newline at end of file diff --git a/tests/container_with_most_water_ut.cpp b/tests/container_with_most_water_ut.cpp index b8cc241..296e6f8 100644 --- a/tests/container_with_most_water_ut.cpp +++ b/tests/container_with_most_water_ut.cpp @@ -2,15 +2,15 @@ #include "../src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.h" TEST(ContainerWithMostWater, TC1) { - std::vector heights{1, 8, 6, 2, 5, 4, 8, 3, 7}; + std::vector height{1, 8, 6, 2, 5, 4, 8, 3, 7}; int expected = 49; ContainerWithMostWater solution{}; - EXPECT_EQ(expected, solution.getMaxAmount(heights)); + EXPECT_EQ(expected, solution.getMaxAmount(height)); } TEST(ContainerWithMostWater, TC2) { - std::vector heights{1, 1}; + std::vector height{1, 1}; int expected = 1; ContainerWithMostWater solution{}; - EXPECT_EQ(expected, solution.getMaxAmount(heights)); + EXPECT_EQ(expected, solution.getMaxAmount(height)); } \ No newline at end of file From d606c200dd4661bb7ba5458e89e4921b8d53c041 Mon Sep 17 00:00:00 2001 From: Phong Nguyen Date: Sun, 1 Feb 2026 22:30:49 +0700 Subject: [PATCH 3/3] LC 14 --- CMakeLists.txt | 2 ++ .../arrays/longest_common_prefix/Solution.cpp | 25 +++++++++++++++ .../arrays/longest_common_prefix/Solution.h | 32 +++++++++++++++++++ tests/longest_common_prefix_ut.cpp | 31 ++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 src/leetcode/arrays/longest_common_prefix/Solution.cpp create mode 100644 src/leetcode/arrays/longest_common_prefix/Solution.h create mode 100644 tests/longest_common_prefix_ut.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 102dd43..540975a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -133,6 +133,7 @@ set(APP_SOURCES "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/longest_common_prefix/Solution.cpp" ) # Test files @@ -141,6 +142,7 @@ set(APP_TESTS "tests/two_sum_ut.cpp" "tests/median_two_sorted_arrays_ut.cpp" "tests/container_with_most_water_ut.cpp" + "tests/longest_common_prefix_ut.cpp" ) # ---------------------------------------------------------------------------------------- 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/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