Maximum Product Subarray,maximumsubarray

來源:互聯網
上載者:User

Maximum Product Subarray,maximumsubarray

Leetcode 更新題目啦!!!

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.

首先想到就是動態規劃利用path[i][j] 記錄i到j 的乘積,計算過程中更新最大值代碼如下:

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;}}


提交發現出現逾時錯誤,因為測試案例中有一個很長的數組,上面方法需要開闢很大的數組並填寫數組比較耗時O(n^2)。

其實分析一下可以發現,一次迴圈其實就可以解決問題,因為數組中出現正數負數,所以我們需要記錄到某個位置時的最大值與最小值,因為最小值可能在下一步乘以負數就變成最大值啦。

代碼如下:

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怎解決

不知道你這個是在什麼情況下出現的。字面意思是“產品的尺寸超過最大整數。”
 
開啟憤怒的小鳥出現texture is too large:2048x2048,maximum supported size 1024x1024怎解決?


調整width = ?height = ?
就可以
用滑鼠直接拖也可以啊!
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
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.