Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of product maximum subsequence in leetcode

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report