We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
You're using N^2 TC for this while this can be done in O(n) using prefix sum method.
public static long arrayManipulation(int n, List<List<Integer>> queries) {
long arr[] = new long[n];
for (List<Integer> query : queries) {
arr[query.get(0)-1] += query.get(2);
if (query.get(1)<n) {
arr[query.get(1)] -= query.get(2);
}
}
long res =Long.MIN_VALUE;
long sum = 0;
for (long x : arr) {
sum+=x;
res=Math.max(res, sum);
}
return res;
}
Array Manipulation
You are viewing a single comment's thread. Return to all comments →
Javascript. does anyone know how I would make this faster??
Lets investigate using LLM
Nope...I gave up....Hackerrank does not explain why it fails even though the run time is less than 10s
You're using N^2 TC for this while this can be done in O(n) using prefix sum method.
you need to initialize n + 1 length of array instead, otherwise it will throw index out of bound exception
nope. i am already handling that in th if condition. no need to create that exyra index array.