c - Leetcode 算法计算时间过长
问题描述
https://leetcode.com/problems/pizza-with-3n-slices/
我了解编程的基础知识,最近决定研究 leetcode,所以我很想知道我在这些难题上的立场。警告的话,它看起来并不漂亮。该代码有效,但计算您添加的更多切片需要成倍增加的时间。我的解决方案是可以挽救的还是死路一条,我应该从不同的思路开始?
我的算法的本质适用于这个快速图表,请记住我为此使用 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,2,4,3,10,4,1,4,6,5,5,3,4,7,6,5,8,7,3,8,8,1,7,1,7,8}; // gave up trying to compute this after 20min
int slices[] = {1,2,3,4,5,6}; // computes quickly
int size = 6;
int max = maxSizeSlices(slices, size);
printf("final max: %d",max);
return 0;
}'''
解决方案
推荐阅读
- json - 试图在反应中显示嵌套对象
- protractor - 失败:无法使用预期条件读取未定义的属性“绑定”
- ios - 如何管理数组索引?
- java - Qt调用具有多个参数的java方法
- java - java.lang.ClassCastException: org.apache.catalina.core.DefaultInstanceManager 不能转换为 org.apache.tomcat.InstanceManager
- javascript - 在javascript中操作多维数组
- r - 将区间与 R 中另一个表中的值匹配
- javascript - 如何在 koa-http-request 中发出 POST 请求
- vmware-clarity - 用于包装 clr-datagrid 元素的清晰容器元素
- flutter - 在 GraphQL 库中为 dart 添加多个变量 [flutter]