Thanks to visit codestin.com
Credit goes to github.com

Skip to content

853. Car Fleet.java #282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 32 additions & 0 deletions java/853. Car Fleet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public int carFleet(int target, int[] position, int[] speed) {
double[][] mergedPoints = new double[position.length][2];
for (int idx=0;idx<position.length;idx++){
mergedPoints[idx][0] = position[idx];
mergedPoints[idx][1] = speed[idx];
}
Arrays.sort(mergedPoints, Comparator.comparingDouble(o -> -o[0]));
Stack<Double> backup = new Stack();
for (int idx=0;idx<position.length;idx++){

double _position = mergedPoints[idx][0];
double _speed = mergedPoints[idx][1];

//System.out.println("_position : " + _position);

double _timeTaken = (target-_position)/_speed;
double _lastValue = 0;
if (!backup.isEmpty()){
_lastValue = backup.peek();
}
backup.push(_timeTaken);
//System.out.println(_timeTaken +"<="+ _lastValue +" is true :" + (_timeTaken <= _lastValue));
if (backup.size()>1 &&
_timeTaken <= _lastValue){
backup.pop();
}
}
return backup.size();

}
}
16 changes: 0 additions & 16 deletions javascript/217-Contains-Duplicate.js

This file was deleted.