Skip to content

Instantly share code, notes, and snippets.

@mbuff24
Created December 30, 2015 15:33

Revisions

  1. mbuff24 created this gist Dec 30, 2015.
    64 changes: 64 additions & 0 deletions Solution.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    import java.io.*;
    import java.util.*;

    public class Solution {

    static class Operation {
    public int a;
    public int b;
    public int k;

    public Operation(int a, int b, int k) {
    this.a = a;
    this.b = b;
    this.k = k;
    }

    public String toString() {
    return "[" + a + ", " + b + ", " + k + "]";
    }
    }

    public static long crush(int n, int m, Operation[] ops) {
    long[] crushState = new long[n+1];
    for(int i=0; i < crushState.length; i++) {
    crushState[i] = 0;
    }

    for(int i=0; i < m; i++) {
    crushState[ops[i].a-1] += ops[i].k;
    crushState[ops[i].b] -= ops[i].k;
    }

    long max = crushState[0];
    long sum = max;
    for(int i=1; i < n; i++) {
    sum += crushState[i];
    if(sum > max) {
    max = sum;
    }
    }

    return max;
    }

    public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    /* https://www.hackerrank.com/contests/w4/challenges/crush */
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int m = sc.nextInt();
    Operation[] ops = new Operation[m];

    for(int i=0; i < m; i++) {
    int a = sc.nextInt();
    int b = sc.nextInt();
    int k = sc.nextInt();
    ops[i] = new Operation(a, b, k);
    }

    long max = crush(n, m, ops);
    System.out.println(max);
    }

    }