Skip to content

Commit

Permalink
Max product subarray (#189)
Browse files Browse the repository at this point in the history
* add max product subarray

* impl max product subarray
  • Loading branch information
SKTT1Ryze authored Mar 6, 2024
1 parent 4d1e8d2 commit a593b22
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
71 changes: 71 additions & 0 deletions leetcode-cc/MaxProductSubarray.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <algorithm>

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

using namespace std;

IMPLEMENT_PROBLEM_CLASS(PMaxProductSubarray, 152, DIFFI_MEDIUM,
TOPIC_ALGORITHMS, "Maximum Product Subarray",
"Given an integer array nums, find a subarray that has "
"the largest product, and return the product.",
{"DP"});

class SMaxProductSubarray : public ISolution {
public:
size_t problemId() const override { return 152; }
string name() const override {
return ("Solution for " + string("Maximum Product Subarray"));
}
string location() const override { return __FILE_NAME__; }
int test() const override {
return testHelper<vector<int>, int>({{2, 3, -2, 4}, {-2, 0, -1}}, {6, 0},
[this](auto input) {
// return this->maxProduct(input);
return this->maxProductV2(input);
});
};
int benchmark() const override { return 0; }

private:
// work, but time limit
int maxProduct(vector<int>& nums) const {
int n = nums.size();
vector<vector<int>> dp(n, vector(n, 0));
int res = INT_MIN;

for (int i = 0; i < n; i++) {
dp[i][i] = nums[i];
res = max(res, dp[i][i]);
}

for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
dp[i][j] = dp[i][j - 1] * nums[j];
res = max(res, dp[i][j]);
}
}

return res;
}

int maxProductV2(vector<int>& nums) const {
int n = nums.size();
if (n == 0) return 0;

int max_product = nums[0];
int min_product = nums[0];
int result = nums[0];

for (int i = 1; i < n; ++i) {
int temp_max = max_product;
int temp_min = min_product;
max_product = max({nums[i], nums[i] * temp_max, nums[i] * temp_min});
min_product = min({nums[i], nums[i] * temp_max, nums[i] * temp_min});
result = max(result, max_product);
}

return result;
}
};
6 changes: 6 additions & 0 deletions runtime-cc/src/registration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "../leetcode-cc/MaxDiffBetNodeAndAncestor.hpp"
#include "../leetcode-cc/MaxLenOfConcatenatedStrWithUniqueChars.hpp"
#include "../leetcode-cc/MaxPoints.hpp"
#include "../leetcode-cc/MaxProductSubarray.hpp"
#include "../leetcode-cc/MaximalRectangle.hpp"
#include "../leetcode-cc/MergeKSortedLists.hpp"
#include "../leetcode-cc/MergeSortedArray.hpp"
Expand Down Expand Up @@ -659,5 +660,10 @@ const int registerAll(std::shared_ptr<Container> handle) {
handle->registerSolution(
[]() -> ArcSolution { return std::make_shared<SReverseWords>(); });

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

return 0;
}

0 comments on commit a593b22

Please sign in to comment.