Created
June 30, 2021 15:57
-
-
Save sagittaracc/6ded6908de9fa6bccc348aade16211c7 to your computer and use it in GitHub Desktop.
Google interview
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
| var houses = [ | |
| { | |
| bk: true, | |
| mac: false, | |
| kfc: false | |
| }, | |
| { | |
| bk: false, | |
| mac: false, | |
| kfc: false | |
| }, | |
| { | |
| bk: true, | |
| mac: false, | |
| kfc: true | |
| }, | |
| { | |
| bk: true, | |
| mac: false, | |
| kfc: true | |
| }, | |
| { | |
| bk: true, | |
| mac: false, | |
| kfc: true | |
| }, | |
| { | |
| bk: false, | |
| mac: false, | |
| kfc: true | |
| }, | |
| { | |
| bk: false, | |
| mac: false, | |
| kfc: false | |
| } | |
| ]; | |
| var demand = ['mac']; | |
| function getClosestHouseFor(demand, house) | |
| { | |
| var distance; | |
| var closest = Infinity; | |
| for (var i = 0; i < houses.length; i++) { | |
| if (houses[i][demand] === true) { | |
| distance = Math.abs(i - house); | |
| if (distance < closest) { | |
| closest = distance; | |
| } | |
| } | |
| } | |
| return closest; | |
| } | |
| var currMinPath = Infinity; | |
| var currHouseIndex = null; | |
| for (var i = 0; i < houses.length; i++) { | |
| var path = 0; | |
| for (var j = 0; j < demand.length; j++) { | |
| path += getClosestHouseFor(demand[j], i); | |
| } | |
| if (path < currMinPath) { | |
| currMinPath = path; | |
| currHouseIndex = i; | |
| } | |
| } | |
| console.log(currHouseIndex); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment