Skip to content

Instantly share code, notes, and snippets.

@CarlaTeo
Created July 15, 2022 23:51
Show Gist options
  • Save CarlaTeo/15bc2b9d7c907cde0897fefb5235c3d2 to your computer and use it in GitHub Desktop.
Save CarlaTeo/15bc2b9d7c907cde0897fefb5235c3d2 to your computer and use it in GitHub Desktop.
[JS] Double Rotary Lock
/**
* @param {number} N
* @param {number} M
* @param {number[]} C
*/
function getMinCodeEntryTime(N, M, C) {
function minDist(i, j) {
const path1 = Math.abs(j - i);
const bigger = Math.max(i, j);
const smaller = Math.min(i, j);
const path2 = N - bigger + smaller;
return Math.min(path1, path2);
}
C.unshift(1); // C = [1,1,2,3]
const R = [minDist(1, C[1])]; // R = [2, 2, 2]
for(let i = 2; i <= M; i++) { // i = 3
const Raux = []; // [2, 3]
for(let k = 0; k < R.length ; k++) { // k = 1 (k < 2 ?)
Raux[k] = R[k] + minDist(C[i], C[k]);
R[k] += minDist(C[i], C[i - 1]);
}
R[R.length] = Math.min(...Raux);
}
return Math.min(...R);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment