How to solve the problem of product maximum subsequence in leetcode
This article mainly introduces how to solve the problem of the largest subsequence of product in leetcode, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
Topic link
Https://leetcode-cn.com/problems/maximum-product-subarray/
Topic description
Given an integer array nums, find the continuous subsequence with the largest product in a sequence (the sequence contains at least one number).
Example 1:
Output: 6 explanation: subarray [2mem3] has a maximum product 6.
Example 2:
Output: 0 explanation: the result cannot be 2, because [- 2 memoir 1] is not a subarray. The idea of solving the problem
Tags: dynamic plannin
Calculate the current maximum when traversing the array, constantly updating
If imax is the current maximum, the current maximum is imax = max (imax * nums [I], nums [I])
Because of the existence of negative numbers, it will cause the largest to become the smallest and the smallest to the largest. Therefore, you also need to maintain the current minimum value imin,imin = min (伊明 * nums [I], nums [I])
When a negative number appears, imax exchanges with 伊明 and then carries on the next calculation.
Time complexity: O (n)
Code class Solution {public int maxProduct (int [] nums) {int max = Integer.MIN_VALUE, imax = 1, 伊明 = 1; for (int iTuno; I