【发布时间】:2020-08-08 13:27:15
【问题描述】:
我一直在尝试解决这个挑战,并且有点想出该怎么做。但是经过我所有的尝试,我只能通过测试用例,并且在提交面板中还有一个用例。之后一切都失败了
问题:
一家公司要求简化其产品分配策略,并且给定 n 个产品,每个产品都有一个关联的价值,您需要将这些产品排列成段进行加工。索引为 1、2、3 等的无限段。
但是,有两个限制:
- 当且仅当 i = 1 或索引为 i-1 的段至少有 m 个产品时,您可以将产品分配给索引为 i 的段。
- 任何细分都必须不包含产品或至少包含 m 个产品。
段的分数定义为段的索引乘以它包含的产品的值的总和。产品排列的得分是分段得分的总和。您的任务是计算安排的最高分。
例如,考虑 n = 11 个产品和 m = 3。一种最佳分配方式是 -
- 将前三个产品分配给细分 1。
- 将接下来的三个产品分配给细分 2。
- 将接下来的五个产品分配给细分 3。
请注意,我们不能将 2 个产品分配给细分 4,因为会违反第二个约束。上述安排的得分为-
1 * (1 + 2 + 3) + 2 * (4 + 5 + 6) + 3 * (7 + 8 + 9 + 10 + 11) = 6 + 30 + 135 = 171。
由于排列分数可能很大,以 10^9 + 7 为模打印。
输入格式
在第一行,有两个以空格分隔的整数 n 和 m。
在第二行中,有 n 个以空格分隔的整数 a0,a1,....,an-1 表示与产品相关的值。
约束
- 1
- 1
- 1
输出格式
在一行中,打印一个整数,表示排列的最大分数模 10^9 + 7。
示例输入 0
5 2
1 5 4 2 3
样本输出 0
27
解释0
数组是 a = [1,5,4,2,3] 和 m = 2。最好将第一个和第四个产品放入第一段,将其余产品放入第二段。这样做,我们得到排列分数 (1+2) * 1 + (3+4+5) * 2 = 27,这是可以获得的最大分数。最后,答案是模 10^9 + 7 即 27。
示例输入 1
4 4
4 1 9 7
样本输出 1
21
说明1
所有四种产品都必须放在第一段。在这种情况下,分数将为 1 * (4 + 1 + 9 + 7) = 21。
我的解决方案
现在我的算法解释了我的发现:
- 检查数组长度是否== m,如果是,则返回所有元素之和
- 如果不是,则将 start = 0 和 end = m 作为指针,它将处理元素的总和直到该部分
start -> end- 对数组进行排序以获得最佳结果
- 取batch = 1,将在while循环中递增,并乘以limited products数组的总和
- 对于数组中剩余的元素,做同样的操作
batch * (sum of elements from start till end)- 将其添加到 maxSum 并返回 maxSum
代码
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
在所有测试之后,the code works fine for all the visible test cases, but doesn't pass any of the hidden test cases on HackerRank。我需要一些帮助,我想在理解问题时发生了某种误解,因为我的解决方案似乎很好
【问题讨论】:
-
也许你也可以分享问题链接。我找不到它。是来自正在进行的比赛吗?
-
是的@shivank98,比赛名称是Hack the Interview II - Global,否则我会分享链接。无论如何,问题都清楚地写出来供您理解
-
我不确定,但实际上,在这种情况下,你试图在某个地方找到答案是一种欺骗。它可能似乎不遵循 SO 政策。虽然在某个地方提到了它,但我找不到链接。
-
我没有作弊,因为我已经通过我的代码解决了这个问题。只是在指导方面我需要一些帮助。就是这样
-
我看不到你在哪里取模。我不太了解 Python,但是如果变量范围不够大,只在最后取模是行不通的
标签: arrays python-3.x algorithm functional-programming