You are viewing a single comment's thread. Return to all comments →
great solution. thank you.
Same algorithm on C++ // Complete the arrayManipulation function below. long arrayManipulation(int n, vector> queries) {
vector<long> v(n, 0); for(vector<int> q: queries) { v[q[0]-1] += q[2]; v[q[1]] -= q[2]; } long max = 0; long sum = 0; for(long l: v) { sum += l; if(max < sum) { max = sum; } } return max;
}
This code has a little bug. This is the code fixed:
long arrayManipulation(int n, vector> queries) { vector<long> v(n, 0); for(vector<int> q: queries) { v[q[0]-1] += q[2]; if (q[1] < v.size()) v[q[1]] -= q[2]; } long max = 0; long sum = 0; for(long l: v) { sum += l; if(max < sum) { max = sum; } } return max; }
Seems like cookies are disabled on this browser, please enable them to open this website
I agree to HackerRank's Terms of Service and Privacy Policy.
Array Manipulation
You are viewing a single comment's thread. Return to all comments →
great solution. thank you.
Same algorithm on C++ // Complete the arrayManipulation function below. long arrayManipulation(int n, vector> queries) {
}
This code has a little bug. This is the code fixed: