Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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/8/sorting/2.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
Python 通过对重叠的间隔求和求最大元素_Python_Sorting_Max_Intervals_Overlapping - Fatal编程技术网

Python 通过对重叠的间隔求和求最大元素

Python 通过对重叠的间隔求和求最大元素,python,sorting,max,intervals,overlapping,Python,Sorting,Max,Intervals,Overlapping,假设我们得到了间隔空间的总大小。假设我们还得到了一个元组数组,该数组给出了区间的开始和结束索引以及一个值。完成所有求和后,我们希望返回最大元素。我该如何有效地解决这个问题 输入格式:n=间隔空间,间隔=元组数组,其中包含开始索引、结束索引和要添加到每个元素的值 Eg: Input: n = 5, intervals = [(1,2,100),(2,5,100),(3,4,100)] Output: 200 so array is initially

假设我们得到了间隔空间的总大小。假设我们还得到了一个元组数组,该数组给出了区间的开始和结束索引以及一个值。完成所有求和后,我们希望返回最大元素。我该如何有效地解决这个问题

输入格式:n=间隔空间,间隔=元组数组,其中包含开始索引、结束索引和要添加到每个元素的值


    Eg: 
    Input: n = 5, intervals = [(1,2,100),(2,5,100),(3,4,100)]
    Output: 200
    
    so array is initially [0,0,0,0,0]
    
    At each iteration the following modifications will be made:
    1) [100,100,0,0,0]
    2) [100,200,100,100,100]
    3) [100,200,200,200,100]
    
    Thus the answer is 200.


到目前为止,我所知道的是拼接阵列并为拼接部分添加值的强力解决方案。我怎样才能做得更好?感谢您的帮助

一种方法是将你的时间间隔分为开始和结束,并根据你是否在该时间间隔内指定加或减多少。一旦根据间隔在数字行上的位置对其进行排序,就可以遍历它,根据输入或离开间隔来添加或减去值。下面是一些执行此操作的代码:

def find_max_val(intervals):
  operations = []
  for i in intervals:
    operations.append([i[0],i[2]])
    operations.append([i[1]+1,-i[2]])
  unique_ops = defaultdict(int)
  for operation in operations:
    unique_ops[operation[0]] += operation[1]

  sorted_keys = sorted(unique_ops.keys())

  print(unique_ops)
  curr_val = unique_ops[sorted_keys[0]]
  max_val = curr_val
  for key in sorted_keys[1:]:
    curr_val += unique_ops[key]
    max_val = max(max_val, curr_val)

  return max_val


intervals = [(1,2,100),(2,5,100),(3,4,100)]

print(find_max_val(intervals))
# Output: 200   

                                                                 

下面是3个间隔的代码

n = int(input())
x = [0]*n
interval = []
for i in range(3):
   s = int(input()) #start
   e = int(input()) #end
   v = int(input()) #value
   #add value
   for i in range (s-1, e):
      x[i] += v
  
print(max(x))

你们可以使用列表理解来做很多工作

n=5
intervals = [(1,2,100),(2,5,100),(3,4,100)]

intlst = [[r[2] if i>=r[0]-1 and i<=r[1]-1 else 0 for i in range(n)] for r in intervals]

lst = [0]*n  #[0,0,0,0,0]
for ls in intlst:
  lst = [lst[i]+ls[i] for i in range(n)]

print(lst)
print(max(lst))

请指定输入的格式。编辑以添加格式间隔是否仅固定为3?可以大于3?如何计算间隔数?谢谢!我想这正是我想要的。运行时看起来是nlog(n),这比我使用的嵌套for循环要好。嘿,我意识到如果加法和减法出现在同一点上,它就不太正确了,所以我添加了一个循环,在其中我合并了变换,这样每个点只能有一个加法或减法。我编辑了我的帖子来反映这一点。
[100, 200, 200, 200, 100]
200