GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — LeetCode/Dynamic_Programming/152.Maximum-Product-Subarray at master · wisdompeak/LeetCode

Expand file tree

/

152.Maximum-Product-Subarray

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

152.Maximum-Product-Subarray

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
152.Maximum-Product-Subarray_DP.cpp
152.Maximum-Product-Subarray_DP.cpp
 
 
Readme.md
Readme.md
 
 

Readme.md

152.Maximum-Product-Subarray

此题是053.Maximum-Subarray的进阶版。本题的关键点在于,如果nums[i]是负数,那么求以i为结尾的最大乘积子数组,其实转化为求以i-1为结尾的最小乘积子数组。所以我们每次考察以i为结尾的子数组时,需要同时保留最大和最小值。于是状态转移方程:

            curMax = max(max(preMin*nums[i],preMax*nums[i]),nums[i]);
            curMin = min(min(preMin*nums[i],preMax*nums[i]),nums[i]);            

最后返回的结果不是最后的curMax,而是遍历每个回合的curMax中的最大值。

Leetcode Link

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.