File tree 3 files changed +42
-2
lines changed
solution/2200-2299/2280.Minimum Lines to Represent a Line Chart
3 files changed +42
-2
lines changed Original file line number Diff line number Diff line change @@ -149,7 +149,20 @@ func minimumLines(stockPrices [][]int) int {
149
149
### ** TypeScript**
150
150
151
151
``` ts
152
-
152
+ function minimumLines(stockPrices : number [][]): number {
153
+ const n = stockPrices .length ;
154
+ stockPrices .sort ((a , b ) => a [0 ] - b [0 ]);
155
+ let ans = 0 ;
156
+ let pre = [BigInt (0 ), BigInt (0 )];
157
+ for (let i = 1 ; i < n ; i ++ ) {
158
+ const [x1, y1] = stockPrices [i - 1 ];
159
+ const [x2, y2] = stockPrices [i ];
160
+ const dx = BigInt (x2 - x1 ), dy = BigInt (y2 - y1 );
161
+ if (i == 1 || (dx * pre [1 ] !== dy * pre [0 ])) ans ++ ;
162
+ pre = [dx , dy ];
163
+ }
164
+ return ans ;
165
+ };
153
166
```
154
167
155
168
### ** ...**
Original file line number Diff line number Diff line change @@ -131,7 +131,20 @@ func minimumLines(stockPrices [][]int) int {
131
131
### ** TypeScript**
132
132
133
133
``` ts
134
-
134
+ function minimumLines(stockPrices : number [][]): number {
135
+ const n = stockPrices .length ;
136
+ stockPrices .sort ((a , b ) => a [0 ] - b [0 ]);
137
+ let ans = 0 ;
138
+ let pre = [BigInt (0 ), BigInt (0 )];
139
+ for (let i = 1 ; i < n ; i ++ ) {
140
+ const [x1, y1] = stockPrices [i - 1 ];
141
+ const [x2, y2] = stockPrices [i ];
142
+ const dx = BigInt (x2 - x1 ), dy = BigInt (y2 - y1 );
143
+ if (i == 1 || (dx * pre [1 ] !== dy * pre [0 ])) ans ++ ;
144
+ pre = [dx , dy ];
145
+ }
146
+ return ans ;
147
+ };
135
148
```
136
149
137
150
### ** ...**
Original file line number Diff line number Diff line change
1
+ function minimumLines ( stockPrices : number [ ] [ ] ) : number {
2
+ const n = stockPrices . length ;
3
+ stockPrices . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
4
+ let ans = 0 ;
5
+ let pre = [ BigInt ( 0 ) , BigInt ( 0 ) ] ;
6
+ for ( let i = 1 ; i < n ; i ++ ) {
7
+ const [ x1 , y1 ] = stockPrices [ i - 1 ] ;
8
+ const [ x2 , y2 ] = stockPrices [ i ] ;
9
+ const dx = BigInt ( x2 - x1 ) , dy = BigInt ( y2 - y1 ) ;
10
+ if ( i == 1 || ( dx * pre [ 1 ] !== dy * pre [ 0 ] ) ) ans ++ ;
11
+ pre = [ dx , dy ] ;
12
+ }
13
+ return ans ;
14
+ } ;
You can’t perform that action at this time.
0 commit comments