Skip to content

Instantly share code, notes, and snippets.

@Kyeongan
Created January 24, 2019 20:30
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 Kyeongan/a23d9e986f287971dab1bc8878205577 to your computer and use it in GitHub Desktop.
Save Kyeongan/a23d9e986f287971dab1bc8878205577 to your computer and use it in GitHub Desktop.
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