Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add 853-Car-Fleet
  • Loading branch information
loczek authored Jul 6, 2022
commit 83b7926243617867f8784919e020f72f8fc7f8d1
25 changes: 25 additions & 0 deletions javascript/853-Car-Fleet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function carFleet(target, position, speed) {
const combined = position
.map((item, index) => {
return [item, speed[index]];
})
.sort((a, b) => a[0] - b[0]);

const stack = [];

for (let i = combined.length - 1; i > -1; i--) {
const p = combined[i][0];
const s = combined[i][1];

stack.push((target - p) / s);

if (
stack.length >= 2 &&
stack[stack.length - 1] <= stack[stack.length - 2]
) {
stack.pop();
}
}

return stack.length;
}