Skip to content

Instantly share code, notes, and snippets.

@sagar-gavhane
Last active September 12, 2020 04:51
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 sagar-gavhane/f597a2f96ba08fcaf3b829d8b5ec27e9 to your computer and use it in GitHub Desktop.
Save sagar-gavhane/f597a2f96ba08fcaf3b829d8b5ec27e9 to your computer and use it in GitHub Desktop.
Combined drivers solution
function getMilliseconds(date) {
const [hourse, minute] = date.split(':')
return hourse*60*60 + minute*60
}
function sortingFn (a, z) {
return getMilliseconds(a[0]) - getMilliseconds(z[0])
}
function combinedDrivers(city1, city2) {
const sorted = city1.concat(city2).sort(sortingFn)
for(let i = 0; i < sorted.length -1; i++) {
if (sorted[i][0] === sorted[i+1][0]) {
sorted[i][1] += sorted[i+1][1]
sorted.splice(i+1, 1)
}
}
return sorted
}
let bangalore = [['1:00', 10], ['1:15', 20], ['1:05', 12], ['2:00', 9]]
let kolkata = [['1:03', 11], ['1:30',3], ['2:30', 4], ['1:15', 11]]
let result = combinedDrivers(bangalore, kolkata)
console.log(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment