-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmax-array-queries.js
101 lines (87 loc) · 2.27 KB
/
max-array-queries.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
*
* ----------------------
* | MAX OF ARRAY QUERIES |
* ----------------------
*
*
* Given an array of integers, find the sum of its elements.
*
* e.g.
*
* numArr = [1, 3, 4 ]
*
* maxArrayQueries(numArr) // returns 1 + 3 + 4
*
*
* */
/*
*
* ***** SOLUTION
*
* */
function maxArrayQueries(n, queries) {
let maxValue = 0;
const numberMap = {};
// run queries
for(let query of queries){
for(let i = query[0]; i <= query[1]; i++) {
numberMap[i] = numberMap[i] + query[2] || query[2];
if(numberMap[i] > maxValue){
maxValue = numberMap[i];
};
};
};
return maxValue;
};
// function maxArrayQueries(n, queries) {
// // create array of zeros
// let initialArr = [];
// for(let i = 0; i < n; i++){
// initialArr.push(0);
// };
// // console.log("initialArr", initialArr);
// let lowest = 0;
// let highest = 0;
// // run queries
// for(let query of queries){
// if(query[0] < lowest){
// lowest = query[0];
// };
// if(query[0] > highest){
// highest = query[0];
// };
// // console.log("query --- ", query);
// for(let j = query[0]; j <= query[1]; j ++){
// initialArr[j - 1] += query[2];
// };
// // console.log("finishedArr -- ", initialArr);
// };
// // find largest value and return it
// // initialize maxValue
// let maxValue = 0;
// for(let i = lowest - 1; i < highest; i++){
// if(initialArr[i] > maxValue){
// maxValue = initialArr[i];
// };
// };
// return maxValue;
// };
/*
*
* ***** TESTS
*
* */
const n1 = 10;
const queries1 = [
[1, 5, 3],
[4, 8, 7],
[6, 9, 1]
];
console.log("## TEST 1 ----- > ", maxArrayQueries(n1, queries1)); // 10
// const magazine2 = ["hello", "are", "you", "there", "friend"];
// const note2 = ["friend", "hello", "are", "you", "there"];
// console.log("## TEST 1 ----- > ", maxArrayQueries(magazine2, note2)); // true
// const magazine3 = ["hello", "are", "you", "there", "friend"];
// const note3 = ["hello", "friend", "hello", "are", "you", "there"];
// console.log("## TEST 1 ----- > ", maxArrayQueries(magazine3, note3)); // false