Skip to content

Instantly share code, notes, and snippets.

@caglarorhan
Last active February 14, 2018 21:05
Show Gist options
  • Save caglarorhan/19d18d390b13f94272fcc23a7c07f973 to your computer and use it in GitHub Desktop.
Save caglarorhan/19d18d390b13f94272fcc23a7c07f973 to your computer and use it in GitHub Desktop.
// https://www.hackerrank.com/amansbhandari?hr_r=1
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
long int N,K,p,q,sum,i,j,max=0,x=0;
cin>>N>>K;
long int *a=new long int[N+1]();
for(i=0;i<K;i++)
{
cin>>p>>q>>sum;
a[p]+=sum;
if((q+1)<=N) a[q+1]-=sum;
}
for(i=1;i<=N;i++)
{
x=x+a[i];
if(max<x) max=x;
}
cout<<max;
return 0;
}
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var n_tempA = input_stdin_array[0].split(' ');
var n = parseInt(n_tempA[0]);
var m = parseInt(n_tempA[1]);
var maxInList = 0; var newList=[]; for(xcv=1; xcv<=n; xcv++){newList.push(0);} // added
for(xcv=1; xcv<input_stdin_array.length;xcv++){
let a_temp = input_stdin_array[xcv].split(' ');
a_temp = a_temp.map(Number);
for(c=a_temp[0]; c<=a_temp[1]; c++){
newList[c-1]+=a_temp[2];
//maxInList = (maxInList<newList[c-1])?newList[c-1]:maxInList;
}
//console.log(newList);
}
console.log(maxInList);
//console.log(Math.max(...newList));
}
@caglarorhan
Copy link
Author

My solution is bit slower which cause out of time in some text cases. There is an unnecesary for loop in my algorithm.

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