Skip to content

Commit 1d9efc7

Browse files
committed
update cheatsheet
1 parent df3ab01 commit 1d9efc7

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

doc/cheatsheet/sort.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,4 +382,85 @@ public String sort(String s) {
382382
Arrays.sort(t);
383383
return new String(t);
384384
}
385-
```
385+
```
386+
387+
### 2-7) Car Fleet
388+
389+
```java
390+
// java
391+
392+
// LC 853. Car Fleet
393+
394+
// V0
395+
// IDEA: pair position and speed, sorting (gpt)
396+
/**
397+
* IDEA :
398+
*
399+
* The approach involves sorting the cars by their starting positions
400+
* (from farthest to nearest to the target)
401+
* and computing their time to reach the target.
402+
* We then iterate through these times to count the number of distinct fleets.
403+
*
404+
*
405+
*
406+
* Steps in the Code:
407+
* 1. Pair Cars with Their Speeds:
408+
* • Combine position and speed into a 2D array cars for easier sorting and access.
409+
* 2. Sort Cars by Position Descending:
410+
* • Use Arrays.sort with a custom comparator to sort cars from farthest to nearest relative to the target.
411+
* 3. Calculate Arrival Times:
412+
* • Compute the time each car takes to reach the target using the formula:
413+
*
414+
* time = (target - position) / speed
415+
*
416+
* 4. Count Fleets:
417+
* • Iterate through the times array:
418+
* • If the current car’s arrival time is greater than the lastTime (time of the last fleet), it forms a new fleet.
419+
* • Update lastTime to the current car’s time.
420+
* 5. Return Fleet Count:
421+
* • The number of distinct times that exceed lastTime corresponds to the number of fleets.
422+
*
423+
*/
424+
public int carFleet(int target, int[] position, int[] speed) {
425+
int n = position.length;
426+
// Pair positions with speeds and `sort by position in descending order`
427+
// cars : [position][speed]
428+
int[][] cars = new int[n][2];
429+
for (int i = 0; i < n; i++) {
430+
cars[i][0] = position[i];
431+
cars[i][1] = speed[i];
432+
}
433+
434+
/**
435+
* NOTE !!!
436+
*
437+
* Sort by position descending (simulate the "car arriving" process
438+
*/
439+
Arrays.sort(cars, (a, b) -> b[0] - a[0]); // Sort by position descending
440+
441+
// Calculate arrival times
442+
double[] times = new double[n];
443+
for (int i = 0; i < n; i++) {
444+
times[i] = (double) (target - cars[i][0]) / cars[i][1];
445+
}
446+
447+
// Count fleets
448+
int fleets = 0;
449+
double lastTime = 0;
450+
for (double time : times) {
451+
/**
452+
* 4. Count Fleets:
453+
* • Iterate through the times array:
454+
* • If the current car’s arrival time is greater than the lastTime (time of the last fleet), it forms a new fleet.
455+
* • Update lastTime to the current car’s time.
456+
*/
457+
// If current car's time is greater than the last fleet's time, it forms a new fleet
458+
if (time > lastTime) {
459+
fleets++;
460+
lastTime = time;
461+
}
462+
}
463+
464+
return fleets;
465+
}
466+
```

0 commit comments

Comments
 (0)