i implemented the same code and tried to submit it on leetcode but it is giving me wa for test case [-1,-1,0] . Expected answer is 1 while this code is giving 0. How come he passes all the test cases in the video?
Same code giving wrong answer on leetcode
Link to code-> https://ide.codingblocks.com/s/644048
Link to question-> https://leetcode.com/problems/maximum-product-subarray/
Here, this same code is passing all test cases on coding blocks but showing wa on leetcode.
please reply soon
hi @ikshuldureja130
ig the soln is not fully correct that’s why code failed on some corner test cases… I have informed the academic team, they will look into it…
till then u can refer this other approach
The idea is to track prior maximum product maxv and minimum product minv. Depends on the value of current element i, we may need to choose from minv * i or maxv * i, also do not forget element i should be compared.
int maxProduct(vector<int>& nums) {
int minv = 1, maxv = 1, ans = INT_MIN;
for (int i : nums) {
int _minv = min(i, min(minv * i, maxv * i));
int _maxv = max(i, max(minv * i, maxv * i));
minv = _minv, maxv = _maxv;
ans = max(ans, maxv);
}
return ans;
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.