第一版有7个时间超限,优化成了第二版:
1:
#!/bin/python3 import math import os import random import re import sys # Complete the arrayManipulation function below. def arrayManipulation(n, queries): # n is lenth m is operatation m = len(queries) arr = [0]*n for i in range(m): start = queries[i][0] end = queries[i][1] for j in range (start -1, end): arr[j] += queries[i][2] return max(arr) if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') nm = input().split() n = int(nm[0]) m = int(nm[1]) queries = [] for _ in range(m): queries.append(list(map(int, input().rstrip().split()))) result = arrayManipulation(n, queries) fptr.write(str(result) + ' ') fptr.close()
2:
#!/bin/python3 import math import os import random import re import sys # Complete the arrayManipulation function below. def arrayManipulation(n, queries): # n is lenth m is operatation m = len(queries) arr = [0]*(n+1) for i in range(m): start = queries[i][0] end = queries[i][1] arr[start - 1] += queries[i][2] if (end <=len(arr)): arr[end] -= queries[i][2] max =x =0 for i in arr: x = x + i if (max < x) : max =x return max if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') nm = input().split() n = int(nm[0]) m = int(nm[1]) queries = [] for _ in range(m): queries.append(list(map(int, input().rstrip().split()))) result = arrayManipulation(n, queries) fptr.write(str(result) + ' ') fptr.close()