I have added some comments to make the code easier to understand. Thanks.
Z
zhongliang.li.5
@zhongliang.li.5
17
Reputation
4
Posts
38
Profile views
0
Followers
0
Following
Posts made by zhongliang.li.5

RE: Share the First C++ Solution with Notes

RE: Share the First C++ Solution with Notes
class Solution { public: int maxProduct(int A[], int n) { if (n < 1) return 0; int r = A[0]; int max_p = A[0]; // max_p is the maximum product that could be achieved // from the subarrays ending at the current position. int min_p = A[0]; // The minimum product that could be achieved from // the subarrays ending at the current position. for(int i=1; i<n; i++){ // The maximum or minimum product of the subarrays // ending at the current position could be achieved from the next three values. int a = max_p*A[i]; // the max product of subarray ending at the previous position multiply the current value int b = min_p*A[i]; // the minimum product of subarray ending at the previous position multiply the current value int c = A[i]; // the current value max_p = max(max(a, b), c); min_p = min(min(a, b), c); if (max_p > r) r = max_p; } return r; } };
Same idea. But it looks simpler.

RE: Sort function problems
"This comparison shall produce a strict weak ordering of the elements (i.e., a consistent transitive comparison, without considering its reflexiveness)."
from http://www.cplusplus.com/reference/list/list/sort/