Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays HackerRank产品分销_Arrays_Python 3.x_Algorithm_Functional Programming - Fatal编程技术网

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个产品
段的分数定义为段的指数乘以其包含的产品的值之和。产品排列的得分是各部分得分的总和。您的任务是计算安排的最大分数

例如,考虑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我们首先对给定数组进行排序。这是因为我们最终“分段”的数字越大,它将导致一个更大的总和,并且它将乘以分段指数,从而使输出最大化

      解决这个问题的简单逻辑是找到
      m
      产品的最大段数。与示例输入一样,对于输入
      n=5
      m=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
      
      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