Skip to content

Instantly share code, notes, and snippets.

@abozhilov
Created December 15, 2015 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abozhilov/409d653c453957dac702 to your computer and use it in GitHub Desktop.
Save abozhilov/409d653c453957dac702 to your computer and use it in GitHub Desktop.
Bridges
var north = [2, 5, 3, 3, 3, 1, 8, 2, 6, 7, 6];
var south = [1, 2, 5, 3, 4, 1, 7, 8, 2, 5, 6];
var nLen = north.length;
var sLen = south.length;
var matrix = [];
for (var i = 0; i <= nLen; i++) {
matrix[i] = [];
for (var j = 0; j <= sLen; j++) {
matrix[i][j] = 0;
}
}
for (var i = 0; i < nLen; i++) {
for (var j = 0; j < sLen; j++) {
if (north[i] == south[j]) {
matrix[i + 1][j + 1] = Math.max(matrix[i + 1][j], matrix[i][j + 1]) + 1;
}
else {
matrix[i + 1][j + 1] = Math.max(matrix[i][j + 1], matrix[i + 1][j]);
}
}
}
console.log(matrix[nLen][sLen]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment