Created
December 15, 2015 18:51
-
-
Save abozhilov/409d653c453957dac702 to your computer and use it in GitHub Desktop.
Bridges
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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