大佬教程收集整理的这篇文章主要介绍了Leetcode 算法计算时间太长,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
https://leetcode.com/problems/pizza-with-3n-slices/
我了解编程的基础知识,最近决定研究 leetcode,所以我很想知道我对这些难题的立场。警告的话,它看起来不漂亮。该代码有效,但计算添加的切片越多,所需的时间就越多。我的解决方案是可挽救的还是死路一条,我应该从不同的思路开始?
我的算法的本质与此 quick diagram 一起使用,请记住我为此使用了 C。 第一个循环遍历数组的每个单独元素,在那里它调用一个函数来删除 3 个切片,将它们替换为 -1 作为一种标志类型。此函数是递归的,使用数组的旧副本从下一个元素中删除切片,直到它遍历每个元素。它将退出第二个循环并从第一个循环的下一个元素重新开始。 我想我可以把这一切都做成一个递归循环,但我不确定我是否可以编辑 leetcode 的参数来完成他们给我的功能。
int copyArray(int * from,int * to,int size){
for(int i = 0; i < size; i++)
to[i] = from[i];
return 1;
}
int findFirstElement(int *slices,int size){
for(int i = 0; i< size; i++)
if (slices[i] != -1)
return i;
}
int findLastElement(int *slices,int size){
for(int i = size-1; i>=0; i--)
if (slices[i] != -1)
return i;
}
int getMax(int *a,int b){
return (*a>b)? *a: b;
}
int removeSlices(int *slices,int pos,int slicesSize,int *max,int size){ //int size is
//the size of the pizza which will never change unlike
// slicesSize which is decremented by 3 in each level we enter
int *tempmax;
int l = *max;
tempmax = &l;
int newSlices[size];
int k = 0;
int m = 0;
int j = 0;
int *maxmax;
int p = 0;
maxmax = &p;
int newSlicesSize = slicesSize;
copyArray(slices,newSlices,size);
int firstElement = findFirstElement(newSlices,size);
int lastElement = findLastElement(newSlices,size);
*max += newSlices[pos];
if(pos == firstElement){ // represent the array as a circle
newSlices[lastElement] = -1;
newSlices[pos] = -1;
newSlices[pos+1] = -1;
}
else if(pos == lastElement)
{
newSlices[pos-1] = -1;
newSlices[pos] = -1;
newSlices[firstElement] = -1;
}
else {
newSlices[pos-1] = -1;
newSlices[pos] = -1;
newSlices[pos+1] = -1;
}
// for(int i = 0; i<size; i++)
// printf("%d,",newSlices[i]);
// printf("\n");
newSlicesSize -= 3;
if(newSlicesSize == 0)
return *max;
while(k<newSlicesSize){ //the second loop
m = findFirstElement(newSlices,size);
*tempmax = *max; // yet another max var (sorry),before we enter a new level we have to keep hold of the max so we can return to it once it has finished
j = removeSlices(newSlices,m+k,newSlicesSize,tempmax,size);
*maxmax =getMax(maxmax,j); // maxmax holds the max of all decisions/possibilites for only the indivIDual elements of the first loop,not the final max of all elements of the first loop
k++;
}
return *maxmax;
}
int maxSizeSlices(int* slices,int slicesSize){
int *max;
int k = 0;
max = &k;
int j = 0;
int *maxmax;
int p = 0;
maxmax = &p;
int i = 0;
while(i < slicesSize){
*max =0;
j = removeSlices(slices,i,slicesSize,max,slicesSize);
*maxmax =getMax(maxmax,j);
i++;
}
return *maxmax;
}
int main()
{
// int slices[] = {6,3,1,2,6,4,10,5,7,8,8}; // gave up trying to compute this after 20min
int slices[] = {1,6}; // computes quickly
int size = 6;
int max = maxSizeSlices(slices,size);
printf("final max: %d",max);
return 0;
}'''
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的Leetcode 算法计算时间太长全部内容,希望文章能够帮你解决Leetcode 算法计算时间太长所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。