Advertisement
sweet1cris

Untitled

Jan 9th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. // LeetCode version:
  2. public class Solution {
  3.     /**
  4.      * @param nums: an array of integers
  5.      * @return: an integer
  6.      */
  7.     public int maxProduct(List<Integer> nums) {
  8.         int[] max = new int[nums.size()];
  9.         int[] min = new int[nums.size()];
  10.        
  11.         min[0] = max[0] = nums.get(0);
  12.         int result = nums.get(0);
  13.         for (int i = 1; i < nums.size(); i++) {
  14.             min[i] = max[i] = nums.get(i);
  15.             if (nums.get(i) > 0) {
  16.                 max[i] = Math.max(max[i], max[i - 1] * nums.get(i));
  17.                 min[i] = Math.min(min[i], min[i - 1] * nums.get(i));
  18.             } else if (nums.get(i) < 0) {
  19.                 max[i] = Math.max(max[i], min[i - 1] * nums.get(i));
  20.                 min[i] = Math.min(min[i], max[i - 1] * nums.get(i));
  21.             }
  22.            
  23.             result = Math.max(result, max[i]);
  24.         }
  25.        
  26.         return result;
  27.     }
  28. }
  29.  
  30. // LintCode Version 1:
  31. public class Solution {
  32.     /**
  33.      * @param nums: an array of integers
  34.      * @return: an integer
  35.      */
  36.     public int maxProduct(int[] nums) {
  37.         int[] max = new int[nums.length];
  38.         int[] min = new int[nums.length];
  39.        
  40.         min[0] = max[0] = nums[0];
  41.         int result = nums[0];
  42.         for (int i = 1; i < nums.length; i++) {
  43.             min[i] = max[i] = nums[i];
  44.             if (nums[i] > 0) {
  45.                 max[i] = Math.max(max[i], max[i - 1] * nums[i]);
  46.                 min[i] = Math.min(min[i], min[i - 1] * nums[i]);
  47.             } else if (nums[i] < 0) {
  48.                 max[i] = Math.max(max[i], min[i - 1] * nums[i]);
  49.                 min[i] = Math.min(min[i], max[i - 1] * nums[i]);
  50.             }
  51.            
  52.             result = Math.max(result, max[i]);
  53.         }
  54.        
  55.         return result;
  56.     }
  57. }
  58.  
  59. //LintCode version2: O(1) Space Complexity
  60. public class Solution {
  61.     /**
  62.      * @param nums: an array of integers
  63.      * @return: an integer
  64.      */
  65.     public int maxProduct(int[] nums) {
  66.         // write your code here
  67.         if (nums == null || nums.length == 0) {
  68.             return 0;
  69.         }
  70.         int minPre = nums[0], maxPre = nums[0];
  71.         int max = nums[0], min = nums[0];
  72.         int res = nums[0];
  73.         for (int i = 1; i < nums.length; i ++) {
  74.             max = Math.max(nums[i], Math.max(maxPre * nums[i], minPre * nums[i]));
  75.             min = Math.min(nums[i], Math.min(maxPre * nums[i], minPre * nums[i]));
  76.             res = Math.max(res, max);
  77.             maxPre = max;
  78.             minPre = min;
  79.         }
  80.         return res;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement