|
| 1 | +//Link: https://www.hackerrank.com/challenges/crush/problem |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.math.*; |
| 5 | +import java.security.*; |
| 6 | +import java.text.*; |
| 7 | +import java.util.*; |
| 8 | +import java.util.concurrent.*; |
| 9 | +import java.util.regex.*; |
| 10 | + |
| 11 | +public class Solution { |
| 12 | + |
| 13 | + // Complete the arrayManipulation function below. |
| 14 | + static long arrayManipulation(int n, int[][] queries, int m) { |
| 15 | + |
| 16 | + long[] a=new long[n]; |
| 17 | + for(int i=0;i<n;i++) |
| 18 | + { |
| 19 | + a[i]=0; |
| 20 | + } |
| 21 | + |
| 22 | + for( int j=0; j<m; j++) |
| 23 | + { |
| 24 | + int lower=queries[j][0]; |
| 25 | + int upper=queries[j][1]; |
| 26 | + int sum= queries[j][2]; |
| 27 | + a[lower-1]+=sum; |
| 28 | + if(upper<n) a[upper]-=sum; |
| 29 | + } |
| 30 | + |
| 31 | + long max=a[0]; |
| 32 | + long temp=0; |
| 33 | + for(int k=0; k<n; k++) |
| 34 | + { |
| 35 | + temp += a[k]; |
| 36 | + if(temp> max) |
| 37 | + max=temp; |
| 38 | + } |
| 39 | + |
| 40 | + return max; |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + private static final Scanner scanner = new Scanner(System.in); |
| 45 | + |
| 46 | + public static void main(String[] args) throws IOException { |
| 47 | + BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); |
| 48 | + |
| 49 | + String[] nm = scanner.nextLine().split(" "); |
| 50 | + |
| 51 | + int n = Integer.parseInt(nm[0]); |
| 52 | + |
| 53 | + int m = Integer.parseInt(nm[1]); |
| 54 | + |
| 55 | + int[][] queries = new int[m][3]; |
| 56 | + |
| 57 | + for (int i = 0; i < m; i++) { |
| 58 | + String[] queriesRowItems = scanner.nextLine().split(" "); |
| 59 | + scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); |
| 60 | + |
| 61 | + for (int j = 0; j < 3; j++) { |
| 62 | + int queriesItem = Integer.parseInt(queriesRowItems[j]); |
| 63 | + queries[i][j] = queriesItem; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + long result = arrayManipulation(n, queries, m); |
| 68 | + |
| 69 | + bufferedWriter.write(String.valueOf(result)); |
| 70 | + bufferedWriter.newLine(); |
| 71 | + |
| 72 | + bufferedWriter.close(); |
| 73 | + |
| 74 | + scanner.close(); |
| 75 | + } |
| 76 | +} |
0 commit comments