problem:https://leetcode.com/problems/maximum-product-subarray
类似买卖股票,需要维护两个状态,当前最大数和最小数。
class Solution { public: int maxProduct(vector<int>& nums) { int res = INT_MIN; int num_max = 1; int num_min = 1; for(int i = 0;i<nums.size();i++) { int a = num_max * nums[i]; int b = num_min * nums[i]; num_max = max({a, b, nums[i]}); num_min = min({a, b, nums[i]}); res = max(res, num_max); } return res; } };
原文地址:https://www.cnblogs.com/fish1996/p/11330291.html
时间: 2024-10-08 02:55:32