-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path67-ArrayManipulation.cs
68 lines (65 loc) · 1.94 KB
/
67-ArrayManipulation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HackerRank
{
internal class ArrayManipulation_Solution
{
//Brut Force Algorithm
public static long arrayManipulation_BrutForce(int n, List<List<int>> queries)
{
List<List<int>> result = new List<List<int>>();
List<int> first = new List<int>();
long max = 0;
for (int i = 0; i < n; i++)
{
first.Add(0);
}
result.Add(first);
for (int i = 0; i < queries.Count; i++)
{
int start = queries[i][0];
int end = queries[i][1];
int value = queries[i][2];
List<int> check = new List<int>(result[i]);
for (int j = start-1; j <= end-1; j++)
{
check[j] += value;
if (check[j] > max)
{ max = check[j];}
}
result.Add(check);
}
return max;
}
public static long arrayManipulation(int n, List<List<int>> queries)
{
uint m = (uint)queries[0].Count;
long[] numList = new long[n + 1];
for (int i = 0; i < queries.Count; i++)
{
uint a =(uint)queries[i][0];
uint b = (uint)queries[i][1];
long k = (long)queries[i][2];
numList[a] += k;
if(b+1 <= n)
{
numList[b+1] -= k;
}
}
long tempMax = 0;
long max = 0;
for(int j = 1; j <= n; j++)
{
tempMax += numList[j];
if(tempMax > max)
{
max = tempMax;
}
}
return max;
}
}
}