Skip to content

Commit

Permalink
1. Formatting fixes.
Browse files Browse the repository at this point in the history
2. Added new problem: Product of Array Except Self - 238.
  • Loading branch information
bibinyana committed Jun 19, 2024
1 parent 0fe69ff commit 0244c2e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
18 changes: 9 additions & 9 deletions include/leeter/h_index_274.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include <vector>
#include <algorithm>
using std::vector;
#include <vector>
using std::sort;
using std::vector;

namespace h_index {
class Solution {
public:
int hIndex(vector<int>& citations) {
sort(citations.begin(), citations.end());
int n = citations.size();
for (int i = 0; i < n; ++i) {
if (citations[i] >= n - i) {
return n - i;
}
sort(citations.begin(), citations.end());
int n = citations.size();
for (int i = 0; i < n; ++i) {
if (citations[i] >= n - i) {
return n - i;
}
return 0;
}
return 0;
}
};
} // namespace h_index
22 changes: 22 additions & 0 deletions include/leeter/product_of_array_except_self_238.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <vector>
using std::vector;

namespace product_of_array_except_self {
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int prefix_sum = 1;
int suffix_sum = 1;
vector<int> result(nums.size(), 1);
for (int i = 0; i < nums.size(); i++) {
result[i] = prefix_sum;
prefix_sum *= nums[i];
}
for (int i = nums.size() - 1; i >= 0; i--) {
result[i] *= suffix_sum;
suffix_sum *= nums[i];
}
return result;
}
};
} // namespace product_of_array_except_self
2 changes: 1 addition & 1 deletion test/source/test_h_index_274.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <doctest/doctest.h>
#include "leeter/h_index_274.hpp"

#include "leeter/h_index_274.hpp"

TEST_CASE("h index: test case 1") {
vector<int> input = {3, 0, 6, 1, 5};
Expand Down
17 changes: 17 additions & 0 deletions test/source/test_product_of_array_238.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <doctest/doctest.h>

#include "leeter/product_of_array_except_self_238.hpp"

TEST_CASE("Product of Array Except Self") {
product_of_array_except_self::Solution s;
std::vector<int> nums = {1, 2, 3, 4};
std::vector<int> expected = {24, 12, 8, 6};
CHECK(s.productExceptSelf(nums) == expected);
}

TEST_CASE("Product of Array Except Self - Test case 2") {
product_of_array_except_self::Solution s;
std::vector<int> nums = {-1, 1, 0, -3, 3};
std::vector<int> expected = {0, 0, 9, 0, 0};
CHECK(s.productExceptSelf(nums) == expected);
}

0 comments on commit 0244c2e

Please sign in to comment.