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