Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,17 @@ 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
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"
)

# ----------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "ContainerWithMostWater.h"

// /**
// * @brief Complexity
// * Time: O(n^2)
// * Space: O(1)
// */
// int ContainerWithMostWater::getMaxAmount(const std::vector<int>& 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 <iostream>
int ContainerWithMostWater::getMaxAmount(const std::vector<int>& 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<int>(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;
}
Original file line number Diff line number Diff line change
@@ -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 <vector>

class ContainerWithMostWater {
public:
int getMaxAmount(const std::vector<int>& height);
};
25 changes: 25 additions & 0 deletions src/leetcode/arrays/longest_common_prefix/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "Solution.h"
/**
* @brief Complexity
* - Time: O(n*m)
* - Space: O(1)
*/
std::string Solution::longestCommonPrefix(
const std::vector<std::string>& 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;
}
32 changes: 32 additions & 0 deletions src/leetcode/arrays/longest_common_prefix/Solution.h
Original file line number Diff line number Diff line change
@@ -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 <string>
#include <vector>

class Solution {
public:
std::string longestCommonPrefix(const std::vector<std::string>& strs);
};
16 changes: 16 additions & 0 deletions tests/container_with_most_water_ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <gtest/gtest.h>
#include "../src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.h"

TEST(ContainerWithMostWater, TC1) {
std::vector<int> 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<int> height{1, 1};
int expected = 1;
ContainerWithMostWater solution{};
EXPECT_EQ(expected, solution.getMaxAmount(height));
}
31 changes: 31 additions & 0 deletions tests/longest_common_prefix_ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <gtest/gtest.h>
#include "../src/leetcode/arrays/longest_common_prefix/Solution.h"

TEST(LongestCommonPrefix, TC1) {
std::vector<std::string> strs{"flower", "flow", "flight"};
std::string expected{"fl"};
Solution s{};
EXPECT_EQ(expected, s.longestCommonPrefix(strs));
}

TEST(LongestCommonPrefix, TC2) {
std::vector<std::string> strs{"dog","racecar","car"};
std::string expected{""};
Solution s{};
EXPECT_EQ(expected, s.longestCommonPrefix(strs));
}


TEST(LongestCommonPrefix, TC3) {
std::vector<std::string> strs{"a"};
std::string expected{"a"};
Solution s{};
EXPECT_EQ(expected, s.longestCommonPrefix(strs));
}

TEST(LongestCommonPrefix, TC4) {
std::vector<std::string> strs{"ab","a"};
std::string expected{"a"};
Solution s{};
EXPECT_EQ(expected, s.longestCommonPrefix(strs));
}