Leetcode 隨筆 - 152. Maximum Product Subarray
題目難度:Medium
題目連結: Maximum Product Subarray
題目大綱
給你一個整數陣列, 找出擁有最大乘積的子陣列 (contiguous non-empty subarray)
程式碼
class Solution {
public:
int maxProduct(vector<int>& nums) {
int ans = nums[0];
int max_product = 1;
int min_product = 1;
int t1, t2;
for (int i = 0; i < nums.size(); i++)
{
t1 = max_product * nums[i];
t2 = min_product * nums[i];
max_product = max(t1, max(t2, nums[i]));
min_product = min(t1, min(t2, nums[i]));
ans = max(max_product, ans);
}
return ans;
}
};
評論由 Disqus