0
我正在尝试解决问题。我们将在O(n)中找到一个数组的maxProduct,因此它没有双重循环,因为它将是O(n2) 您将在我的代码中看到,除第一个元素和最后的元素。我怎样才能使用我的代码的逻辑来扩充我的数组的第一个和最后一个元素?数组中的元素相乘
这里是我的代码:
public class Maxprod {
public static void main(String [] args){
Maxprod myclass = new Maxprod();
myclass.maxProduct();
}
public void maxProduct(){
int [] myarr = {4, -5, -7, 5};
int max = 0, p=0;
int q = 0;
for(int i=0; i <myarr.length-1; i++){
p = myarr[i]*myarr[i+1]; // 4 * 5 is missing here
if (p > max){
max = p;
}
}
System.out.println(max);
}
}