File tree Expand file tree Collapse file tree 4 files changed +117
-1
lines changed
solution/0600-0699/0624.Maximum Distance in Arrays Expand file tree Collapse file tree 4 files changed +117
-1
lines changed Original file line number Diff line number Diff line change 21
21
22
22
<p ><strong >示例 1:</strong ></p >
23
23
24
- <pre ><strong >输入:</strong >
24
+ <pre ><strong >输入:</strong >
25
25
[[1,2,3],
26
26
[4,5],
27
27
[1,2,3]]
@@ -136,6 +136,48 @@ func abs(x int) int {
136
136
}
137
137
```
138
138
139
+ #### TypeScript
140
+
141
+ ``` ts
142
+ function maxDistance(arrays : number [][]): number {
143
+ const n = arrays .length ;
144
+ let res = 0 ;
145
+ let [min, max] = [Number .POSITIVE_INFINITY, Number .NEGATIVE_INFINITY];
146
+
147
+ for (let i = 0 ; i < n ; i ++ ) {
148
+ const a = arrays [i ];
149
+ res = Math .max (Math .max (a .at (- 1 )! - min , max - a [0 ]), res );
150
+ min = Math .min (min , a [0 ]);
151
+ max = Math .max (max , a .at (- 1 )! );
152
+ }
153
+
154
+ return res ;
155
+ }
156
+ ```
157
+
158
+ #### JavaScript
159
+
160
+ ``` js
161
+ /**
162
+ * @param {number[][]} arrays
163
+ * @return {number}
164
+ */
165
+ var maxDistance = function (arrays ) {
166
+ const n = arrays .length ;
167
+ let res = 0 ;
168
+ let [min, max] = [Number .POSITIVE_INFINITY , Number .NEGATIVE_INFINITY ];
169
+
170
+ for (let i = 0 ; i < n; i++ ) {
171
+ const a = arrays[i];
172
+ res = Math .max (Math .max (a .at (- 1 ) - min, max - a[0 ]), res);
173
+ min = Math .min (min, a[0 ]);
174
+ max = Math .max (max, a .at (- 1 ));
175
+ }
176
+
177
+ return res;
178
+ };
179
+ ```
180
+
139
181
<!-- tabs: end -->
140
182
141
183
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -139,6 +139,48 @@ func abs(x int) int {
139
139
}
140
140
```
141
141
142
+ #### TypeScript
143
+
144
+ ``` ts
145
+ function maxDistance(arrays : number [][]): number {
146
+ const n = arrays .length ;
147
+ let res = 0 ;
148
+ let [min, max] = [Number .POSITIVE_INFINITY, Number .NEGATIVE_INFINITY];
149
+
150
+ for (let i = 0 ; i < n ; i ++ ) {
151
+ const a = arrays [i ];
152
+ res = Math .max (Math .max (a .at (- 1 )! - min , max - a [0 ]), res );
153
+ min = Math .min (min , a [0 ]);
154
+ max = Math .max (max , a .at (- 1 )! );
155
+ }
156
+
157
+ return res ;
158
+ }
159
+ ```
160
+
161
+ #### JavaScript
162
+
163
+ ``` js
164
+ /**
165
+ * @param {number[][]} arrays
166
+ * @return {number}
167
+ */
168
+ var maxDistance = function (arrays ) {
169
+ const n = arrays .length ;
170
+ let res = 0 ;
171
+ let [min, max] = [Number .POSITIVE_INFINITY , Number .NEGATIVE_INFINITY ];
172
+
173
+ for (let i = 0 ; i < n; i++ ) {
174
+ const a = arrays[i];
175
+ res = Math .max (Math .max (a .at (- 1 ) - min, max - a[0 ]), res);
176
+ min = Math .min (min, a[0 ]);
177
+ max = Math .max (max, a .at (- 1 ));
178
+ }
179
+
180
+ return res;
181
+ };
182
+ ```
183
+
142
184
<!-- tabs: end -->
143
185
144
186
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } arrays
3
+ * @return {number }
4
+ */
5
+ var maxDistance = function ( arrays ) {
6
+ const n = arrays . length ;
7
+ let res = 0 ;
8
+ let [ min , max ] = [ Number . POSITIVE_INFINITY , Number . NEGATIVE_INFINITY ] ;
9
+
10
+ for ( let i = 0 ; i < n ; i ++ ) {
11
+ const a = arrays [ i ] ;
12
+ res = Math . max ( Math . max ( a . at ( - 1 ) - min , max - a [ 0 ] ) , res ) ;
13
+ min = Math . min ( min , a [ 0 ] ) ;
14
+ max = Math . max ( max , a . at ( - 1 ) ) ;
15
+ }
16
+
17
+ return res ;
18
+ } ;
Original file line number Diff line number Diff line change
1
+ function maxDistance ( arrays : number [ ] [ ] ) : number {
2
+ const n = arrays . length ;
3
+ let res = 0 ;
4
+ let [ min , max ] = [ Number . POSITIVE_INFINITY , Number . NEGATIVE_INFINITY ] ;
5
+
6
+ for ( let i = 0 ; i < n ; i ++ ) {
7
+ const a = arrays [ i ] ;
8
+ res = Math . max ( Math . max ( a . at ( - 1 ) ! - min , max - a [ 0 ] ) , res ) ;
9
+ min = Math . min ( min , a [ 0 ] ) ;
10
+ max = Math . max ( max , a . at ( - 1 ) ! ) ;
11
+ }
12
+
13
+ return res ;
14
+ }
You can’t perform that action at this time.
0 commit comments