File tree Expand file tree Collapse file tree 3 files changed +127
-0
lines changed
solution/0800-0899/0838.Push Dominoes Expand file tree Collapse file tree 3 files changed +127
-0
lines changed Original file line number Diff line number Diff line change @@ -133,6 +133,50 @@ class Solution {
133
133
}
134
134
```
135
135
136
+ ### ** TypeScript**
137
+
138
+ ``` ts
139
+ function pushDominoes(dominoes : string ): string {
140
+ const n = dominoes .length ;
141
+ const map = {
142
+ L: - 1 ,
143
+ R: 1 ,
144
+ ' .' : 0 ,
145
+ }
146
+ let ans = new Array (n ).fill (0 );
147
+ let visited = new Array (n ).fill (0 );
148
+ let queue = [];
149
+ let depth = 1 ;
150
+ for (let i = 0 ; i < n ; i ++ ) {
151
+ let cur = map [dominoes .charAt (i )];
152
+ if (cur ) {
153
+ queue .push (i );
154
+ visited [i ] = depth ;
155
+ ans [i ] = cur ;
156
+ }
157
+ }
158
+ while (queue .length ) {
159
+ depth ++ ;
160
+ let nextLevel = [];
161
+ for (let i of queue ) {
162
+ const dx = ans [i ];
163
+ let x = i + dx ;
164
+ if (x >= 0 && x < n && [0 , depth ].includes (visited [x ])) {
165
+ ans [x ] += dx ;
166
+ visited [x ] = depth ;
167
+ nextLevel .push (x );
168
+ }
169
+ }
170
+ queue = nextLevel ;
171
+ }
172
+ return ans .map (d => {
173
+ if (! d ) return ' .' ;
174
+ else if (d < 0 ) return ' L' ;
175
+ else return ' R' ;
176
+ }).join (' ' );
177
+ };
178
+ ```
179
+
136
180
### ** C++**
137
181
138
182
``` cpp
Original file line number Diff line number Diff line change @@ -132,6 +132,50 @@ class Solution {
132
132
}
133
133
```
134
134
135
+ ### ** TypeScript**
136
+
137
+ ``` ts
138
+ function pushDominoes(dominoes : string ): string {
139
+ const n = dominoes .length ;
140
+ const map = {
141
+ L: - 1 ,
142
+ R: 1 ,
143
+ ' .' : 0 ,
144
+ }
145
+ let ans = new Array (n ).fill (0 );
146
+ let visited = new Array (n ).fill (0 );
147
+ let queue = [];
148
+ let depth = 1 ;
149
+ for (let i = 0 ; i < n ; i ++ ) {
150
+ let cur = map [dominoes .charAt (i )];
151
+ if (cur ) {
152
+ queue .push (i );
153
+ visited [i ] = depth ;
154
+ ans [i ] = cur ;
155
+ }
156
+ }
157
+ while (queue .length ) {
158
+ depth ++ ;
159
+ let nextLevel = [];
160
+ for (let i of queue ) {
161
+ const dx = ans [i ];
162
+ let x = i + dx ;
163
+ if (x >= 0 && x < n && [0 , depth ].includes (visited [x ])) {
164
+ ans [x ] += dx ;
165
+ visited [x ] = depth ;
166
+ nextLevel .push (x );
167
+ }
168
+ }
169
+ queue = nextLevel ;
170
+ }
171
+ return ans .map (d => {
172
+ if (! d ) return ' .' ;
173
+ else if (d < 0 ) return ' L' ;
174
+ else return ' R' ;
175
+ }).join (' ' );
176
+ };
177
+ ```
178
+
135
179
### ** C++**
136
180
137
181
``` cpp
Original file line number Diff line number Diff line change
1
+ function pushDominoes ( dominoes : string ) : string {
2
+ const n = dominoes . length ;
3
+ const map = {
4
+ L : - 1 ,
5
+ R : 1 ,
6
+ '.' : 0 ,
7
+ }
8
+ let ans = new Array ( n ) . fill ( 0 ) ;
9
+ let visited = new Array ( n ) . fill ( 0 ) ;
10
+ let queue = [ ] ;
11
+ let depth = 1 ;
12
+ for ( let i = 0 ; i < n ; i ++ ) {
13
+ let cur = map [ dominoes . charAt ( i ) ] ;
14
+ if ( cur ) {
15
+ queue . push ( i ) ;
16
+ visited [ i ] = depth ;
17
+ ans [ i ] = cur ;
18
+ }
19
+ }
20
+ while ( queue . length ) {
21
+ depth ++ ;
22
+ let nextLevel = [ ] ;
23
+ for ( let i of queue ) {
24
+ const dx = ans [ i ] ;
25
+ let x = i + dx ;
26
+ if ( x >= 0 && x < n && [ 0 , depth ] . includes ( visited [ x ] ) ) {
27
+ ans [ x ] += dx ;
28
+ visited [ x ] = depth ;
29
+ nextLevel . push ( x ) ;
30
+ }
31
+ }
32
+ queue = nextLevel ;
33
+ }
34
+ return ans . map ( d => {
35
+ if ( ! d ) return '.' ;
36
+ else if ( d < 0 ) return 'L' ;
37
+ else return 'R' ;
38
+ } ) . join ( '' ) ;
39
+ } ;
You can’t perform that action at this time.
0 commit comments