Skip to content

Instantly share code, notes, and snippets.

@black-dragon74
Created December 2, 2020 21:16
Show Gist options
  • Save black-dragon74/5bd9c8b0842d76279f5749762584627c to your computer and use it in GitHub Desktop.
Save black-dragon74/5bd9c8b0842d76279f5749762584627c to your computer and use it in GitHub Desktop.
Difference Arrays
package com.blackdragon74;
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int M = scan.nextInt();
long maxSum = 0;
long[] arr = new long[N + 1];
while (M-- > 0) {
int a = scan.nextInt();
int b = scan.nextInt();
int k = scan.nextInt();
arr[a - 1] += k;
arr[b] -= k;
}
scan.close();
long sum, max;
sum = max = 0;
for (long a : arr) {
sum += a;
max = Math.max(sum, max);
}
System.out.println(max);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment