File tree Expand file tree Collapse file tree 3 files changed +75
-0
lines changed
leetcode practice/array and strings Expand file tree Collapse file tree 3 files changed +75
-0
lines changed Original file line number Diff line number Diff line change
1
+ var groupAnagrams = function ( strs ) {
2
+ const wordMap = { } ;
3
+ strs . forEach ( str => {
4
+ const sortedStr = str . split ( '' ) . sort ( ) . join ( ) ;
5
+ wordMap [ sortedStr ] = wordMap [ sortedStr ] === undefined ? [ str ] : [ ...wordMap [ sortedStr ] , str ] ;
6
+ } )
7
+
8
+ return Object . values ( wordMap )
9
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } matrix
3
+ * @return {void } Do not return anything, modify matrix in-place instead.
4
+ */
5
+ var setZeroes = function ( matrix ) {
6
+ const numRows = matrix . length ;
7
+ const numColumns = matrix [ 0 ] . length ;
8
+ let firstColHasZero = false ;
9
+
10
+ for ( let rowIndex = 0 ; rowIndex < numRows ; rowIndex ++ ) {
11
+ if ( matrix [ rowIndex ] [ 0 ] === 0 ) {
12
+ firstColHasZero = true ;
13
+ }
14
+
15
+ for ( let colIndex = 1 ; colIndex < numColumns ; colIndex ++ ) {
16
+ if ( matrix [ rowIndex ] [ colIndex ] === 0 ) {
17
+ matrix [ 0 ] [ colIndex ] = 0 ;
18
+ matrix [ rowIndex ] [ 0 ] = 0 ;
19
+ }
20
+ }
21
+ }
22
+
23
+ for ( let rowIndex = 1 ; rowIndex < numRows ; rowIndex ++ ) {
24
+ for ( let colIndex = 1 ; colIndex < numColumns ; colIndex ++ ) {
25
+ if ( matrix [ rowIndex ] [ 0 ] === 0 || matrix [ 0 ] [ colIndex ] === 0 ) {
26
+ matrix [ rowIndex ] [ colIndex ] = 0 ;
27
+ }
28
+ }
29
+ }
30
+
31
+ if ( matrix [ 0 ] [ 0 ] === 0 ) {
32
+ for ( let colIndex = 0 ; colIndex < numColumns ; colIndex ++ ) {
33
+ matrix [ 0 ] [ colIndex ] = 0 ;
34
+ }
35
+ }
36
+
37
+ if ( firstColHasZero ) {
38
+ for ( let rowIndex = 0 ; rowIndex < numRows ; rowIndex ++ ) {
39
+ matrix [ rowIndex ] [ 0 ] = 0 ;
40
+ }
41
+ }
42
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } height
3
+ * @return {number }
4
+ */
5
+ var trap = function ( bars ) {
6
+ if ( ! bars || bars . length === 0 ) { return null ; }
7
+
8
+ const stack = [ ] ;
9
+ let globalVolume = 0 ;
10
+ for ( let barIndex = 0 ; barIndex < bars . length ; barIndex ++ ) {
11
+ while ( stack . length !== 0 && bars [ barIndex ] > bars [ stack [ stack . length - 1 ] ] ) {
12
+ const shortBarIndex = stack . pop ( ) ;
13
+ if ( stack . length === 0 ) { break ; }
14
+ const lastPeakIndex = stack [ stack . length - 1 ] ;
15
+ const numberOfBars = barIndex - lastPeakIndex - 1 ;
16
+ const heightDifference = Math . min ( bars [ barIndex ] , bars [ lastPeakIndex ] ) - bars [ shortBarIndex ] ;
17
+ globalVolume += heightDifference * numberOfBars ;
18
+ }
19
+
20
+ stack . push ( barIndex ) ;
21
+ }
22
+
23
+ return globalVolume ;
24
+ } ;
You can’t perform that action at this time.
0 commit comments