问题
I have been trying to solve this challenge, and somewhat figured out what to do. But after all my attempts, I am able to pass only the test cases, and one more case in the Submit Panel. All fails after that
PROBLEM:
A company has requested to streamline their product allocation strategy, and given n products, each of which has an associated value, you are required to arrange these products into segments for processing. There are infinite segments indexed as 1, 2, 3 and so on.
However, there are two constraints:
- You can assign a product to a segment with index i if and only if i = 1 or the segment with index i-1 has at least m products.
- Any segment must contain either no products or at least m products.
The score for a segment is defined as the index of the segment multiplied by the sum of values of the products it contains. The score of an arrangement of products is the sum of scores of segments. Your task is to compute the maximum score of an arrangement.
Consider, for example, n = 11 products and m = 3. One optimal way to assign is -
- Assign the first three products to segment 1.
- Assign the next three products to segment 2.
- Assign the next five products to segment 3.
Note that we can not assign 2 products to segment 4 as the second constraint would be violated. The score of the above arrangement is -
1 * (1 + 2 + 3) + 2 * (4 + 5 + 6) + 3 * (7 + 8 + 9 + 10 + 11) = 6 + 30 + 135 = 171.
Since the arrangement score can be very large, print it modulo 10^9 + 7.
Input Format
In the first line, there are two space-separated integers n and m.
In the second line, there are n space-separated integers a0,a1,....,an-1 denoting the values associated with the products.
Constraints
- 1 <= n <= 10^6
- 1 <= m <= n
- 1 <= ai <= 10^9
Output Format
In a single line, print a single integer denoting the maximum score of the arrangement modulo 10^9 + 7.
Sample Input 0
5 2
1 5 4 2 3
Sample Output 0
27
Explanation 0
The array is a = [1,5,4,2,3] and m = 2. It is optimal to put the first and fourth products into the first segment and the remaining products to the second segment. Doing that, we get the arrangement score (1+2) * 1 + (3+4+5) * 2 = 27 which is the greatest score that can be obtained. Finally, the answer is modulo 10^9 + 7 which is 27.
Sample Input 1
4 4
4 1 9 7
Sample Output 1
21
Explanation 1
All the four products must be placed in the first segment. The score in this case will be 1 * (4 + 1 + 9 + 7) = 21.
MY SOLUTION
Now what I have figured out is explained in my algorithm:
- To check if the array length == m, if yes, return the sum of all elements
- If not, take start = 0 and end = m, as a pointer which will take care of the sum of the elements till that part
start -> end
- Sort the array for best results
- Take batch = 1, which will be incremented in a while loop, and multiplied with the sum of the limited products array
- For remaining elements in the array, do same operation
batch * (sum of elements from start till end)
- Add it to the maxSum and return the maxSum
Code
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
Another Solution:
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
After all the testing, the code works fine for all the visible test cases, but doesn't pass any of the hidden test cases on HackerRank
. I need some help on this, I guess there is some kind of misunderstanding happened in understanding the question cos solution seems fine to me
回答1:
We firstly, sort the given array. This is because the numerically greater the number we "segment" in the end, it would result in a greater sum and also it would be multiplied by the index of segment which would in turn maximise the output.
The simple logic to solve the question, is to find the maximum number of segments with m
products. As in the sample input, for the input n=5
, m=2
:
We can form at max 1 segment with 2 products because if we take 2 segments with 2 products, we're left with 1 product in the end, and we cannot segment that (as per the question).
In order to achieve that we divide the length of the string with m
and subtract 1 from it, if it is not completely divisible.
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
回答2:
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)
回答3:
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
This solution won't take more than 2.5 seconds!
Happy coding.
来源:https://stackoverflow.com/questions/61417417/hackerrank-product-distribution