-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
132 lines (88 loc) · 3.5 KB
/
Copy pathSolution.java
File metadata and controls
132 lines (88 loc) · 3.5 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package Test;
import java.io.IOException;
import java.util.Scanner;
public class Solution {
/*
* Complete the arrayManipulation function below.
*/
static long arrayManipulation2(int n, int[][] queries) {
long[] arr = new long[n];
int rowLength = queries.length;
for(int j =0;j<rowLength;j++){
int a = queries[j][0];
int b = queries[j][1];
int k = queries[j][2];
for(int i=a-1;i<b; i++){
arr[i] = arr[i] + k;
}
}
long max = (long) arr[0];
for(int i=0;i<n;i++){
if(max < arr[i]){
max = arr[i];
}
}
return max;
}
static long arrayManipulation3(int n, int[][] queries) {
long[] arr = new long[n];
int rowLength = queries.length;
long max = 0L;
for(int j =0;j<rowLength;j++){
int a = queries[j][0];
int b = queries[j][1];
int k = queries[j][2];
for(int i=a-1;i<=b-1;i++) {
arr[i] = arr[i] +k;
if(max < arr[i]) {
max = arr[i];
}
}
}
return max;
}
static long arrayManipulation(int n, int[][] queries) {
int rowLength = queries.length;
long[] differenceArray = new long[n+1];
for(int j =0;j<rowLength;j++){
int a = queries[j][0];
int b = queries[j][1];
int k = queries[j][2];
differenceArray[a] += k;
if (b+1<=n)
differenceArray [b+1] -= k;
}
long max = 0L;
long addedDifference = 0L;
for (int i=1; i<=n; i++)
{
addedDifference = addedDifference + differenceArray[i];
if (max < addedDifference)
max = addedDifference;
}
return max;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
String Str = new String("Welcome to Tutorialspoint.com");
System.out.print("Return Value :" );
System.out.println(Str.matches("(.*)Tutorials(.*)"));
System.out.print("Return Value :" );
System.out.println(Str.matches("Tutorials"));
System.out.print("Return Value :" );
System.out.println(Str.matches("Welcome(.*)"));
String[] nm = scanner.nextLine().split(" ");
int n = Integer.parseInt(nm[0].trim());
int m = Integer.parseInt(nm[1].trim());
int[][] queries = new int[m][3];
for (int queriesRowItr = 0; queriesRowItr < m; queriesRowItr++) {
String[] queriesRowItems = scanner.nextLine().split(" ");
for (int queriesColumnItr = 0; queriesColumnItr < 3; queriesColumnItr++) {
int queriesItem = Integer.parseInt(queriesRowItems[queriesColumnItr].trim());
queries[queriesRowItr][queriesColumnItr] = queriesItem;
}
}
long result = arrayManipulation(n, queries);
System.out.println(result);
}
}