Maximum Product Subarray, maximumsubarray

Source: Internet
Author: User

Maximum Product Subarray, maximumsubarray

Leetcode has been updated !!!

Find the contiguous subarray within an array (containing at least one number) which has the largest product.

For example, given the array[2,3,-2,4],
The contiguous subarray[2,3]Has the largest product =6.

The first thought is to dynamically plan to use path [I] [j] to record the product of I to j. The code for updating the maximum value during calculation is as follows:

public class Solution {    public int maxProduct(int[] A) {int len = A.length;int [][] path = new int[len][];for(int i=0;i<len;i++){path[i] = new int[len];}int MaxPro = Integer.MIN_VALUE;path[0][0] = A[0];for(int i=1;i<len;i++){path[i][i] = A[i];path[0][i] = path[0][i-1] * A[i];if(A[i]>MaxPro){MaxPro = A[i];}if(path[0][i]>MaxPro){MaxPro = path[0][i];}}for(int i=1;i<len;i++){for(int j=i+1;j<len;j++){path[i][j] = path[i][j-1] * A[j];if(path[i][j]>MaxPro){MaxPro = path[i][j];}}}return MaxPro;}}


A time-out error occurs during submission. Because there is a long array in the test case, the above method needs to open up a large array and fill in the array that is time-consuming O (n ^ 2 ).

In fact, after analysis, we can find that a loop can actually solve the problem. Because there are positive and negative numbers in the array, we need to record the maximum and minimum values at a certain position, because the minimum value may change to the maximum value by multiplying the negative value in the next step.

The Code is as follows:

public class Solution {    public int maxProduct(int[] A){        if(A.length < 1){return 0;}int min_temp = A[0];int max_temp = A[0];int result = A[0];;for(int i=1;i<A.length;i++){int a = max_temp * A[i];int b = min_temp * A[i];int c = A[i];max_temp = Math.max(Math.max(a, b), c);min_temp = Math.min(Math.min(a, b), c);result = max_temp>result? max_temp:result;}return result;}}


 


Product of dimensions is greater than maximum integer

I don't know under what circumstances you have this. The literal meaning is "the product size exceeds the maximum integer ."

When an angry bird appears, texture is too large: 2048x2048, maximum supported size 1024x1024, how can this problem be solved?

Parts
Adjust width =? Height =?
You can
You can drag it with the mouse!
Product = "Angry Birds"
Publisher = "Rovio"
Name = "Angry Birds"
Width = 1024
Height = 600
Orientation = 0
Datapath = "data"
ImagePath = "images/pc_build"
FontPath = "fonts/pc_build"
AudioPath = "audio"
LocalizationPath = "localization"
LevelPath = "levels"
ScriptPath = "scripts"
DeviceModel = "windows"
Fullscreen = false
ShowCursor = false

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.