File tree Expand file tree Collapse file tree 5 files changed +1062
-15
lines changed
booking-concert-tickets-in-groups
partition-array-into-disjoint-intervals Expand file tree Collapse file tree 5 files changed +1062
-15
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,8 @@ Step 2. Add the dependency
45
45
46
46
<summary >展开查看</summary >
47
47
48
+ https://leetcode.cn/problems/shortest-bridge/
49
+
48
50
https://leetcode.cn/problems/partition-array-into-disjoint-intervals/
49
51
50
52
https://leetcode.cn/problems/maximum-profit-in-job-scheduling/
Original file line number Diff line number Diff line change @@ -10,7 +10,6 @@ class SegmentTree<T> {
10
10
) {
11
11
this . #root = new TreeNode < T > ( ) ;
12
12
this . #build( this . #root, start , end ) ;
13
-
14
13
}
15
14
#build( node : TreeNode < T > , start : number , end : number ) {
16
15
if ( start === end ) {
Original file line number Diff line number Diff line change 1
- export default function partitionDisjoint ( nums : number [ ] ) : number {
2
- const n = nums . length ;
3
- let leftMax = nums [ 0 ] ,
4
- leftPos = 0 ,
5
- curMax = nums [ 0 ] ;
6
- for ( let i = 1 ; i < n - 1 ; i ++ ) {
7
- curMax = Math . max ( curMax , nums [ i ] ) ;
8
- if ( nums [ i ] < leftMax ) {
9
- leftMax = curMax ;
10
- leftPos = i ;
11
- }
12
- }
13
- return leftPos + 1 ;
14
- }
1
+ export default function partitionDisjoint ( nums : number [ ] ) : number {
2
+ const n = nums . length ;
3
+ let leftMax = nums [ 0 ] ,
4
+ leftPos = 0 ,
5
+ curMax = nums [ 0 ] ;
6
+ for ( let i = 1 ; i < n - 1 ; i ++ ) {
7
+ curMax = Math . max ( curMax , nums [ i ] ) ;
8
+ if ( nums [ i ] < leftMax ) {
9
+ leftMax = curMax ;
10
+ leftPos = i ;
11
+ }
12
+ }
13
+ return leftPos + 1 ;
14
+ }
Original file line number Diff line number Diff line change
1
+ export default function shortestBridge ( grid : number [ ] [ ] ) : number {
2
+ const directions = [
3
+ [ 0 , 1 ] ,
4
+ [ 1 , 0 ] ,
5
+ [ 0 , - 1 ] ,
6
+ [ - 1 , 0 ] ,
7
+ ] ;
8
+
9
+ let firstIsland : [ number , number ] [ ] = [ ] ;
10
+ loop:
11
+ for ( const [ i , a ] of grid . entries ( ) ) {
12
+ for ( const [ j , v ] of a . entries ( ) ) {
13
+ if ( v === 1 ) {
14
+ firstIsland . push ( [ i , j ] ) ;
15
+
16
+ for ( const [ x , y ] of firstIsland ) {
17
+ grid [ x ] [ y ] = 2 ;
18
+ for ( const [ q , w ] of directions ) {
19
+ const [ e , r ] = [ x + q , y + w ] ;
20
+ if ( grid [ e ] ?. [ r ] === 1 ) {
21
+ firstIsland . push ( [ e , r ] ) ;
22
+ grid [ e ] [ r ] = 2 ;
23
+ }
24
+ }
25
+ }
26
+ break loop;
27
+ }
28
+ }
29
+ }
30
+
31
+ let step = 0 ;
32
+ while ( firstIsland . length ) {
33
+ const temp : [ number , number ] [ ] = [ ] ;
34
+ for ( const [ x , y ] of firstIsland ) {
35
+ grid [ x ] [ y ] = 2 ;
36
+ for ( const [ q , w ] of directions ) {
37
+ const [ e , r ] = [ x + q , y + w ] ;
38
+ if ( grid [ e ] ?. [ r ] === 1 ) {
39
+ return step ;
40
+ }
41
+ if ( grid [ e ] ?. [ r ] === 0 ) {
42
+ temp . push ( [ e , r ] ) ;
43
+ grid [ e ] [ r ] = 2 ;
44
+ }
45
+ }
46
+ }
47
+ step ++ ;
48
+ firstIsland = temp ;
49
+ }
50
+ return - 1 ;
51
+ }
You can’t perform that action at this time.
0 commit comments