Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product of array except self #253

Merged
merged 3 commits into from
May 6, 2024
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
60 changes: 60 additions & 0 deletions leetcode-cc/ProductOfArrayExceptSelf.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <numeric>

#include "TestHelper.h"
#include "problem.h"
#include "solution.h"

using namespace std;

IMPLEMENT_PROBLEM_CLASS(
PProductOfArrayExceptSelf, 238, DIFFI_MEDIUM, TOPIC_ALGORITHMS,
"Product of Array Except Self",
"Given an integer array nums, return an array answer such that answer[i] "
"is equal to the product of all the elements of nums except nums[i].",
{"Array"});

class SProductOfArrayExceptSelf : public ISolution {
public:
size_t problemId() const override { return 238; }
string name() const override {
return ("Solution for " + string("Product of Array Expect Self"));
}
string location() const override { return __FILE_NAME__; }
int test() const override {
return testHelper<vector<int>, vector<int>>(
{{1, 2, 3, 4}, {-1, 1, 0, -3, 3}}, {{24, 12, 8, 6}, {0, 0, 9, 0, 0}},
[this](auto input) { return this->productExceptSelf(input); });
};
int benchmark() const override { return 0; }

private:
vector<int> productExceptSelf(vector<int>& nums) const {
int n = nums.size();
auto zeroCount = count(nums.begin(), nums.end(), 0);
if (zeroCount > 1) {
vector<int> ans(n, 0);
return ans;
}
int product = accumulate(nums.begin(), nums.end(), 1, [](int a, int b) {
if (a != 0 && b != 0) {
return a * b;
} else if (a == 0) {
return b;
} else {
return a;
}
});
vector<int> ans(n, 0);
for (int i = 0; i < n; i++) {
if (nums[i] == 0) {
ans[i] = product;
} else if (zeroCount > 0) {
ans[i] = 0;
} else {
ans[i] = product / nums[i];
}
}

return ans;
}
};
8 changes: 8 additions & 0 deletions runtime-cc/src/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
#include "../leetcode-cc/PathSum.hpp"
#include "../leetcode-cc/PathSumII.hpp"
#include "../leetcode-cc/PopulateNextRightPointers.hpp"
#include "../leetcode-cc/ProductOfArrayExceptSelf.hpp"
#include "../leetcode-cc/PseudoPalindromicPathsInBTree.hpp"
#include "../leetcode-cc/RecoverBST.hpp"
#include "../leetcode-cc/RectangleArea.hpp"
Expand Down Expand Up @@ -974,5 +975,12 @@ const int registerAll(std::shared_ptr<Container> handle) {
return std::make_shared<SDeleteNodeInLinkedList>();
});

handle->registerProblem([]() -> ArcProblem {
return std::make_shared<PProductOfArrayExceptSelf>();
});
handle->registerSolution([]() -> ArcSolution {
return std::make_shared<SProductOfArrayExceptSelf>();
});

return 0;
}
Loading