File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -25,3 +25,4 @@ https://neetcode.io/roadmap
25
25
| 150 | [ Evaluate Reverse Polish Notation] ( https://leetcode.com/problems/evaluate-reverse-polish-notation/ ) | Medium | [ TypeScript] ( ./TypeScript/150.evaluate-reverse-polish-notation.ts ) | Stack |
26
26
| 22 | [ Generate Parentheses] ( https://leetcode.com/problems/generate-parentheses/ ) | Medium | [ TypeScript] ( ./TypeScript/22.generate-parentheses.ts ) | Stack |
27
27
| 739 | [ Daily Temperatures] ( https://leetcode.com/problems/daily-temperatures/ ) | Medium | [ TypeScript] ( ./TypeScript/739.daily-temperatures.ts ) | Stack |
28
+ | 853 | [ Car Fleet] ( https://leetcode.com/problems/car-fleet/ ) | Medium | [ TypeScript] ( ./TypeScript/853.car-fleet.ts ) | Stack |
Original file line number Diff line number Diff line change
1
+ type CarPosition = number ;
2
+ type CarTime = number ;
3
+ type CarItem = [ CarPosition , CarTime ] ;
4
+
5
+ function carFleet ( target : number , position : number [ ] , speed : number [ ] ) : number {
6
+ const items : CarItem [ ] = new Array ( position . length ) ;
7
+ let result = 0 ;
8
+ let maxTime = 0 ;
9
+
10
+ for ( let i = 0 ; i < items . length ; i ++ ) {
11
+ items [ i ] = [ position [ i ] , ( target - position [ i ] ) / speed [ i ] ] ;
12
+ }
13
+ items . sort ( ( [ p1 ] , [ p2 ] ) => p2 - p1 ) ;
14
+
15
+ for ( let i = 0 ; i < items . length ; i ++ ) {
16
+ if ( items [ i ] [ 1 ] > maxTime ) {
17
+ result += 1 ;
18
+ maxTime = items [ i ] [ 1 ] ;
19
+ }
20
+ }
21
+
22
+ return result ;
23
+ }
You can’t perform that action at this time.
0 commit comments