-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathmaxCounters.js
More file actions
53 lines (42 loc) · 1.45 KB
/
maxCounters.js
File metadata and controls
53 lines (42 loc) · 1.45 KB
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
//Iteration over array TC: O(m+n), SC(n)
function maxCounters(n, operations) {
const counters = Array(n).fill(0);
let max = 0, lastUpdate = 0;
for(let i = 0; i < operations.length; i++) {
const counterNumber = operations[i];
if(1 <= counterNumber && counterNumber <= n) {
// Apply lazy update if needed
if(counters[counterNumber-1] < lastUpdate) {
counters[counterNumber-1] = lastUpdate;
}
//Increment counter by one
counters[counterNumber-1]++;
//Update maximum value so far
if(counters[counterNumber-1] > max) {
max = counters[counterNumber-1];
}
} else if(counterNumber === n+1) {
//defer the update
lastUpdate = max;
}
}
// Final update: make all counters up to date
for(let i=0; i < counters.length; i++) {
if(counters[i] < lastUpdate) {
counters[i] = lastUpdate;
}
}
return counters;
}
// Example 1: Mixed operations
const n1 = 4;
const operations1 = [1, 4, 4, 5, 2, 4, 4];
console.log(maxCounters(n1, operations1)); // Output: [2, 3, 2, 4]
// Example 2: No max operation
const n2 = 3;
const operations2 = [1, 2, 1, 3];
console.log(maxCounters(n2, operations2)); // Output: [2, 1, 1]
// Example 3: All max operations
const n3 = 2;
const operations3 = [3, 3, 3];
console.log(maxCounters(n3, operations3)); // Output: [0, 0]