赞
踩
- class Solution {
- public:
- int maxProduct(vector<int>& nums) {
- int n = nums.size();
- // dp[i][0]表示以i结尾的乘积最小值,dp[i][1]表示最大值
- int maxDP = nums[0];
- int minDP = nums[0];
-
- int result = nums[0];
- for (int i = 1; i < n; ++i) {
- if (nums[i] < 0) swap(maxDP, minDP);
-
- maxDP = max(maxDP * nums[i], nums[i]);
- minDP = min(minDP * nums[i], nums[i]);
- result = max(result, maxDP);
- }
- return result;
-
- }
- };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。