File tree Expand file tree Collapse file tree 4 files changed +39
-49
lines changed Expand file tree Collapse file tree 4 files changed +39
-49
lines changed Original file line number Diff line number Diff line change @@ -99,13 +99,9 @@ https://garciparedes.me/#headers
99
99
- [x] 42
100
100
- [ ] 45
101
101
- [ ] 55
102
- - [ ] 56
103
- - [ ] 59
104
- - [ ] 62
105
- - [ ] 64
106
- - [ ] 66
107
- - [ ] 73
108
- - [ ] 75
102
+ - [x] 56
103
+ - [x] 66
104
+ - [x] 73
109
105
- [ ] 78
110
106
- [ ] 80
111
107
- [ ] 88
Original file line number Diff line number Diff line change 1
- export const merge = intervals => {
2
- let res = [ ] ;
3
- intervals . sort ( ( x , y ) => x . start - y . start ) ;
4
- let current = intervals . slice ( 0 , 1 ) [ 0 ] ;
1
+ const merge = ( intervals ) => {
2
+ intervals . sort ( ( x , y ) => x [ 0 ] - y [ 0 ] ) ;
3
+ const res = [ ] ;
4
+ let temp = intervals [ 0 ] ;
5
5
6
- for ( let { start, end } of intervals . slice ( 1 ) ) {
7
- if ( current . end < start ) {
8
- res . push ( [ current . start , current . end ] ) ;
9
- current = { start, end } ;
6
+ for ( let i = 1 ; i < intervals . length ; i ++ ) {
7
+ const cur = intervals [ i ] ;
8
+
9
+ if ( cur [ 0 ] > temp [ 1 ] ) {
10
+ res . push ( temp ) ;
11
+ temp = cur ;
10
12
} else {
11
- current = {
12
- start : Math . min ( current . start , start ) ,
13
- end : Math . max ( current . end , end ) ,
14
- } ;
13
+ temp [ 1 ] = Math . max ( cur [ 1 ] , temp [ 1 ] ) ;
15
14
}
16
15
}
17
16
18
- if ( current . start !== undefined ) {
19
- res . push ( [ current . start , current . end ] ) ;
20
- }
17
+ res . push ( temp ) ;
21
18
22
19
return res ;
23
20
} ;
Original file line number Diff line number Diff line change 1
1
export function plusOne ( digits ) {
2
- const lastIndex = digits . length - 1 ;
3
- let i = lastIndex ;
4
-
5
- while ( i >= 0 ) {
6
- if ( digits [ i ] !== 9 ) {
7
- digits [ i ] += 1 ;
8
- return digits ;
9
- }
2
+ let carry = 1 ;
3
+ const res = [ ] ;
4
+ for ( let i = digits . length - 1 ; i >= 0 ; i -- ) {
5
+ res . unshift ( ( digits [ i ] + carry ) % 10 ) ;
6
+ carry = digits [ i ] + carry === 10 ? 1 : 0 ;
7
+ }
10
8
11
- digits [ i ] = 0 ;
12
- if ( i === 0 ) {
13
- digits . unshift ( 1 ) ;
14
- return digits ;
15
- }
16
- i -- ;
9
+ if ( carry > 0 ) {
10
+ return [ 1 , ...res ] ;
17
11
}
18
12
19
- return digits ;
13
+ return res ;
20
14
}
Original file line number Diff line number Diff line change 1
- export const setZeroes = matrix => {
2
- const helper = Array . from ( { length : matrix . length } , _ =>
3
- Array ( matrix [ 0 ] . length ) . fill ( 1 ) ,
4
- ) ;
1
+ const setZeroes = ( matrix ) => {
2
+ const zeroesI = [ ] ;
3
+ const zeroesJ = [ ] ;
5
4
6
5
for ( let i = 0 ; i < matrix . length ; i ++ ) {
7
6
for ( let j = 0 ; j < matrix [ 0 ] . length ; j ++ ) {
8
- if ( matrix [ i ] [ j ] === 0 ) helper [ i ] [ j ] = 0 ;
7
+ if ( matrix [ i ] [ j ] === 0 ) {
8
+ zeroesI . push ( i ) ;
9
+ zeroesJ . push ( j ) ;
10
+ }
9
11
}
10
12
}
11
13
12
- for ( let i = 0 ; i < matrix . length ; i ++ ) {
14
+ for ( let n of zeroesI ) {
13
15
for ( let j = 0 ; j < matrix [ 0 ] . length ; j ++ ) {
14
- if ( helper [ i ] [ j ] === 0 ) {
15
- matrix [ i ] . fill ( 0 ) ;
16
- for ( let n = 0 ; n < matrix . length ; n ++ ) {
17
- matrix [ n ] [ j ] = 0 ;
18
- }
19
- }
16
+ matrix [ n ] [ j ] = 0 ;
17
+ }
18
+ }
19
+
20
+ for ( let n of zeroesJ ) {
21
+ for ( let j = 0 ; j < matrix . length ; j ++ ) {
22
+ matrix [ j ] [ n ] = 0 ;
20
23
}
21
24
}
22
25
You can’t perform that action at this time.
0 commit comments