Arrays HackerRank产品分销
我一直在努力解决这个挑战,并在一定程度上想出了该怎么做。但是在我所有的尝试之后,我只通过了测试用例,并且在提交面板中又通过了一个用例。之后一切都失败了 问题:Arrays HackerRank产品分销,arrays,python-3.x,algorithm,functional-programming,Arrays,Python 3.x,Algorithm,Functional Programming,我一直在努力解决这个挑战,并在一定程度上想出了该怎么做。但是在我所有的尝试之后,我只通过了测试用例,并且在提交面板中又通过了一个用例。之后一切都失败了 问题: def maxScore(segment, products): # Write your code here # If the segment == products, then it should return all the sum # We will evaluate as per the products
def maxScore(segment, products):
# Write your code here
# If the segment == products, then it should return all the sum
# We will evaluate as per the products listing requirement and find the sum
'''
Algo for else condition
1. We will maintain a start and end pointer to keep a check till counter equals products
2. We will keep adding the maxSum of the value [i * sum(batch of the element)]
3. Come out of the loop and perform a final operation as above with the remaining elements
4. Add it to sum
5. Return maxSum
'''
batch = 1
maxSum = 0
start = 0
end = products
segment.sort()
while batch != products:
maxSum += (batch * sumElem(segment[start:end]))
batch += 1
start += products
end += products
maxSum += (batch * sumElem(segment[start:len(segment)]))
return maxSum
# function to find the sum of the elements
def sumElem(arr):
total = 0
for item in arr: total += item
return total
一家公司要求简化其产品分配策略,并且给定n个产品,每个产品都有一个关联的值,您需要将这些产品分为多个部分进行处理。有无穷多段索引为1、2、3等等
但是,有两个限制:
- 当且仅当i=1或索引为i-1的段至少有m个产品时,可以将产品分配给索引为i的段
- 任何段必须不包含产品或至少包含m个产品
- 1我们首先对给定数组进行排序。这是因为我们最终“分段”的数字越大,它将导致一个更大的总和,并且它将乘以分段指数,从而使输出最大化
解决这个问题的简单逻辑是找到
产品的最大段数。与示例输入一样,对于输入m
,n=5
: 我们最多可以用2个产品组成1个细分市场,因为如果我们用2个产品组成2个细分市场,我们最终只剩下1个产品,而我们无法将其细分(根据问题) 为了达到这个目的,我们用m=2
除以字符串的长度,如果它不能完全整除,则从中减去1m
def maxScore(a, m): a.sort() print(a) x=len(a) if x%m==0: y=int(x/m) else: y=int(x/m)-1 summ=0 count=1 #print(y) i=0 for _ in range(y): summ=summ+(sum(a[i:i+m])*count) count=count+1 i=i+m print(summ) summ=summ+sum(a[i:])*count print(summ) return summ%1000000007
def最大分数(a,m): #在这里编写代码 a、 排序() l=len(a) j=1 s=0 i=0
而i+(2*m)也许你也可以分享问题链接。我找不到它。是来自正在进行的比赛吗?是的@shivank98,比赛名称是Hack the Interview II-Global,否则我会分享这个链接。不管怎样,这个问题写得很清楚是为了让你们理解。我不确定,但实际上,在这种情况下,你们试图在某处找到答案是一种欺骗。它似乎没有遵循这样的政策。有人提到我找不到链接。我没有作弊,因为我已经用我的代码解决了这个问题。只是我需要一些指导方面的帮助。那是allI,我看不出你在哪里取模。我对Python不太了解,但如果变量范围不够大,那么在末尾取模是不可行的4 4 4 1 9 7
21
def maxScore(segment, products): # Write your code here # If the segment == products, then it should return all the sum # We will evaluate as per the products listing requirement and find the sum ''' Algo for else condition 1. We will maintain a start and end pointer to keep a check till counter equals products 2. We will keep adding the maxSum of the value [i * sum(batch of the element)] 3. Come out of the loop and perform a final operation as above with the remaining elements 4. Add it to sum 5. Return maxSum ''' batch = 1 maxSum = 0 start = 0 end = products segment.sort() if len(segment) == products: maxSum += (batch * sumElem(segment[start:len(segment)])) else: while batch != products: maxSum += (batch * sumElem(segment[start:end])) batch += 1 start += products end += products maxSum += (batch * sumElem(segment[start:len(segment)])) return maxSum # function to find the sum of the elements def sumElem(arr): total = 0 for item in arr: total += item return total
def maxScore(segment, products): # Write your code here # If the segment == products, then it should return all the sum # We will evaluate as per the products listing requirement and find the sum ''' Algo for else condition 1. We will maintain a start and end pointer to keep a check till counter equals products 2. We will keep adding the maxSum of the value [i * sum(batch of the element)] 3. Come out of the loop and perform a final operation as above with the remaining elements 4. Add it to sum 5. Return maxSum ''' batch = 1 maxSum = 0 start = 0 end = products segment.sort() while batch != products: maxSum += (batch * sumElem(segment[start:end])) batch += 1 start += products end += products maxSum += (batch * sumElem(segment[start:len(segment)])) return maxSum # function to find the sum of the elements def sumElem(arr): total = 0 for item in arr: total += item return total
def maxScore(a, m): # Write your code here n=len(a) a.sort() possible_quotient=n//m sets=possible_quotient-1 set_range=sets*m base=1 SUM=0 k=0 for i in range(sets): temp=a[k:k+m] k+=m tsum=sum(temp)*base SUM+=tsum base+=1 i+=m temp2=sum(a[set_range:n])*base SUM+=temp2 return(SUM%1000000007)
def maxScore(a, m): a.sort() print(a) x=len(a) if x%m==0: y=int(x/m) else: y=int(x/m)-1 summ=0 count=1 #print(y) i=0 for _ in range(y): summ=summ+(sum(a[i:i+m])*count) count=count+1 i=i+m print(summ) summ=summ+sum(a[i:])*count print(summ) return summ%1000000007
def maxScore(a, m): # Write your code here a.sort() l = len(a) j = 1 s = 0 i = 0 while i+(2*m) <= l: s += sum(a[i:i+m])*j i += m j += 1 print(s) s += sum(a[i:])*j return s%1000000007