Skip to content

Commit 5bded1a

Browse files
authored
Merge pull request #33 from urbytes21/dev_branch
id 1769959868
2 parents 0b87ff0 + d606c20 commit 5bded1a

File tree

7 files changed

+212
-2
lines changed

7 files changed

+212
-2
lines changed

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,17 @@ set(APP_SOURCES
132132
"src/core/expression/FunctionPointer.cpp"
133133
"src/leetcode/arrays/two_sum/TwoSum.cpp"
134134
"src/leetcode/arrays/median_two_arrays/MedianTwoSortedArrays.cpp"
135-
# "src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.cpp"
135+
"src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.cpp"
136+
"src/leetcode/arrays/longest_common_prefix/Solution.cpp"
136137
)
137138

138139
# Test files
139140
set(APP_TESTS
140141
"tests/DeleteMeTest.cpp"
141142
"tests/two_sum_ut.cpp"
142143
"tests/median_two_sorted_arrays_ut.cpp"
143-
# "tests/container_with_most_water_ut.cpp"
144+
"tests/container_with_most_water_ut.cpp"
145+
"tests/longest_common_prefix_ut.cpp"
144146
)
145147

146148
# ----------------------------------------------------------------------------------------
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "ContainerWithMostWater.h"
2+
3+
// /**
4+
// * @brief Complexity
5+
// * Time: O(n^2)
6+
// * Space: O(1)
7+
// */
8+
// int ContainerWithMostWater::getMaxAmount(const std::vector<int>& height) {
9+
// std::size_t size = height.size();
10+
// int max{};
11+
// for (std::size_t i = 0; i < size - 1; ++i) {
12+
// for (std::size_t j = i + 1; j < size; ++j) {
13+
// int w = j - i;
14+
// int h = std::min(height.at(i), height.at(j));
15+
// max = std::max(max, w * h);
16+
// }
17+
// }
18+
19+
// return max;
20+
// }
21+
22+
/**
23+
* @brief Two pointers solution
24+
* Complexity
25+
* Time: O(n)
26+
* Space: O(1)
27+
*/
28+
#include <iostream>
29+
int ContainerWithMostWater::getMaxAmount(const std::vector<int>& height) {
30+
std::size_t size = height.size();
31+
if (size < 2) {
32+
return -1;
33+
}
34+
35+
// size_t index = it - v.begin();
36+
auto lpos = height.begin();
37+
auto rpos = height.end() - 1; // end point to the last one e
38+
int result{0};
39+
while (lpos < rpos) {
40+
int h = std::min(*lpos, *rpos);
41+
int w = static_cast<int>(rpos - lpos);
42+
int t = h * w;
43+
result = std::max(result, t);
44+
45+
// the height of the container is determined by the shorter position
46+
// so it means we do not need to change the taller one.
47+
if (*lpos < *rpos) {
48+
lpos += 1;
49+
} else {
50+
rpos -= 1;
51+
}
52+
}
53+
54+
return result;
55+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//cppcheck-suppress-file [functionStatic]
2+
3+
/* 11. Container With Most Water
4+
Medium
5+
6+
Hint:
7+
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]).
8+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
9+
Return the maximum amount of water a container can store.
10+
Notice that you may not slant the container.
11+
12+
Example 1:
13+
Input: height = [1,8,6,2,5,4,8,3,7]
14+
Output: 49
15+
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.
16+
17+
Example 2:
18+
Input: height = [1,1]
19+
Output: 1
20+
21+
Constraints:
22+
n == height.length
23+
2 <= n <= 105
24+
0 <= height[i] <= 104
25+
26+
====
27+
...--.................................................................................
28+
.----........----.......................................---...........................
29+
.-----.......-+#----------------------------------------##------------------..........
30+
.----........-+#+---------------------------------------##+-------------+#--..........
31+
.----........-+#+-------##------------------------------##+-------------+#--..........
32+
..---........-+#+-------##--------------##+-------------##+-------------+#--..........
33+
.----........-+#+-------##--------------##+-----##------##+-------------+#--..........
34+
.----........-+#+-------##--------------##+-----##------##+-----##------+#--..........
35+
..---........-+#+-------##------##------##+-----##------##+-----##------+#--..........
36+
.-------##----+#+-------##------##------##+-----##------##+-----##------+#-------.....
37+
........0......1.........................................................8............
38+
39+
h[1] & h[8]
40+
*/
41+
42+
#pragma once
43+
44+
#include <vector>
45+
46+
class ContainerWithMostWater {
47+
public:
48+
int getMaxAmount(const std::vector<int>& height);
49+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "Solution.h"
2+
/**
3+
* @brief Complexity
4+
* - Time: O(n*m)
5+
* - Space: O(1)
6+
*/
7+
std::string Solution::longestCommonPrefix(
8+
const std::vector<std::string>& strs) {
9+
const std::string& str_benchmark =
10+
strs.at(0); // use 1st string as a benchmark
11+
12+
// Iterator charactor-by-charator over the first string
13+
// to compare it against all other strings
14+
for (size_t i = 0; i < str_benchmark.size(); ++i) {
15+
char c = str_benchmark.at(i);
16+
for (size_t j = 1; j < strs.size(); ++j) {
17+
const std::string& str = strs.at(j);
18+
if (i >= str.size() || c != str.at(i)) {
19+
return str_benchmark.substr(0, i);
20+
}
21+
}
22+
}
23+
24+
return str_benchmark;
25+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//cppcheck-suppress-file [functionStatic,ctuOneDefinitionRuleViolation]
2+
3+
// 14 - E
4+
// Write a function to find the longest common prefix string amongst an array of strings.
5+
// If there is no common prefix, return an empty string "".
6+
// Example 1:
7+
8+
// Input: strs = ["flower","flow","flight"]
9+
// Output: "fl"
10+
// Example 2:
11+
12+
// Input: strs = ["dog","racecar","car"]
13+
// Output: ""
14+
// Explanation: There is no common prefix among the input strings.
15+
16+
// Constraints:
17+
18+
// 1 <= strs.length <= 200
19+
// 0 <= strs[i].length <= 200
20+
// strs[i] consists of only lowercase English letters if it is non-empty.
21+
22+
// Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity ?
23+
24+
#pragma once
25+
26+
#include <string>
27+
#include <vector>
28+
29+
class Solution {
30+
public:
31+
std::string longestCommonPrefix(const std::vector<std::string>& strs);
32+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <gtest/gtest.h>
2+
#include "../src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.h"
3+
4+
TEST(ContainerWithMostWater, TC1) {
5+
std::vector<int> height{1, 8, 6, 2, 5, 4, 8, 3, 7};
6+
int expected = 49;
7+
ContainerWithMostWater solution{};
8+
EXPECT_EQ(expected, solution.getMaxAmount(height));
9+
}
10+
11+
TEST(ContainerWithMostWater, TC2) {
12+
std::vector<int> height{1, 1};
13+
int expected = 1;
14+
ContainerWithMostWater solution{};
15+
EXPECT_EQ(expected, solution.getMaxAmount(height));
16+
}

tests/longest_common_prefix_ut.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <gtest/gtest.h>
2+
#include "../src/leetcode/arrays/longest_common_prefix/Solution.h"
3+
4+
TEST(LongestCommonPrefix, TC1) {
5+
std::vector<std::string> strs{"flower", "flow", "flight"};
6+
std::string expected{"fl"};
7+
Solution s{};
8+
EXPECT_EQ(expected, s.longestCommonPrefix(strs));
9+
}
10+
11+
TEST(LongestCommonPrefix, TC2) {
12+
std::vector<std::string> strs{"dog","racecar","car"};
13+
std::string expected{""};
14+
Solution s{};
15+
EXPECT_EQ(expected, s.longestCommonPrefix(strs));
16+
}
17+
18+
19+
TEST(LongestCommonPrefix, TC3) {
20+
std::vector<std::string> strs{"a"};
21+
std::string expected{"a"};
22+
Solution s{};
23+
EXPECT_EQ(expected, s.longestCommonPrefix(strs));
24+
}
25+
26+
TEST(LongestCommonPrefix, TC4) {
27+
std::vector<std::string> strs{"ab","a"};
28+
std::string expected{"a"};
29+
Solution s{};
30+
EXPECT_EQ(expected, s.longestCommonPrefix(strs));
31+
}

0 commit comments

Comments
 (0)