Python 具有给定k令牌的最大项目数
数组中有N个元素。我可以选择第一项最多N次,第二项最多N-1次,依此类推 我有K个令牌要使用,需要使用它们,这样我就可以拥有最大数量的项目 arr=[3,4,8],其中数组元素表示第i项所需的令牌Python 具有给定k令牌的最大项目数,python,algorithm,Python,Algorithm,数组中有N个元素。我可以选择第一项最多N次,第二项最多N-1次,依此类推 我有K个令牌要使用,需要使用它们,这样我就可以拥有最大数量的项目 arr=[3,4,8],其中数组元素表示第i项所需的令牌 n = 10 , represents number of tokens I have Output: 3 说明: 代码: 但它只对少数测试用例有效,对一些隐藏的测试用例无效。我不确定我在哪里犯了错误。有人能帮我吗?@learner您的逻辑似乎不正常。 请尝试以下输入:arr=[
n = 10 , represents number of tokens I have
Output:
3
说明:
代码:
但它只对少数测试用例有效,对一些隐藏的测试用例无效。我不确定我在哪里犯了错误。有人能帮我吗?@learner您的逻辑似乎不正常。 请尝试以下输入:arr=[10,8,6,4,2],n=30。
根据您的描述,答案应该是6次骑乘,但您的代码将产生3次,您的贪婪方法将在以下测试用例中失败: [8,2,1,1]10 您的代码将返回2,但最大值为6 我将使用一个元组堆,即heap[cost_of_ride,max_no_rides] 请参阅下面的代码:
from heapq import *
def process(arr,n):
count = 0
heap = []
for i in range(len(arr)):
heappush(heap,(arr[i],-(len(arr)-i))) # Constructing min-heap with second index as negative of maximum number of rides
while(n>0 and heap):
cost,no_of_rides = heappop(heap)
no_of_rides = -1 * no_of_rides # Changing maximum no_of_rides from negative to positive
div = n//cost
# If the amount of money is not sufficient to calculate the last number of rides user could take
if(div<no_of_rides):
count += div
break
# Else decrement the number of tokens by minimum cost * maximum no_of_rides
else:
count += no_of_rides
n -= no_of_rides*cost
return count;
解决方案的时间复杂度为:Olenarr*lglenarr或ON*lgN。使用修改后的表单快速选择,根据成本*最大时间的乘积之和选择下一个枢轴,但仍仅基于成本进行排序。这是^2上的最坏情况,但应在上。请尝试: def processarr,n,res=[]: l=lenarr 对于范围为LENARR+1的j: r=[arr[0]]*j 如果sumr==n或sumr>printmaxprocess[3,4,8],则为10 3. >>>printmaxprocess[8,2,1,1],10 6. >>>printmaxprocess[10,8,6,4,2],30 6.
如果这是来自HackerRank的,那么失败的测试用例将具有较大的输入值。例如,arr将有许多值,n将是一个大数字。因此,在大型输入上,当代码花费大量时间时,测试用例会因为超时而失败。因此,您需要对其进行优化。请共享问题的链接。@BalajiAmbresh,我上周做了这件事,没有任何链接now@MoosaSaadat,它不是hackerrank,一些不同的应用程序,别忘了贪婪的方法尽可能多地骑最便宜的车应该是可行的。为了加快速度,将剩余代币除以最便宜的车费:骑乘=代币左/最低价格。如果骑乘次数大于允许的次数,则将其设置为允许的次数。我正试图理解这一点,为什么我们需要在堆中添加负片,如-lenarr-I?@learner,以便以最低成本获得最大的骑乘次数。。。虽然如果你不添加负号,代码也会以类似的方式工作…所以如果你想编辑代码…你可以自由编辑。谢谢,你能告诉我什么时候需要使用负号吗?情况会怎样。还想知道NLgN的解释,当我们有while循环时,LgN是如何运行的,直到堆中有包含数组元素的元素。@learner因为我们使用的是python中内置的min HEAPIN,所以如果您希望元素采用max heap格式,然后你应该加上那个元素的负数。我加上了解释——如果需要更详细的说明,请告诉我
def process(arr,n):
count = 0
sum = 0
size = len(arr)+1
for i in range(0, len(arr), 1):
size1 = size-1
size -= 1
while((sum+arr[i] <= n) and (size1 > 0)):
size1 = size1 -1
sum = sum + arr[i]
count += 1
return count;
from heapq import *
def process(arr,n):
count = 0
heap = []
for i in range(len(arr)):
heappush(heap,(arr[i],-(len(arr)-i))) # Constructing min-heap with second index as negative of maximum number of rides
while(n>0 and heap):
cost,no_of_rides = heappop(heap)
no_of_rides = -1 * no_of_rides # Changing maximum no_of_rides from negative to positive
div = n//cost
# If the amount of money is not sufficient to calculate the last number of rides user could take
if(div<no_of_rides):
count += div
break
# Else decrement the number of tokens by minimum cost * maximum no_of_rides
else:
count += no_of_rides
n -= no_of_rides*cost
return count;