Created
December 2, 2020 21:16
-
-
Save black-dragon74/5bd9c8b0842d76279f5749762584627c to your computer and use it in GitHub Desktop.
Difference Arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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