File tree 6 files changed +193
-0
lines changed
0628.Maximum Product of Three Numbers
0665.Non-decreasing Array
6 files changed +193
-0
lines changed Original file line number Diff line number Diff line change @@ -221,6 +221,48 @@ func max(a, b int) int {
221
221
}
222
222
```
223
223
224
+ ### ** TypeScript**
225
+
226
+ ``` ts
227
+ function maximumProduct(nums : number []): number {
228
+ nums .sort ((a , b ) => a - b );
229
+ const n = nums .length ;
230
+ const a = nums [n - 1 ] * nums [n - 2 ] * nums [n - 3 ];
231
+ const b = nums [n - 1 ] * nums [0 ] * nums [1 ];
232
+ return Math .max (a , b );
233
+ }
234
+ ```
235
+
236
+ ``` ts
237
+ function maximumProduct(nums : number []): number {
238
+ const inf = 1 << 30 ;
239
+ let mi1 = inf ,
240
+ mi2 = inf ;
241
+ let mx1 = - inf ,
242
+ mx2 = - inf ,
243
+ mx3 = - inf ;
244
+ for (const x of nums ) {
245
+ if (x < mi1 ) {
246
+ mi2 = mi1 ;
247
+ mi1 = x ;
248
+ } else if (x < mi2 ) {
249
+ mi2 = x ;
250
+ }
251
+ if (x > mx1 ) {
252
+ mx3 = mx2 ;
253
+ mx2 = mx1 ;
254
+ mx1 = x ;
255
+ } else if (x > mx2 ) {
256
+ mx3 = mx2 ;
257
+ mx2 = x ;
258
+ } else if (x > mx3 ) {
259
+ mx3 = x ;
260
+ }
261
+ }
262
+ return Math .max (mi1 * mi2 * mx1 , mx1 * mx2 * mx3 );
263
+ }
264
+ ```
265
+
224
266
### ** ...**
225
267
226
268
```
Original file line number Diff line number Diff line change @@ -181,6 +181,48 @@ func max(a, b int) int {
181
181
}
182
182
```
183
183
184
+ ### ** TypeScript**
185
+
186
+ ``` ts
187
+ function maximumProduct(nums : number []): number {
188
+ nums .sort ((a , b ) => a - b );
189
+ const n = nums .length ;
190
+ const a = nums [n - 1 ] * nums [n - 2 ] * nums [n - 3 ];
191
+ const b = nums [n - 1 ] * nums [0 ] * nums [1 ];
192
+ return Math .max (a , b );
193
+ }
194
+ ```
195
+
196
+ ``` ts
197
+ function maximumProduct(nums : number []): number {
198
+ const inf = 1 << 30 ;
199
+ let mi1 = inf ,
200
+ mi2 = inf ;
201
+ let mx1 = - inf ,
202
+ mx2 = - inf ,
203
+ mx3 = - inf ;
204
+ for (const x of nums ) {
205
+ if (x < mi1 ) {
206
+ mi2 = mi1 ;
207
+ mi1 = x ;
208
+ } else if (x < mi2 ) {
209
+ mi2 = x ;
210
+ }
211
+ if (x > mx1 ) {
212
+ mx3 = mx2 ;
213
+ mx2 = mx1 ;
214
+ mx1 = x ;
215
+ } else if (x > mx2 ) {
216
+ mx3 = mx2 ;
217
+ mx2 = x ;
218
+ } else if (x > mx3 ) {
219
+ mx3 = x ;
220
+ }
221
+ }
222
+ return Math .max (mi1 * mi2 * mx1 , mx1 * mx2 * mx3 );
223
+ }
224
+ ```
225
+
184
226
### ** ...**
185
227
186
228
```
Original file line number Diff line number Diff line change
1
+ function maximumProduct ( nums : number [ ] ) : number {
2
+ const inf = 1 << 30 ;
3
+ let mi1 = inf ,
4
+ mi2 = inf ;
5
+ let mx1 = - inf ,
6
+ mx2 = - inf ,
7
+ mx3 = - inf ;
8
+ for ( const x of nums ) {
9
+ if ( x < mi1 ) {
10
+ mi2 = mi1 ;
11
+ mi1 = x ;
12
+ } else if ( x < mi2 ) {
13
+ mi2 = x ;
14
+ }
15
+ if ( x > mx1 ) {
16
+ mx3 = mx2 ;
17
+ mx2 = mx1 ;
18
+ mx1 = x ;
19
+ } else if ( x > mx2 ) {
20
+ mx3 = mx2 ;
21
+ mx2 = x ;
22
+ } else if ( x > mx3 ) {
23
+ mx3 = x ;
24
+ }
25
+ }
26
+ return Math . max ( mi1 * mi2 * mx1 , mx1 * mx2 * mx3 ) ;
27
+ }
Original file line number Diff line number Diff line change @@ -162,6 +162,35 @@ func checkPossibility(nums []int) bool {
162
162
}
163
163
```
164
164
165
+ ### ** TypeScript**
166
+
167
+ ``` ts
168
+ function checkPossibility(nums : number []): boolean {
169
+ const isSorted = (nums : number []) => {
170
+ for (let i = 0 ; i < nums .length - 1 ; ++ i ) {
171
+ if (nums [i ] > nums [i + 1 ]) {
172
+ return false ;
173
+ }
174
+ }
175
+ return true ;
176
+ };
177
+ for (let i = 0 ; i < nums .length - 1 ; ++ i ) {
178
+ const a = nums [i ],
179
+ b = nums [i + 1 ];
180
+ if (a > b ) {
181
+ nums [i ] = b ;
182
+ if (isSorted (nums )) {
183
+ return true ;
184
+ }
185
+ nums [i ] = a ;
186
+ nums [i + 1 ] = a ;
187
+ return isSorted (nums );
188
+ }
189
+ }
190
+ return true ;
191
+ }
192
+ ```
193
+
165
194
### ** ...**
166
195
167
196
```
Original file line number Diff line number Diff line change @@ -141,6 +141,35 @@ func checkPossibility(nums []int) bool {
141
141
}
142
142
```
143
143
144
+ ### ** TypeScript**
145
+
146
+ ``` ts
147
+ function checkPossibility(nums : number []): boolean {
148
+ const isSorted = (nums : number []) => {
149
+ for (let i = 0 ; i < nums .length - 1 ; ++ i ) {
150
+ if (nums [i ] > nums [i + 1 ]) {
151
+ return false ;
152
+ }
153
+ }
154
+ return true ;
155
+ };
156
+ for (let i = 0 ; i < nums .length - 1 ; ++ i ) {
157
+ const a = nums [i ],
158
+ b = nums [i + 1 ];
159
+ if (a > b ) {
160
+ nums [i ] = b ;
161
+ if (isSorted (nums )) {
162
+ return true ;
163
+ }
164
+ nums [i ] = a ;
165
+ nums [i + 1 ] = a ;
166
+ return isSorted (nums );
167
+ }
168
+ }
169
+ return true ;
170
+ }
171
+ ```
172
+
144
173
### ** ...**
145
174
146
175
```
Original file line number Diff line number Diff line change
1
+ function checkPossibility ( nums : number [ ] ) : boolean {
2
+ const isSorted = ( nums : number [ ] ) => {
3
+ for ( let i = 0 ; i < nums . length - 1 ; ++ i ) {
4
+ if ( nums [ i ] > nums [ i + 1 ] ) {
5
+ return false ;
6
+ }
7
+ }
8
+ return true ;
9
+ } ;
10
+ for ( let i = 0 ; i < nums . length - 1 ; ++ i ) {
11
+ const a = nums [ i ] ,
12
+ b = nums [ i + 1 ] ;
13
+ if ( a > b ) {
14
+ nums [ i ] = b ;
15
+ if ( isSorted ( nums ) ) {
16
+ return true ;
17
+ }
18
+ nums [ i ] = a ;
19
+ nums [ i + 1 ] = a ;
20
+ return isSorted ( nums ) ;
21
+ }
22
+ }
23
+ return true ;
24
+ }
You can’t perform that action at this time.
0 commit comments