Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array.
Example:
Input: n=5,m=3,queries={{1,2,100},{2,5,100},{3,4,100}}
Output: 200
Approach
Java
public class ArrayManipulation {public static void main(String[] args) {int n = 5, m = 3;int[][] queries = { { 1, 2, 100 }, { 2, 5, 100 }, { 3, 4, 100 } };System.out.println(arrayManipulation(n, queries));}static long arrayManipulation(int n, int[][] queries) {long arr[] = new long[n + 2];int a, b, k;int m = queries.length;for (int i = 0; i < m; i++) {a = queries[i][0];b = queries[i][1];k = queries[i][2];arr[a] += k;arr[b + 1] += -k;}for (int i = 2; i <= n; i++) {arr[i] += arr[i - 1];}long max1 = arr[1];for (int i = 2; i <= n; i++) {if (arr[i] > max1) {max1 = arr[i];}}return max1;}}
C++
#include <bits/stdc++.h>using namespace std;long arrayManipulation(int n, vector<vector<int>> queries){long long *arr = new long long[n + 2];fill(arr, arr + n + 2, 0);int a, b, k;int m = queries.size();for (long long i = 0; i < m; i++){a = queries[i][0];b = queries[i][1];k = queries[i][2];arr[a] += k;arr[b + 1] += -k;}for (long long i = 2; i <= n; i++){arr[i] += arr[i - 1];}long long max1 = arr[1];for (int i = 2; i <= n; i++){if (arr[i] > max1){max1 = arr[i];}}return max1;}int main(){long long n = 5, m = 3;vector<vector<int>> queries = {{1, 2, 100}, {2, 5, 100}, {3, 4, 100}};cout << arrayManipulation(n, queries);return 0;}
No comments:
Post a Comment