File tree 4 files changed +172
-0
lines changed
solution/0600-0699/0646.Maximum Length of Pair Chain
4 files changed +172
-0
lines changed Original file line number Diff line number Diff line change @@ -209,6 +209,78 @@ func findLongestChain(pairs [][]int) int {
209
209
}
210
210
```
211
211
212
+ ### ** TypeScript**
213
+
214
+ ``` ts
215
+ function findLongestChain(pairs : number [][]): number {
216
+ pairs .sort ((a , b ) => a [0 ] - b [0 ]);
217
+ const n = pairs .length ;
218
+ const dp = new Array (n ).fill (1 );
219
+ for (let i = 0 ; i < n ; i ++ ) {
220
+ for (let j = 0 ; j < i ; j ++ ) {
221
+ if (pairs [i ][0 ] > pairs [j ][1 ]) {
222
+ dp [i ] = Math .max (dp [i ], dp [j ] + 1 );
223
+ }
224
+ }
225
+ }
226
+ return dp [n - 1 ];
227
+ }
228
+ ```
229
+
230
+ ``` ts
231
+ function findLongestChain(pairs : number [][]): number {
232
+ pairs .sort ((a , b ) => a [1 ] - b [1 ]);
233
+ let res = 0 ;
234
+ let pre = - Infinity ;
235
+ for (const [a, b] of pairs ) {
236
+ if (pre < a ) {
237
+ pre = b ;
238
+ res ++ ;
239
+ }
240
+ }
241
+ return res ;
242
+ }
243
+ ```
244
+
245
+ ### ** Rust**
246
+
247
+ ``` rust
248
+ impl Solution {
249
+ pub fn find_longest_chain (mut pairs : Vec <Vec <i32 >>) -> i32 {
250
+ pairs . sort_by (| a , b | a [0 ]. cmp (& b [0 ]));
251
+ let n = pairs . len ();
252
+ let mut dp = vec! [1 ; n ];
253
+ for i in 0 .. n {
254
+ for j in 0 .. i {
255
+ if pairs [i ][0 ] > pairs [j ][1 ] {
256
+ dp [i ] = dp [i ]. max (dp [j ] + 1 );
257
+ }
258
+ }
259
+ }
260
+ dp [n - 1 ]
261
+ }
262
+ }
263
+ ```
264
+
265
+ ``` rust
266
+ impl Solution {
267
+ pub fn find_longest_chain (mut pairs : Vec <Vec <i32 >>) -> i32 {
268
+ pairs . sort_by (| a , b | a [1 ]. cmp (& b [1 ]));
269
+ let mut res = 0 ;
270
+ let mut pre = i32 :: MIN ;
271
+ for pair in pairs . iter () {
272
+ let a = pair [0 ];
273
+ let b = pair [1 ];
274
+ if pre < a {
275
+ pre = b ;
276
+ res += 1 ;
277
+ }
278
+ }
279
+ res
280
+ }
281
+ }
282
+ ```
283
+
212
284
### ** ...**
213
285
214
286
```
Original file line number Diff line number Diff line change @@ -199,6 +199,78 @@ func findLongestChain(pairs [][]int) int {
199
199
}
200
200
```
201
201
202
+ ### ** TypeScript**
203
+
204
+ ``` ts
205
+ function findLongestChain(pairs : number [][]): number {
206
+ pairs .sort ((a , b ) => a [0 ] - b [0 ]);
207
+ const n = pairs .length ;
208
+ const dp = new Array (n ).fill (1 );
209
+ for (let i = 0 ; i < n ; i ++ ) {
210
+ for (let j = 0 ; j < i ; j ++ ) {
211
+ if (pairs [i ][0 ] > pairs [j ][1 ]) {
212
+ dp [i ] = Math .max (dp [i ], dp [j ] + 1 );
213
+ }
214
+ }
215
+ }
216
+ return dp [n - 1 ];
217
+ }
218
+ ```
219
+
220
+ ``` ts
221
+ function findLongestChain(pairs : number [][]): number {
222
+ pairs .sort ((a , b ) => a [1 ] - b [1 ]);
223
+ let res = 0 ;
224
+ let pre = - Infinity ;
225
+ for (const [a, b] of pairs ) {
226
+ if (pre < a ) {
227
+ pre = b ;
228
+ res ++ ;
229
+ }
230
+ }
231
+ return res ;
232
+ }
233
+ ```
234
+
235
+ ### ** Rust**
236
+
237
+ ``` rust
238
+ impl Solution {
239
+ pub fn find_longest_chain (mut pairs : Vec <Vec <i32 >>) -> i32 {
240
+ pairs . sort_by (| a , b | a [0 ]. cmp (& b [0 ]));
241
+ let n = pairs . len ();
242
+ let mut dp = vec! [1 ; n ];
243
+ for i in 0 .. n {
244
+ for j in 0 .. i {
245
+ if pairs [i ][0 ] > pairs [j ][1 ] {
246
+ dp [i ] = dp [i ]. max (dp [j ] + 1 );
247
+ }
248
+ }
249
+ }
250
+ dp [n - 1 ]
251
+ }
252
+ }
253
+ ```
254
+
255
+ ``` rust
256
+ impl Solution {
257
+ pub fn find_longest_chain (mut pairs : Vec <Vec <i32 >>) -> i32 {
258
+ pairs . sort_by (| a , b | a [1 ]. cmp (& b [1 ]));
259
+ let mut res = 0 ;
260
+ let mut pre = i32 :: MIN ;
261
+ for pair in pairs . iter () {
262
+ let a = pair [0 ];
263
+ let b = pair [1 ];
264
+ if pre < a {
265
+ pre = b ;
266
+ res += 1 ;
267
+ }
268
+ }
269
+ res
270
+ }
271
+ }
272
+ ```
273
+
202
274
### ** ...**
203
275
204
276
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn find_longest_chain ( mut pairs : Vec < Vec < i32 > > ) -> i32 {
3
+ pairs. sort_by ( |a, b| a[ 1 ] . cmp ( & b[ 1 ] ) ) ;
4
+ let mut res = 0 ;
5
+ let mut pre = i32:: MIN ;
6
+ for pair in pairs. iter ( ) {
7
+ let a = pair[ 0 ] ;
8
+ let b = pair[ 1 ] ;
9
+ if pre < a {
10
+ pre = b;
11
+ res += 1 ;
12
+ }
13
+ }
14
+ res
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ function findLongestChain ( pairs : number [ ] [ ] ) : number {
2
+ pairs . sort ( ( a , b ) => a [ 1 ] - b [ 1 ] ) ;
3
+ let res = 0 ;
4
+ let pre = - Infinity ;
5
+ for ( const [ a , b ] of pairs ) {
6
+ if ( pre < a ) {
7
+ pre = b ;
8
+ res ++ ;
9
+ }
10
+ }
11
+ return res ;
12
+ }
You can’t perform that action at this time.
0 commit comments