classSolution{publiclongminimumPerimeter(long neededApples){long i =1, sum =12;while(sum < neededApples){
i++;
sum +=(i + i)*4;// (i,i)这个点
sum += i *4;// (i,0)这个点for(int k =1; k < i; k++){
sum +=(i + k)*2*4;// 中间的(i,k)的点}}return i *8;}}
java代码——优化思路
classSolution{publiclongminimumPerimeter(long neededApples){long i =1, sum =12;while(sum < neededApples){
i++;
sum +=12* i * i;}return i *8;}}