Skip to content

Instantly share code, notes, and snippets.

@mbuff24
Created December 30, 2015 15:33
Show Gist options
  • Save mbuff24/5d24ffcf06c7ba7f82ae to your computer and use it in GitHub Desktop.
Save mbuff24/5d24ffcf06c7ba7f82ae to your computer and use it in GitHub Desktop.
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);
}
}
@mestal
Copy link

mestal commented Feb 7, 2017

meetshah51245 : He/She stores only changes. He/She doesnt calculate one by one for the range. So after b that change will not be affected. The next loop he progress with sum. Very good. Thanks.

@uvgoyal
Copy link

uvgoyal commented Jun 21, 2017

I tried writing the solution and I wrote the below code:

import java.util.Scanner;

public class AlgorithmicCrush {

public static void main(String[] args) {
	/*
	 * Enter your code here. Read input from STDIN. Print output to STDOUT.
	 * Your class should be named Solution.
	 */
	Scanner scanner = new Scanner(System.in);

// System.out.println("Input the size of the list:");
int listSize = scanner.nextInt();

// System.out.println("Input the number of the operations to be performed:");
int numberOfOperations = scanner.nextInt();

	int i = 0, j = 0;
	long c[] = new long[listSize];
	int a[] = new int[numberOfOperations];
	int b[] = new int[numberOfOperations];
	long k[] = new long[numberOfOperations];

// System.out.println("Enter the list of the numbers:");
for (i = 0; i < numberOfOperations; i++) {
a[i] = scanner.nextInt();
b[i] = scanner.nextInt();
k[i] = scanner.nextLong();
}

	for (i = 0; i < numberOfOperations; i++) {
		for (j = a[i] - 1; j <= b[i] - 1; j++) {
			if (a[i] <= listSize && b[i] <= listSize) {
				c[j] = c[j] + k[i];
			}
		}
	}

	long temp = c[0];
	for (i = 1; i < c.length; i++) {
		if (c[i] > temp) {
			temp = c[i];
		}
	}

// System.out.println("Final Output is:" + temp);
System.out.println(temp);

}

}

Many of the test cases were passed but the remaining ones got timed out, can anyone please help with the issue in this code?

Note: I am pretty new to the coding, hence the above code might not be that optimized.

@sharadm20
Copy link

can someone explain the for loop

for(int i=0; i < m; i++) {

  crushState[ops[i].a-1] += ops[i].k;
  crushState[ops[i].b] -= ops[i].k;//this line preferably
  | }

@s1ac2x1
Copy link

s1ac2x1 commented Aug 16, 2017

There is no need to populate crushState with zeroes, it is the default behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment