One Pager Cheat Sheet
- Write a method to return the frequency of the highest value in a 2D matrix array, which is initially prefilled with 0s, where the size of the array is
m
rows andn
columns, and given a list of increment operations in the form of a two-number array[row, column]
. - Given the operations
[1,1]
and then[2,2]
on a blank state of all0
s, we obtain a modified state of2
s. - The
largest number
in theupper left
of thematrix
wasincremented
by1
due to theoverlap
of the twooperations
. - The cells with the greatest numbers can be found by looking at the edges of the range of each operation, as these are guaranteed to be layered over the most, with
O(n)
complexity.
This is our final solution.
To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.
xxxxxxxxxx
45
}
var assert = require('assert');
​
function maxFromOps(m, n, operations) {
var minCol = m;
var minRow = n;
​
for (let op of operations) {
minCol = Math.min(minCol, op[0]);
minRow = Math.min(minRow, op[1]);
}
return minCol * minRow;
}
​
try {
assert.equal(
maxFromOps(4, 4, [
[1, 1],
[2, 2],
]),
1
);
​
console.log(
'PASSED: ' + 'Expect `maxFromOps(4, 4, [[1, 1], [2, 2]])` to return `1`'
);
} catch (err) {
console.log(err);
}
​
try {
assert.equal(
maxFromOps(4, 4, [
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
You're doing a wonderful job. Keep going!
If you had any problems with this tutorial, check out the main forum thread here.