Created
January 24, 2019 20:30
-
-
Save kyeongan/a23d9e986f287971dab1bc8878205577 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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