Last active
February 14, 2018 21:05
-
-
Save caglarorhan/19d18d390b13f94272fcc23a7c07f973 to your computer and use it in GitHub Desktop.
Array Manipulation from: https://www.hackerrank.com/challenges/crush/problem
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
// 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; | |
} |
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
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)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My solution is bit slower which cause out of time in some text cases. There is an unnecesary for loop in my algorithm.