Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 点燃蜡烛的最长天数_Arrays_Sorting_Puzzle - Fatal编程技术网

Arrays 点燃蜡烛的最长天数

Arrays 点燃蜡烛的最长天数,arrays,sorting,puzzle,Arrays,Sorting,Puzzle,你被赋予n支蜡烛的高度。第一天你点燃了一支蜡烛第二天你需要点燃两支蜡烛第三天你需要点燃三支蜡烛直到可能。点燃蜡烛后,蜡烛的高度每天减少1。你也可以熄灭任何你想要的蜡烛,但只能在一天结束时熄灭。所以你需要告诉最大天数,你可以继续点蜡烛 Example : there are three candles of heights {2 ,2 ,2 } Answer : 3 1.You light first candle on day one. heights -> {1,2,2} 2

你被赋予n支蜡烛的高度。第一天你点燃了一支蜡烛第二天你需要点燃两支蜡烛第三天你需要点燃三支蜡烛直到可能。点燃蜡烛后,蜡烛的高度每天减少1。你也可以熄灭任何你想要的蜡烛,但只能在一天结束时熄灭。所以你需要告诉最大天数,你可以继续点蜡烛

Example : there are three candles of heights {2 ,2 ,2 }  
Answer : 3  
1.You light first candle on day one. heights -> {1,2,2}  
2.You light second and third and extinguish first one . heights ->{1, 1,1}  
3.You light all the candles. heights -{0,0,0}  
我在一个博客上找到了解决方案:

对数组进行排序。从每天的最大值开始,我们添加一个元素,然后减去所需的蜡烛数。所以对于2,2,2

Day 1: 2-1 = 1 Day 2: 1+2-2 = 1 Day 3: 1+2-3 = 0  
Answer is: 3  
But for 1,1,1 this algorithm will fail as answer should be 2.  

请帮我解决这个问题

头两天,它点燃了1+2=3单位的蜡烛 之后3个单位/天

So if you want to lit a candle for 5 days , total unit of candle must be = 3 unit (for two days) + 
(3 * 3) (for rest of three days)
= 12 unit,
so you either need 3 candles of 4 unit , or 4 candles of 3 unit or 6 candles of 2 unit or 12 candles of 1 unit.

Hence, if h = number of height and n = number of candles(must be greater than 2)
then your equation will be number of days = (((h * n) - 3)/3) + 2
= (h * n)/3 + 1
in(((h*n)-3)/3)+2,-3在前两天(3个单位)减少,因为它作为常数+2(天)添加到方程式中

只有蜡烛的数量大于或等于3时,这个等式才成立。因为如果只有一根无限高的蜡烛,你就不能在第二天点燃它两次。
因此,如果n在前两天,它点燃了1+2=3单位的蜡烛 之后3个单位/天

So if you want to lit a candle for 5 days , total unit of candle must be = 3 unit (for two days) + 
(3 * 3) (for rest of three days)
= 12 unit,
so you either need 3 candles of 4 unit , or 4 candles of 3 unit or 6 candles of 2 unit or 12 candles of 1 unit.

Hence, if h = number of height and n = number of candles(must be greater than 2)
then your equation will be number of days = (((h * n) - 3)/3) + 2
= (h * n)/3 + 1
in(((h*n)-3)/3)+2,-3在前两天(3个单位)减少,因为它作为常数+2(天)添加到方程式中

只有蜡烛的数量大于或等于3时,这个等式才成立。因为如果只有一根无限高的蜡烛,你就不能在第二天点燃它两次。
如果我今天向我的学生提出这个问题,这就是我提出的解决方案。它以O(n)为单位运行(实际上,它是以O(数量)时间b/c为单位运行高度在这里是一个非因素。你燃烧的天数永远不会超过你有蜡烛的天数b/c在数量+1天的情况下,即使你有足够的蜡烛剩余,你也不会有足够的实际蜡烛燃烧。)

public static int getAmtOfBurnDays(int height,int amount){
int TotalDaysThatWhere Burned=0;
int totalPiecesLeft=高度*数量;
while(总燃烧天数<数量){
如果(TotalPieclesLeft<0)中断;
燃烧的总天数++;
TotalPieclesLeft-=燃烧的总天数;
}
返回燃烧的总天数;
}

今天我向学生们提出了这个问题,这就是我提出的解决方案。它以O(n)为单位运行(实际上,它是以O(数量)时间b/c为单位运行高度在这里是一个非因素。你燃烧的天数永远不会超过你有蜡烛的天数b/c在数量+1天的情况下,即使你有足够的蜡烛剩余,你也不会有足够的实际蜡烛燃烧。)

public static int getAmtOfBurnDays(int height,int amount){
int TotalDaysThatWhere Burned=0;
int totalPiecesLeft=高度*数量;
while(总燃烧天数<数量){
如果(TotalPieclesLeft<0)中断;
燃烧的总天数++;
TotalPieclesLeft-=燃烧的总天数;
}
返回燃烧的总天数;
}

可以通过每天排序并获取最大元素来完成,但时间复杂度为O(N^2 LogN)。我想进一步优化它。即使使用像heap这样的自排序结构,也会得到
O(N^2*logN)
复杂性。这看起来不像是一个数学解决方案,更像是这个问题需要一个贪婪的算法。N的上限是多少?是的,你是对的。没有给出N的上界。我只是想用贪婪算法进一步优化它,但这不会每次都奏效。我投票决定把这个问题作为离题题来结束,因为它与编程无关。也许可以尝试每天排序并获取最大元素,但时间复杂度为O(N^2 LogN)。我想进一步优化它。即使使用像heap这样的自排序结构,也会得到
O(N^2*logN)
复杂性。这看起来不像是一个数学解决方案,更像是这个问题需要一个贪婪的算法。N的上限是多少?是的,你是对的。没有给出N的上界。我只是想用贪婪算法进一步优化它,但这不会每次都奏效。我投票决定把这个问题作为离题题来结束,因为它与编程无关。也许可以尝试将height参数作为整数。这不是一个输入数组吗?很好!我给出的答案不符合问题要求。您将高度参数视为整数。这不是一个输入数组吗?很好!我给出的答案不符合问题要求。
public static int getAmtOfBurnDays(int height, int amount){
    int totalDaysThatWhereBurned = 0;

    int totalPiecesLeft = height * amount;

    while (totalDaysThatWhereBurned < amount){

        if(totalPiecesLeft < 0) break;

        totalDaysThatWhereBurned++;

        totalPiecesLeft -= totalDaysThatWhereBurned;
    }

    return totalDaysThatWhereBurned;
}