File tree Expand file tree Collapse file tree 6 files changed +125
-0
lines changed
2543.Check if Point Is Reachable
2547.Minimum Cost to Split an Array Expand file tree Collapse file tree 6 files changed +125
-0
lines changed Original file line number Diff line number Diff line change @@ -117,6 +117,19 @@ func gcd(a, b int) int {
117
117
}
118
118
```
119
119
120
+ ### ** TypeScript**
121
+
122
+ ``` ts
123
+ function isReachable(targetX : number , targetY : number ): boolean {
124
+ const x = gcd (targetX , targetY );
125
+ return (x & (x - 1 )) === 0 ;
126
+ }
127
+
128
+ function gcd(a : number , b : number ): number {
129
+ return b == 0 ? a : gcd (b , a % b );
130
+ }
131
+ ```
132
+
120
133
### ** ...**
121
134
122
135
```
Original file line number Diff line number Diff line change @@ -97,6 +97,19 @@ func gcd(a, b int) int {
97
97
}
98
98
```
99
99
100
+ ### ** TypeScript**
101
+
102
+ ``` ts
103
+ function isReachable(targetX : number , targetY : number ): boolean {
104
+ const x = gcd (targetX , targetY );
105
+ return (x & (x - 1 )) === 0 ;
106
+ }
107
+
108
+ function gcd(a : number , b : number ): number {
109
+ return b == 0 ? a : gcd (b , a % b );
110
+ }
111
+ ```
112
+
100
113
### ** ...**
101
114
102
115
```
Original file line number Diff line number Diff line change
1
+ function isReachable ( targetX : number , targetY : number ) : boolean {
2
+ const x = gcd ( targetX , targetY ) ;
3
+ return ( x & ( x - 1 ) ) === 0 ;
4
+ }
5
+
6
+ function gcd ( a : number , b : number ) : number {
7
+ return b == 0 ? a : gcd ( b , a % b ) ;
8
+ }
Original file line number Diff line number Diff line change @@ -236,6 +236,38 @@ func min(a, b int) int {
236
236
}
237
237
```
238
238
239
+ ### ** TypeScript**
240
+
241
+ ``` ts
242
+ function minCost(nums : number [], k : number ): number {
243
+ const n = nums .length ;
244
+ const f = new Array (n ).fill (0 );
245
+ const dfs = (i : number ) => {
246
+ if (i >= n ) {
247
+ return 0 ;
248
+ }
249
+ if (f [i ]) {
250
+ return f [i ];
251
+ }
252
+ const cnt = new Array (n ).fill (0 );
253
+ let one = 0 ;
254
+ let ans = 1 << 30 ;
255
+ for (let j = i ; j < n ; ++ j ) {
256
+ const x = ++ cnt [nums [j ]];
257
+ if (x == 1 ) {
258
+ ++ one ;
259
+ } else if (x == 2 ) {
260
+ -- one ;
261
+ }
262
+ ans = Math .min (ans , k + j - i + 1 - one + dfs (j + 1 ));
263
+ }
264
+ f [i ] = ans ;
265
+ return f [i ];
266
+ };
267
+ return dfs (0 );
268
+ }
269
+ ```
270
+
239
271
### ** ...**
240
272
241
273
```
Original file line number Diff line number Diff line change @@ -220,6 +220,38 @@ func min(a, b int) int {
220
220
}
221
221
```
222
222
223
+ ### ** TypeScript**
224
+
225
+ ``` ts
226
+ function minCost(nums : number [], k : number ): number {
227
+ const n = nums .length ;
228
+ const f = new Array (n ).fill (0 );
229
+ const dfs = (i : number ) => {
230
+ if (i >= n ) {
231
+ return 0 ;
232
+ }
233
+ if (f [i ]) {
234
+ return f [i ];
235
+ }
236
+ const cnt = new Array (n ).fill (0 );
237
+ let one = 0 ;
238
+ let ans = 1 << 30 ;
239
+ for (let j = i ; j < n ; ++ j ) {
240
+ const x = ++ cnt [nums [j ]];
241
+ if (x == 1 ) {
242
+ ++ one ;
243
+ } else if (x == 2 ) {
244
+ -- one ;
245
+ }
246
+ ans = Math .min (ans , k + j - i + 1 - one + dfs (j + 1 ));
247
+ }
248
+ f [i ] = ans ;
249
+ return f [i ];
250
+ };
251
+ return dfs (0 );
252
+ }
253
+ ```
254
+
223
255
### ** ...**
224
256
225
257
```
Original file line number Diff line number Diff line change
1
+ function minCost ( nums : number [ ] , k : number ) : number {
2
+ const n = nums . length ;
3
+ const f = new Array ( n ) . fill ( 0 ) ;
4
+ const dfs = ( i : number ) => {
5
+ if ( i >= n ) {
6
+ return 0 ;
7
+ }
8
+ if ( f [ i ] ) {
9
+ return f [ i ] ;
10
+ }
11
+ const cnt = new Array ( n ) . fill ( 0 ) ;
12
+ let one = 0 ;
13
+ let ans = 1 << 30 ;
14
+ for ( let j = i ; j < n ; ++ j ) {
15
+ const x = ++ cnt [ nums [ j ] ] ;
16
+ if ( x == 1 ) {
17
+ ++ one ;
18
+ } else if ( x == 2 ) {
19
+ -- one ;
20
+ }
21
+ ans = Math . min ( ans , k + j - i + 1 - one + dfs ( j + 1 ) ) ;
22
+ }
23
+ f [ i ] = ans ;
24
+ return f [ i ] ;
25
+ } ;
26
+ return dfs ( 0 ) ;
27
+ }
You can’t perform that action at this time.
0 commit comments