Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function minimumMovement(obstacleLanes) {
// Write your code here
// console.log(obstacleLanes)
let currentLine = 2;
let total_move = 0;
var len = obstacleLanes.length;
var i = 0
for (i = 0; i < len; i++) {
let element = obstacleLanes[i]
let next = obstacleLanes[i + 1]
if (currentLine == element) { // have to move +1 or -1
total_move = total_move + 1
temp = obstacleLanes.slice(i + 1, )
console.log(temp);
if (!temp.includes(element)) {
return total_move
}
if (next == 3) {
currentLine = currentLine - 1 // have to move middle lane from 3
} else if (next == 1) {
currentLine = currentLine + 1 // have to move middle land from 1
}
}
}
// console.log(total_move)
return total_move
}
input = [2, 3, 2, 1, 3, 1]
console.log(minimumMovement(input))
input = [3, 2, 2, 1, 2, 1]
console.log(minimumMovement(input))
input = [2, 1, 3, 3, 3, 1]
console.log(minimumMovement(input))
input = [3, 2, 2, 1, 2, 1]
console.log(minimumMovement(input))
input = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
console.log(minimumMovement(input))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.