Skip to content

Instantly share code, notes, and snippets.

@kkabdol
Created January 29, 2019 08:33
Show Gist options
  • Save kkabdol/75e75426e5d659e78da317bb522ae0f2 to your computer and use it in GitHub Desktop.
Save kkabdol/75e75426e5d659e78da317bb522ae0f2 to your computer and use it in GitHub Desktop.
Array Manipulation
// problem : https://www.hackerrank.com/challenges/crush/problem
// solution : https://www.hackerrank.com/challenges/crush/forum
long arrayManipulation(int n, vector<vector<int>> queries) {
vector<long> arr( n + 1, 0 );
for( auto q : queries )
{
arr[ q[0] ] += q[2];
if( q[1] + 1 <= n )
{
arr[ q[1] + 1 ] -= q[2];
}
}
long maxK = 0, accK = 0;
for( auto k : arr )
{
accK += k;
if( accK > maxK )
{
maxK = accK;
}
}
return maxK;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment