【问题标题】:HackerRank Product DistributionHackerRank 产品分布
【发布时间】:2020-08-08 13:27:15
【问题描述】:

我一直在尝试解决这个挑战,并且有点想出该怎么做。但是经过我所有的尝试,我只能通过测试用例,并且在提交面板中还有一个用例。之后一切都失败了

问题:

一家公司要求简化其产品分配策略,并且给定 n 个产品,每个产品都有一个关联的价值,您需要将这些产品排列成段进行加工。索引为 1、2、3 等的无限段。

但是,有两个限制:

  • 当且仅当 i = 1 或索引为 i-1 的段至少有 m 个产品时,您可以将产品分配给索引为 i 的段。
  • 任何细分都必须不包含产品或至少包含 m 个产品。

段的分数定义为段的索引乘以它包含的产品的值的总和。产品排列的得分是分段得分的总和。您的任务是计算安排的最高分。

例如,考虑 n = 11 个产品和 m = 3。一种最佳分配方式是 -

  1. 将前三个产品分配给细分 1。
  2. 将接下来的三个产品分配给细分 2。
  3. 将接下来的五个产品分配给细分 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。

我的解决方案

现在我的算法解释了我的发现:

  1. 检查数组长度是否== m,如果是,则返回所有元素之和
  2. 如果不是,则将 start = 0 和 end = m 作为指针,它将处理元素的总和直到该部分 start -> end
  3. 对数组进行排序以获得最佳结果
  4. 取batch = 1,将在while循环中递增,并乘以limited products数组的总和
  5. 对于数组中剩余的元素,做同样的操作batch * (sum of elements from start till end)
  6. 将其添加到 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


【解决方案1】:

我们首先对给定的数组进行排序。这是因为我们最终“分割”的数字在数值上越大,它会导致更大的总和,并且它会乘以段的索引,从而最大化输出。

解决这个问题的简单逻辑是找到具有m 产品的最大段数。与示例输入一样,对于输入n=5m=2: 我们最多可以用 2 个产品形成 1 个细分,因为如果我们用 2 个产品分成 2 个细分,我们最终会剩下 1 个产品,我们不能细分它(根据问题)。

为了实现这一点,我们将字符串的长度除以m,如果它不能完全整除,则减去 1。

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
      

      此解决方案不会超过 2.5 秒!

      编码愉快。

      【讨论】:

        【解决方案4】:

        试试这是在php中

        function maxScore($a, $m) {
            $b=$a;
            rsort($b);
            $segments=[];
            $nullsum=false;
            while(count($b) && !$nullsum){
                $seg=[];
                if(count($b)>=$m){
                    while(!(count($seg)>=$m))
                        array_push($seg,array_pop($b));
                    $segments[]=$seg;
                }else{
                    if(count($segments))
                        while(count($b))
                            array_push($segments[count($segments)-1],array_pop($b));
                    else
                        $nullsum=true;
                }
            }
            $sum=0;
            for($j=0;$j<count($segments);$j++){
                $sum+= array_sum($segments[$j])*($j+1);
            }
            return $sum  % 1000000007;
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-01-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多