File tree Expand file tree Collapse file tree 5 files changed +61
-34
lines changed
minimum-swaps-to-make-sequences-increasing Expand file tree Collapse file tree 5 files changed +61
-34
lines changed Original file line number Diff line number Diff line change 1
- import pathSum from "../path-sum-iii/index.ts" ;
2
-
3
- export default pathSum ;
1
+ import pathSum from "../path-sum-iii/index.ts" ;
2
+
3
+ export default pathSum ;
Original file line number Diff line number Diff line change @@ -1316,6 +1316,8 @@ https://leetcode.cn/problems/delete-node-in-a-linked-list/
1316
1316
1317
1317
https://leetcode.cn/problems/4ueAj6/
1318
1318
1319
+ https://leetcode.cn/problems/linked-list-components/
1320
+
1319
1321
https://leetcode.cn/problems/insert-into-a-sorted-circular-linked-list/
1320
1322
1321
1323
https://leetcode.cn/problems/insert-delete-getrandom-o1/
Original file line number Diff line number Diff line change
1
+ import { ListNode } from "../reverse-linked-list/ListNode.ts" ;
2
+
3
+ export default function numComponents (
4
+ head : ListNode | null ,
5
+ nums : number [ ] ,
6
+ ) : number {
7
+ const numsSet = new Set < number > ( ) ;
8
+ for ( const num of nums ) {
9
+ numsSet . add ( num ) ;
10
+ }
11
+ let inSet = false ;
12
+ let res = 0 ;
13
+ while ( head ) {
14
+ if ( numsSet . has ( head . val ) ) {
15
+ if ( ! inSet ) {
16
+ inSet = true ;
17
+ res ++ ;
18
+ }
19
+ } else {
20
+ inSet = false ;
21
+ }
22
+ head = head . next ;
23
+ }
24
+ return res ;
25
+ }
Original file line number Diff line number Diff line change 1
- export default function minSwap ( nums1 : number [ ] , nums2 : number [ ] ) : number {
2
- const n = nums1 . length ;
3
- let a = 0 ,
4
- b = 1 ;
5
- for ( let i = 1 ; i < n ; i ++ ) {
6
- const at = a ,
7
- bt = b ;
8
- a = b = n ;
9
- if ( nums1 [ i ] > nums1 [ i - 1 ] && nums2 [ i ] > nums2 [ i - 1 ] ) {
10
- a = Math . min ( a , at ) ;
11
- b = Math . min ( b , bt + 1 ) ;
12
- }
13
- if ( nums1 [ i ] > nums2 [ i - 1 ] && nums2 [ i ] > nums1 [ i - 1 ] ) {
14
- a = Math . min ( a , bt ) ;
15
- b = Math . min ( b , at + 1 ) ;
16
- }
17
- }
18
- return Math . min ( a , b ) ;
19
- }
1
+ export default function minSwap ( nums1 : number [ ] , nums2 : number [ ] ) : number {
2
+ const n = nums1 . length ;
3
+ let a = 0 ,
4
+ b = 1 ;
5
+ for ( let i = 1 ; i < n ; i ++ ) {
6
+ const at = a ,
7
+ bt = b ;
8
+ a = b = n ;
9
+ if ( nums1 [ i ] > nums1 [ i - 1 ] && nums2 [ i ] > nums2 [ i - 1 ] ) {
10
+ a = Math . min ( a , at ) ;
11
+ b = Math . min ( b , bt + 1 ) ;
12
+ }
13
+ if ( nums1 [ i ] > nums2 [ i - 1 ] && nums2 [ i ] > nums1 [ i - 1 ] ) {
14
+ a = Math . min ( a , bt ) ;
15
+ b = Math . min ( b , at + 1 ) ;
16
+ }
17
+ }
18
+ return Math . min ( a , b ) ;
19
+ }
Original file line number Diff line number Diff line change 1
- import { assertEquals } from "https://deno.land/[email protected] /testing/asserts.ts" ;
2
- import TripleInOne from "./index.ts" ;
3
-
4
- Deno . test ( "three-in-one-lcci" , ( ) => {
5
- const tio = new TripleInOne ( 1 ) ;
6
- tio . push ( 0 , 1 ) ;
7
- tio . push ( 0 , 2 ) ;
8
- assertEquals ( 1 , tio . pop ( 0 ) ) ;
9
- assertEquals ( - 1 , tio . pop ( 0 ) ) ;
10
- assertEquals ( - 1 , tio . pop ( 0 ) ) ;
11
- assertEquals ( true , tio . isEmpty ( 0 ) ) ;
12
- } ) ;
1
+ import { assertEquals } from "https://deno.land/[email protected] /testing/asserts.ts" ;
2
+ import TripleInOne from "./index.ts" ;
3
+
4
+ Deno . test ( "three-in-one-lcci" , ( ) => {
5
+ const tio = new TripleInOne ( 1 ) ;
6
+ tio . push ( 0 , 1 ) ;
7
+ tio . push ( 0 , 2 ) ;
8
+ assertEquals ( 1 , tio . pop ( 0 ) ) ;
9
+ assertEquals ( - 1 , tio . pop ( 0 ) ) ;
10
+ assertEquals ( - 1 , tio . pop ( 0 ) ) ;
11
+ assertEquals ( true , tio . isEmpty ( 0 ) ) ;
12
+ } ) ;
You can’t perform that action at this time.
0 commit comments