// => Brute Force Solution: O(m* n)
static long arrayManipulation(int n, int[][] queries)
{
long max = arr[0];
foreach (var item in queries)
{
int a = item[0] - 1;
int b = item[1] - 1;
int k = item[2];
for (int i = a; i <= b; i++)
{
arr[i] = arr[i] + k;
if (max < arr[i])
{
max = arr[i];
}
}
}
return max;
}
// => Efficient Solution:O(m+n)
static long arrayManipulation(int n, int[][] queries)
{
long[] arr = new long[n + 1];
long max = Int64.MinValue; ;
foreach (var item in queries)
{
int a = item[0] - 1;
int b = item[1] - 1;
int k = item[2];
arr[a] = arr[a] + item[2];
arr[b + 1] = arr[b + 1] - item[2];
}
for (int i = 0; i < arr.Length; i++)
{
if (i != 0)
{
arr[i] = arr[i] + arr[i - 1];
}
if (arr[i] > max)
{
max = arr[i];
}
}
return max;
}
1 comment:
Thank you.well explained and your video helped me alot but voice is a bit low.
Post a Comment