generated from TheLartians/ModernCppStarter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2. Added new problem: Product of Array Except Self - 238.
- Loading branch information
Showing
4 changed files
with
49 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |