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)); | |
} |
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
You are given a list(1-indexed) of size , initialized with zeroes. You have to perform operations on the list and output the maximum of final values of all the elements in the list. For every operation, you are given three integers , and and you have to add value to all the elements ranging from index to (both inclusive).
For example, consider a list of size . The initial list would be = [, , ] and after performing the update = , the new list would be = [, , ]. Here, we've added value 30 to elements between indices 2 and 3. Note the index of the list starts from 1.
Input Format
The first line will contain two integers and separated by a single space.
Next lines will contain three integers , and separated by a single space.
Numbers in list are numbered from to .
Constraints
Output Format
Print in a single line the maximum value in the updated list.
Sample Input
5 3
1 2 100
2 5 100
3 4 100
Sample Output
200
Explanation
After first update list will be 100 100 0 0 0.
After second update list will be 100 200 100 100 100.
After third update list will be 100 200 200 200 100.
So the required answer will be 200.