https://gist.github.com/gcrfelix/8cbdba5aed9e2e00ecda
https://www.quora.com/How-do-you-find-maximum-product-of-two-numbers-in-an-array-of-positive-integers-in-O-N-time
// 题目:How do you find maximum product of two numbers in an array of positive integers in O(N) time?
// Constraints:
// First element should be present in a lower index than the second element.
// First element should be strictly lesser than the second element.
public class MaximumProduct {
public int maximumProduct(int[] nums) {
if(nums == null || nums.length < 2) {
return -1;
}
int len = nums.length;
int largest_so_far = nums[len-1];
int max = Integer.MIN_VALUE;
for(int i=len-2; i>=0; i--) {
if(nums[i] < largest_so_far) {
// candidate for left value
max = Math.max(max, nums[i] * largest_so_far);
} else {
// candidate for right value
largest_so_far = nums[i];
}
}
return max;
}
public static void main(String[] args) {
MaximumProduct mp = new MaximumProduct();
int[] nums = new int[]{2,4,5,1,8,7};
System.out.println(mp.maximumProduct(nums));
}
}
https://www.quora.com/How-do-you-find-maximum-product-of-two-numbers-in-an-array-of-positive-integers-in-O-N-time
// 题目:How do you find maximum product of two numbers in an array of positive integers in O(N) time?
// Constraints:
// First element should be present in a lower index than the second element.
// First element should be strictly lesser than the second element.
public class MaximumProduct {
public int maximumProduct(int[] nums) {
if(nums == null || nums.length < 2) {
return -1;
}
int len = nums.length;
int largest_so_far = nums[len-1];
int max = Integer.MIN_VALUE;
for(int i=len-2; i>=0; i--) {
if(nums[i] < largest_so_far) {
// candidate for left value
max = Math.max(max, nums[i] * largest_so_far);
} else {
// candidate for right value
largest_so_far = nums[i];
}
}
return max;
}
public static void main(String[] args) {
MaximumProduct mp = new MaximumProduct();
int[] nums = new int[]{2,4,5,1,8,7};
System.out.println(mp.maximumProduct(nums));
}
}